utils.js 4.8 KB

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