utils.js 7.4 KB

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