utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. var compactQueue = function compactQueue(queue) {
  11. var obj;
  12. while (queue.length) {
  13. var item = queue.pop();
  14. obj = item.obj[item.prop];
  15. if (Array.isArray(obj)) {
  16. var compacted = [];
  17. for (var j = 0; j < obj.length; ++j) {
  18. if (typeof obj[j] !== 'undefined') {
  19. compacted.push(obj[j]);
  20. }
  21. }
  22. item.obj[item.prop] = compacted;
  23. }
  24. }
  25. return obj;
  26. };
  27. var arrayToObject = function arrayToObject(source, options) {
  28. var obj = options && options.plainObjects ? Object.create(null) : {};
  29. for (var i = 0; i < source.length; ++i) {
  30. if (typeof source[i] !== 'undefined') {
  31. obj[i] = source[i];
  32. }
  33. }
  34. return obj;
  35. };
  36. var merge = function merge(target, source, options) {
  37. if (!source) {
  38. return target;
  39. }
  40. if (typeof source !== 'object') {
  41. if (Array.isArray(target)) {
  42. target.push(source);
  43. } else if (target && typeof target === 'object') {
  44. if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
  45. target[source] = true;
  46. }
  47. } else {
  48. return [target, source];
  49. }
  50. return target;
  51. }
  52. if (!target || typeof target !== 'object') {
  53. return [target].concat(source);
  54. }
  55. var mergeTarget = target;
  56. if (Array.isArray(target) && !Array.isArray(source)) {
  57. mergeTarget = arrayToObject(target, options);
  58. }
  59. if (Array.isArray(target) && Array.isArray(source)) {
  60. source.forEach(function (item, i) {
  61. if (has.call(target, i)) {
  62. var targetItem = target[i];
  63. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  64. target[i] = merge(targetItem, item, options);
  65. } else {
  66. target.push(item);
  67. }
  68. } else {
  69. target[i] = item;
  70. }
  71. });
  72. return target;
  73. }
  74. return Object.keys(source).reduce(function (acc, key) {
  75. var value = source[key];
  76. if (has.call(acc, key)) {
  77. acc[key] = merge(acc[key], value, options);
  78. } else {
  79. acc[key] = value;
  80. }
  81. return acc;
  82. }, mergeTarget);
  83. };
  84. var assign = function assignSingleSource(target, source) {
  85. return Object.keys(source).reduce(function (acc, key) {
  86. acc[key] = source[key];
  87. return acc;
  88. }, target);
  89. };
  90. var decode = function (str) {
  91. try {
  92. return decodeURIComponent(str.replace(/\+/g, ' '));
  93. } catch (e) {
  94. return str;
  95. }
  96. };
  97. var encode = function encode(str) {
  98. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  99. // It has been adapted here for stricter adherence to RFC 3986
  100. if (str.length === 0) {
  101. return str;
  102. }
  103. var string = typeof str === 'string' ? str : String(str);
  104. var out = '';
  105. for (var i = 0; i < string.length; ++i) {
  106. var c = string.charCodeAt(i);
  107. if (
  108. c === 0x2D // -
  109. || c === 0x2E // .
  110. || c === 0x5F // _
  111. || c === 0x7E // ~
  112. || (c >= 0x30 && c <= 0x39) // 0-9
  113. || (c >= 0x41 && c <= 0x5A) // a-z
  114. || (c >= 0x61 && c <= 0x7A) // A-Z
  115. ) {
  116. out += string.charAt(i);
  117. continue;
  118. }
  119. if (c < 0x80) {
  120. out = out + hexTable[c];
  121. continue;
  122. }
  123. if (c < 0x800) {
  124. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  125. continue;
  126. }
  127. if (c < 0xD800 || c >= 0xE000) {
  128. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  129. continue;
  130. }
  131. i += 1;
  132. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  133. /* eslint operator-linebreak: [2, "before"] */
  134. out += hexTable[0xF0 | (c >> 18)]
  135. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  136. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  137. + hexTable[0x80 | (c & 0x3F)];
  138. }
  139. return out;
  140. };
  141. var compact = function compact(value) {
  142. var queue = [{ obj: { o: value }, prop: 'o' }];
  143. var refs = [];
  144. for (var i = 0; i < queue.length; ++i) {
  145. var item = queue[i];
  146. var obj = item.obj[item.prop];
  147. var keys = Object.keys(obj);
  148. for (var j = 0; j < keys.length; ++j) {
  149. var key = keys[j];
  150. var val = obj[key];
  151. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  152. queue.push({ obj: obj, prop: key });
  153. refs.push(val);
  154. }
  155. }
  156. }
  157. return compactQueue(queue);
  158. };
  159. var isRegExp = function isRegExp(obj) {
  160. return Object.prototype.toString.call(obj) === '[object RegExp]';
  161. };
  162. var isBuffer = function isBuffer(obj) {
  163. if (obj === null || typeof obj === 'undefined') {
  164. return false;
  165. }
  166. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  167. };
  168. module.exports = {
  169. arrayToObject: arrayToObject,
  170. assign: assign,
  171. compact: compact,
  172. decode: decode,
  173. encode: encode,
  174. isBuffer: isBuffer,
  175. isRegExp: isRegExp,
  176. merge: merge
  177. };