utils.js 4.3 KB

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