utils.js 7.2 KB

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