utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var array_intersection, crypto, escapable, lookup, unroll_lookup;
  4. crypto = require('crypto');
  5. exports.array_intersection = array_intersection = function(arr_a, arr_b) {
  6. var a, j, len, r;
  7. r = [];
  8. for (j = 0, len = arr_a.length; j < len; j++) {
  9. a = arr_a[j];
  10. if (arr_b.indexOf(a) !== -1) {
  11. r.push(a);
  12. }
  13. }
  14. return r;
  15. };
  16. exports.escape_selected = function(str, chars) {
  17. var c, i, j, l, len, map, parts, r, ref, v;
  18. map = {};
  19. chars = '%' + chars;
  20. for (j = 0, len = chars.length; j < len; j++) {
  21. c = chars[j];
  22. map[c] = escape(c);
  23. }
  24. r = new RegExp('([' + chars + '])');
  25. parts = str.split(r);
  26. for (i = l = 0, ref = parts.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) {
  27. v = parts[i];
  28. if (v.length === 1 && v in map) {
  29. parts[i] = map[v];
  30. }
  31. }
  32. return parts.join('');
  33. };
  34. exports.buffer_concat = function(buf_a, buf_b) {
  35. var dst;
  36. dst = new Buffer(buf_a.length + buf_b.length);
  37. buf_a.copy(dst);
  38. buf_b.copy(dst, buf_a.length);
  39. return dst;
  40. };
  41. exports.md5_hex = function(data) {
  42. return crypto.createHash('md5').update(data).digest('hex');
  43. };
  44. exports.sha1_base64 = function(data) {
  45. return crypto.createHash('sha1').update(data).digest('base64');
  46. };
  47. exports.timeout_chain = function(arr) {
  48. var fun, ref, timeout, user_fun;
  49. arr = arr.slice(0);
  50. if (!arr.length) {
  51. return;
  52. }
  53. ref = arr.shift(), timeout = ref[0], user_fun = ref[1];
  54. fun = (function(_this) {
  55. return function() {
  56. user_fun();
  57. return exports.timeout_chain(arr);
  58. };
  59. })(this);
  60. return setTimeout(fun, timeout);
  61. };
  62. exports.objectExtend = function(dst, src) {
  63. var k;
  64. for (k in src) {
  65. if (src.hasOwnProperty(k)) {
  66. dst[k] = src[k];
  67. }
  68. }
  69. return dst;
  70. };
  71. exports.overshadowListeners = function(ee, event, handler) {
  72. var new_handler, old_listeners;
  73. old_listeners = ee.listeners(event).slice(0);
  74. ee.removeAllListeners(event);
  75. new_handler = function() {
  76. var j, len, listener;
  77. if (handler.apply(this, arguments) !== true) {
  78. for (j = 0, len = old_listeners.length; j < len; j++) {
  79. listener = old_listeners[j];
  80. listener.apply(this, arguments);
  81. }
  82. return false;
  83. }
  84. return true;
  85. };
  86. return ee.addListener(event, new_handler);
  87. };
  88. escapable = /[\x00-\x1f\ud800-\udfff\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufff0-\uffff]/g;
  89. unroll_lookup = function(escapable) {
  90. var c, i, unrolled;
  91. unrolled = {};
  92. c = (function() {
  93. var j, results;
  94. results = [];
  95. for (i = j = 0; j < 65536; i = ++j) {
  96. results.push(String.fromCharCode(i));
  97. }
  98. return results;
  99. })();
  100. escapable.lastIndex = 0;
  101. c.join('').replace(escapable, function(a) {
  102. return unrolled[a] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  103. });
  104. return unrolled;
  105. };
  106. lookup = unroll_lookup(escapable);
  107. exports.quote = function(string) {
  108. var quoted;
  109. quoted = JSON.stringify(string);
  110. escapable.lastIndex = 0;
  111. if (!escapable.test(quoted)) {
  112. return quoted;
  113. }
  114. return quoted.replace(escapable, function(a) {
  115. return lookup[a];
  116. });
  117. };
  118. exports.parseCookie = function(cookie_header) {
  119. var cookie, cookies, j, len, parts, ref;
  120. cookies = {};
  121. if (cookie_header) {
  122. ref = cookie_header.split(';');
  123. for (j = 0, len = ref.length; j < len; j++) {
  124. cookie = ref[j];
  125. parts = cookie.split('=');
  126. cookies[parts[0].trim()] = (parts[1] || '').trim();
  127. }
  128. }
  129. return cookies;
  130. };
  131. exports.random32 = function() {
  132. var foo, v;
  133. foo = crypto.randomBytes(4);
  134. v = [foo[0], foo[1], foo[2], foo[3]];
  135. return v[0] + (v[1] * 256) + (v[2] * 256 * 256) + (v[3] * 256 * 256 * 256);
  136. };
  137. }).call(this);