utils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. out += hexTable[0xF0 | (c >> 18)]
  152. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  153. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  154. + hexTable[0x80 | (c & 0x3F)];
  155. }
  156. return out;
  157. };
  158. var compact = function compact(value) {
  159. var queue = [{ obj: { o: value }, prop: 'o' }];
  160. var refs = [];
  161. for (var i = 0; i < queue.length; ++i) {
  162. var item = queue[i];
  163. var obj = item.obj[item.prop];
  164. var keys = Object.keys(obj);
  165. for (var j = 0; j < keys.length; ++j) {
  166. var key = keys[j];
  167. var val = obj[key];
  168. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  169. queue.push({ obj: obj, prop: key });
  170. refs.push(val);
  171. }
  172. }
  173. }
  174. compactQueue(queue);
  175. return value;
  176. };
  177. var isRegExp = function isRegExp(obj) {
  178. return Object.prototype.toString.call(obj) === '[object RegExp]';
  179. };
  180. var isBuffer = function isBuffer(obj) {
  181. if (!obj || typeof obj !== 'object') {
  182. return false;
  183. }
  184. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  185. };
  186. var combine = function combine(a, b) {
  187. return [].concat(a, b);
  188. };
  189. var maybeMap = function maybeMap(val, fn) {
  190. if (isArray(val)) {
  191. var mapped = [];
  192. for (var i = 0; i < val.length; i += 1) {
  193. mapped.push(fn(val[i]));
  194. }
  195. return mapped;
  196. }
  197. return fn(val);
  198. };
  199. module.exports = {
  200. arrayToObject: arrayToObject,
  201. assign: assign,
  202. combine: combine,
  203. compact: compact,
  204. decode: decode,
  205. encode: encode,
  206. isBuffer: isBuffer,
  207. isRegExp: isRegExp,
  208. maybeMap: maybeMap,
  209. merge: merge
  210. };