utils.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.maybe_map = exports.combine = exports.is_buffer = exports.is_regexp = exports.compact = exports.encode = exports.decode = exports.assign_single_source = exports.merge = void 0;
  4. const formats_1 = require("./formats.js");
  5. const has = Object.prototype.hasOwnProperty;
  6. const is_array = Array.isArray;
  7. const hex_table = (() => {
  8. const array = [];
  9. for (let i = 0; i < 256; ++i) {
  10. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  11. }
  12. return array;
  13. })();
  14. function compact_queue(queue) {
  15. while (queue.length > 1) {
  16. const item = queue.pop();
  17. if (!item)
  18. continue;
  19. const obj = item.obj[item.prop];
  20. if (is_array(obj)) {
  21. const compacted = [];
  22. for (let j = 0; j < obj.length; ++j) {
  23. if (typeof obj[j] !== 'undefined') {
  24. compacted.push(obj[j]);
  25. }
  26. }
  27. // @ts-ignore
  28. item.obj[item.prop] = compacted;
  29. }
  30. }
  31. }
  32. function array_to_object(source, options) {
  33. const obj = options && options.plainObjects ? Object.create(null) : {};
  34. for (let i = 0; i < source.length; ++i) {
  35. if (typeof source[i] !== 'undefined') {
  36. obj[i] = source[i];
  37. }
  38. }
  39. return obj;
  40. }
  41. function merge(target, source, options = {}) {
  42. if (!source) {
  43. return target;
  44. }
  45. if (typeof source !== 'object') {
  46. if (is_array(target)) {
  47. target.push(source);
  48. }
  49. else if (target && typeof target === 'object') {
  50. if ((options && (options.plainObjects || options.allowPrototypes)) ||
  51. !has.call(Object.prototype, source)) {
  52. target[source] = true;
  53. }
  54. }
  55. else {
  56. return [target, source];
  57. }
  58. return target;
  59. }
  60. if (!target || typeof target !== 'object') {
  61. return [target].concat(source);
  62. }
  63. let mergeTarget = target;
  64. if (is_array(target) && !is_array(source)) {
  65. // @ts-ignore
  66. mergeTarget = array_to_object(target, options);
  67. }
  68. if (is_array(target) && is_array(source)) {
  69. source.forEach(function (item, i) {
  70. if (has.call(target, i)) {
  71. const targetItem = target[i];
  72. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  73. target[i] = merge(targetItem, item, options);
  74. }
  75. else {
  76. target.push(item);
  77. }
  78. }
  79. else {
  80. target[i] = item;
  81. }
  82. });
  83. return target;
  84. }
  85. return Object.keys(source).reduce(function (acc, key) {
  86. const value = source[key];
  87. if (has.call(acc, key)) {
  88. acc[key] = merge(acc[key], value, options);
  89. }
  90. else {
  91. acc[key] = value;
  92. }
  93. return acc;
  94. }, mergeTarget);
  95. }
  96. exports.merge = merge;
  97. function assign_single_source(target, source) {
  98. return Object.keys(source).reduce(function (acc, key) {
  99. acc[key] = source[key];
  100. return acc;
  101. }, target);
  102. }
  103. exports.assign_single_source = assign_single_source;
  104. function decode(str, _, charset) {
  105. const strWithoutPlus = str.replace(/\+/g, ' ');
  106. if (charset === 'iso-8859-1') {
  107. // unescape never throws, no try...catch needed:
  108. return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
  109. }
  110. // utf-8
  111. try {
  112. return decodeURIComponent(strWithoutPlus);
  113. }
  114. catch (e) {
  115. return strWithoutPlus;
  116. }
  117. }
  118. exports.decode = decode;
  119. const limit = 1024;
  120. const encode = (str, _defaultEncoder, charset, _kind, format) => {
  121. // This code was originally written by Brian White for the io.js core querystring library.
  122. // It has been adapted here for stricter adherence to RFC 3986
  123. if (str.length === 0) {
  124. return str;
  125. }
  126. let string = str;
  127. if (typeof str === 'symbol') {
  128. string = Symbol.prototype.toString.call(str);
  129. }
  130. else if (typeof str !== 'string') {
  131. string = String(str);
  132. }
  133. if (charset === 'iso-8859-1') {
  134. return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
  135. return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
  136. });
  137. }
  138. let out = '';
  139. for (let j = 0; j < string.length; j += limit) {
  140. const segment = string.length >= limit ? string.slice(j, j + limit) : string;
  141. const arr = [];
  142. for (let i = 0; i < segment.length; ++i) {
  143. let c = segment.charCodeAt(i);
  144. if (c === 0x2d || // -
  145. c === 0x2e || // .
  146. c === 0x5f || // _
  147. c === 0x7e || // ~
  148. (c >= 0x30 && c <= 0x39) || // 0-9
  149. (c >= 0x41 && c <= 0x5a) || // a-z
  150. (c >= 0x61 && c <= 0x7a) || // A-Z
  151. (format === formats_1.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
  152. ) {
  153. arr[arr.length] = segment.charAt(i);
  154. continue;
  155. }
  156. if (c < 0x80) {
  157. arr[arr.length] = hex_table[c];
  158. continue;
  159. }
  160. if (c < 0x800) {
  161. arr[arr.length] = hex_table[0xc0 | (c >> 6)] + hex_table[0x80 | (c & 0x3f)];
  162. continue;
  163. }
  164. if (c < 0xd800 || c >= 0xe000) {
  165. arr[arr.length] =
  166. hex_table[0xe0 | (c >> 12)] + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
  167. continue;
  168. }
  169. i += 1;
  170. c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
  171. arr[arr.length] =
  172. hex_table[0xf0 | (c >> 18)] +
  173. hex_table[0x80 | ((c >> 12) & 0x3f)] +
  174. hex_table[0x80 | ((c >> 6) & 0x3f)] +
  175. hex_table[0x80 | (c & 0x3f)];
  176. }
  177. out += arr.join('');
  178. }
  179. return out;
  180. };
  181. exports.encode = encode;
  182. function compact(value) {
  183. const queue = [{ obj: { o: value }, prop: 'o' }];
  184. const refs = [];
  185. for (let i = 0; i < queue.length; ++i) {
  186. const item = queue[i];
  187. // @ts-ignore
  188. const obj = item.obj[item.prop];
  189. const keys = Object.keys(obj);
  190. for (let j = 0; j < keys.length; ++j) {
  191. const key = keys[j];
  192. const val = obj[key];
  193. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  194. queue.push({ obj: obj, prop: key });
  195. refs.push(val);
  196. }
  197. }
  198. }
  199. compact_queue(queue);
  200. return value;
  201. }
  202. exports.compact = compact;
  203. function is_regexp(obj) {
  204. return Object.prototype.toString.call(obj) === '[object RegExp]';
  205. }
  206. exports.is_regexp = is_regexp;
  207. function is_buffer(obj) {
  208. if (!obj || typeof obj !== 'object') {
  209. return false;
  210. }
  211. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  212. }
  213. exports.is_buffer = is_buffer;
  214. function combine(a, b) {
  215. return [].concat(a, b);
  216. }
  217. exports.combine = combine;
  218. function maybe_map(val, fn) {
  219. if (is_array(val)) {
  220. const mapped = [];
  221. for (let i = 0; i < val.length; i += 1) {
  222. mapped.push(fn(val[i]));
  223. }
  224. return mapped;
  225. }
  226. return fn(val);
  227. }
  228. exports.maybe_map = maybe_map;
  229. //# sourceMappingURL=utils.js.map