utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Load modules
  2. // Declare internals
  3. var internals = {};
  4. internals.hexTable = new Array(256);
  5. for (var h = 0; h < 256; ++h) {
  6. internals.hexTable[h] = '%' + ((h < 16 ? '0' : '') + h.toString(16)).toUpperCase();
  7. }
  8. exports.arrayToObject = function (source, options) {
  9. var obj = options.plainObjects ? Object.create(null) : {};
  10. for (var i = 0, il = source.length; i < il; ++i) {
  11. if (typeof source[i] !== 'undefined') {
  12. obj[i] = source[i];
  13. }
  14. }
  15. return obj;
  16. };
  17. exports.merge = function (target, source, options) {
  18. if (!source) {
  19. return target;
  20. }
  21. if (typeof source !== 'object') {
  22. if (Array.isArray(target)) {
  23. target.push(source);
  24. }
  25. else if (typeof target === 'object') {
  26. target[source] = true;
  27. }
  28. else {
  29. target = [target, source];
  30. }
  31. return target;
  32. }
  33. if (typeof target !== 'object') {
  34. target = [target].concat(source);
  35. return target;
  36. }
  37. if (Array.isArray(target) &&
  38. !Array.isArray(source)) {
  39. target = exports.arrayToObject(target, options);
  40. }
  41. var keys = Object.keys(source);
  42. for (var k = 0, kl = keys.length; k < kl; ++k) {
  43. var key = keys[k];
  44. var value = source[key];
  45. if (!Object.prototype.hasOwnProperty.call(target, key)) {
  46. target[key] = value;
  47. }
  48. else {
  49. target[key] = exports.merge(target[key], value, options);
  50. }
  51. }
  52. return target;
  53. };
  54. exports.decode = function (str) {
  55. try {
  56. return decodeURIComponent(str.replace(/\+/g, ' '));
  57. } catch (e) {
  58. return str;
  59. }
  60. };
  61. exports.encode = function (str) {
  62. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  63. // It has been adapted here for stricter adherence to RFC 3986
  64. if (str.length === 0) {
  65. return str;
  66. }
  67. if (typeof str !== 'string') {
  68. str = '' + str;
  69. }
  70. var out = '';
  71. for (var i = 0, il = str.length; i < il; ++i) {
  72. var c = str.charCodeAt(i);
  73. if (c === 0x2D || // -
  74. c === 0x2E || // .
  75. c === 0x5F || // _
  76. c === 0x7E || // ~
  77. (c >= 0x30 && c <= 0x39) || // 0-9
  78. (c >= 0x41 && c <= 0x5A) || // a-z
  79. (c >= 0x61 && c <= 0x7A)) { // A-Z
  80. out += str[i];
  81. continue;
  82. }
  83. if (c < 0x80) {
  84. out += internals.hexTable[c];
  85. continue;
  86. }
  87. if (c < 0x800) {
  88. out += internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)];
  89. continue;
  90. }
  91. if (c < 0xD800 || c >= 0xE000) {
  92. out += internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
  93. continue;
  94. }
  95. ++i;
  96. c = 0x10000 + (((c & 0x3FF) << 10) | (str.charCodeAt(i) & 0x3FF));
  97. out += internals.hexTable[0xF0 | (c >> 18)] + internals.hexTable[0x80 | ((c >> 12) & 0x3F)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
  98. }
  99. return out;
  100. };
  101. exports.compact = function (obj, refs) {
  102. if (typeof obj !== 'object' ||
  103. obj === null) {
  104. return obj;
  105. }
  106. refs = refs || [];
  107. var lookup = refs.indexOf(obj);
  108. if (lookup !== -1) {
  109. return refs[lookup];
  110. }
  111. refs.push(obj);
  112. if (Array.isArray(obj)) {
  113. var compacted = [];
  114. for (var i = 0, il = obj.length; i < il; ++i) {
  115. if (typeof obj[i] !== 'undefined') {
  116. compacted.push(obj[i]);
  117. }
  118. }
  119. return compacted;
  120. }
  121. var keys = Object.keys(obj);
  122. for (i = 0, il = keys.length; i < il; ++i) {
  123. var key = keys[i];
  124. obj[key] = exports.compact(obj[key], refs);
  125. }
  126. return obj;
  127. };
  128. exports.isRegExp = function (obj) {
  129. return Object.prototype.toString.call(obj) === '[object RegExp]';
  130. };
  131. exports.isBuffer = function (obj) {
  132. if (obj === null ||
  133. typeof obj === 'undefined') {
  134. return false;
  135. }
  136. return !!(obj.constructor &&
  137. obj.constructor.isBuffer &&
  138. obj.constructor.isBuffer(obj));
  139. };