utils.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. var formats = require('./formats');
  3. var has = Object.prototype.hasOwnProperty;
  4. var isArray = Array.isArray;
  5. var hexTable = (function () {
  6. var array = [];
  7. for (var i = 0; i < 256; ++i) {
  8. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  9. }
  10. return array;
  11. }());
  12. var compactQueue = function compactQueue(queue) {
  13. while (queue.length > 1) {
  14. var item = queue.pop();
  15. var obj = item.obj[item.prop];
  16. if (isArray(obj)) {
  17. var compacted = [];
  18. for (var j = 0; j < obj.length; ++j) {
  19. if (typeof obj[j] !== 'undefined') {
  20. compacted.push(obj[j]);
  21. }
  22. }
  23. item.obj[item.prop] = compacted;
  24. }
  25. }
  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. /* eslint no-param-reassign: 0 */
  38. if (!source) {
  39. return target;
  40. }
  41. if (typeof source !== 'object') {
  42. if (isArray(target)) {
  43. target.push(source);
  44. } else if (target && typeof target === 'object') {
  45. if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
  46. target[source] = true;
  47. }
  48. } else {
  49. return [target, source];
  50. }
  51. return target;
  52. }
  53. if (!target || typeof target !== 'object') {
  54. return [target].concat(source);
  55. }
  56. var mergeTarget = target;
  57. if (isArray(target) && !isArray(source)) {
  58. mergeTarget = arrayToObject(target, options);
  59. }
  60. if (isArray(target) && isArray(source)) {
  61. source.forEach(function (item, i) {
  62. if (has.call(target, i)) {
  63. var targetItem = target[i];
  64. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  65. target[i] = merge(targetItem, item, options);
  66. } else {
  67. target.push(item);
  68. }
  69. } else {
  70. target[i] = item;
  71. }
  72. });
  73. return target;
  74. }
  75. return Object.keys(source).reduce(function (acc, key) {
  76. var value = source[key];
  77. if (has.call(acc, key)) {
  78. acc[key] = merge(acc[key], value, options);
  79. } else {
  80. acc[key] = value;
  81. }
  82. return acc;
  83. }, mergeTarget);
  84. };
  85. var assign = function assignSingleSource(target, source) {
  86. return Object.keys(source).reduce(function (acc, key) {
  87. acc[key] = source[key];
  88. return acc;
  89. }, target);
  90. };
  91. var decode = function (str, decoder, charset) {
  92. var strWithoutPlus = str.replace(/\+/g, ' ');
  93. if (charset === 'iso-8859-1') {
  94. // unescape never throws, no try...catch needed:
  95. return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
  96. }
  97. // utf-8
  98. try {
  99. return decodeURIComponent(strWithoutPlus);
  100. } catch (e) {
  101. return strWithoutPlus;
  102. }
  103. };
  104. var encode = function encode(str, defaultEncoder, charset, kind, format) {
  105. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  106. // It has been adapted here for stricter adherence to RFC 3986
  107. if (str.length === 0) {
  108. return str;
  109. }
  110. var string = str;
  111. if (typeof str === 'symbol') {
  112. string = Symbol.prototype.toString.call(str);
  113. } else if (typeof str !== 'string') {
  114. string = String(str);
  115. }
  116. if (charset === 'iso-8859-1') {
  117. return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
  118. return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
  119. });
  120. }
  121. var out = '';
  122. for (var i = 0; i < string.length; ++i) {
  123. var c = string.charCodeAt(i);
  124. if (
  125. c === 0x2D // -
  126. || c === 0x2E // .
  127. || c === 0x5F // _
  128. || c === 0x7E // ~
  129. || (c >= 0x30 && c <= 0x39) // 0-9
  130. || (c >= 0x41 && c <= 0x5A) // a-z
  131. || (c >= 0x61 && c <= 0x7A) // A-Z
  132. || (format === formats.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
  133. ) {
  134. out += string.charAt(i);
  135. continue;
  136. }
  137. if (c < 0x80) {
  138. out = out + hexTable[c];
  139. continue;
  140. }
  141. if (c < 0x800) {
  142. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  143. continue;
  144. }
  145. if (c < 0xD800 || c >= 0xE000) {
  146. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  147. continue;
  148. }
  149. i += 1;
  150. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  151. /* eslint operator-linebreak: [2, "before"] */
  152. out += hexTable[0xF0 | (c >> 18)]
  153. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  154. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  155. + hexTable[0x80 | (c & 0x3F)];
  156. }
  157. return out;
  158. };
  159. var compact = function compact(value) {
  160. var queue = [{ obj: { o: value }, prop: 'o' }];
  161. var refs = [];
  162. for (var i = 0; i < queue.length; ++i) {
  163. var item = queue[i];
  164. var obj = item.obj[item.prop];
  165. var keys = Object.keys(obj);
  166. for (var j = 0; j < keys.length; ++j) {
  167. var key = keys[j];
  168. var val = obj[key];
  169. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  170. queue.push({ obj: obj, prop: key });
  171. refs.push(val);
  172. }
  173. }
  174. }
  175. compactQueue(queue);
  176. return value;
  177. };
  178. var isRegExp = function isRegExp(obj) {
  179. return Object.prototype.toString.call(obj) === '[object RegExp]';
  180. };
  181. var isBuffer = function isBuffer(obj) {
  182. if (!obj || typeof obj !== 'object') {
  183. return false;
  184. }
  185. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  186. };
  187. var combine = function combine(a, b) {
  188. return [].concat(a, b);
  189. };
  190. var maybeMap = function maybeMap(val, fn) {
  191. if (isArray(val)) {
  192. var mapped = [];
  193. for (var i = 0; i < val.length; i += 1) {
  194. mapped.push(fn(val[i]));
  195. }
  196. return mapped;
  197. }
  198. return fn(val);
  199. };
  200. module.exports = {
  201. arrayToObject: arrayToObject,
  202. assign: assign,
  203. combine: combine,
  204. compact: compact,
  205. decode: decode,
  206. encode: encode,
  207. isBuffer: isBuffer,
  208. isRegExp: isRegExp,
  209. maybeMap: maybeMap,
  210. merge: merge
  211. };