stringify.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { encode, is_buffer, maybe_map } from "./utils.mjs";
  2. import { default_format, formatters } from "./formats.mjs";
  3. const has = Object.prototype.hasOwnProperty;
  4. const array_prefix_generators = {
  5. brackets(prefix) {
  6. return String(prefix) + '[]';
  7. },
  8. comma: 'comma',
  9. indices(prefix, key) {
  10. return String(prefix) + '[' + key + ']';
  11. },
  12. repeat(prefix) {
  13. return String(prefix);
  14. },
  15. };
  16. const is_array = Array.isArray;
  17. const push = Array.prototype.push;
  18. const push_to_array = function (arr, value_or_array) {
  19. push.apply(arr, is_array(value_or_array) ? value_or_array : [value_or_array]);
  20. };
  21. const to_ISO = Date.prototype.toISOString;
  22. const defaults = {
  23. addQueryPrefix: false,
  24. allowDots: false,
  25. allowEmptyArrays: false,
  26. arrayFormat: 'indices',
  27. charset: 'utf-8',
  28. charsetSentinel: false,
  29. delimiter: '&',
  30. encode: true,
  31. encodeDotInKeys: false,
  32. encoder: encode,
  33. encodeValuesOnly: false,
  34. format: default_format,
  35. formatter: formatters[default_format],
  36. /** @deprecated */
  37. indices: false,
  38. serializeDate(date) {
  39. return to_ISO.call(date);
  40. },
  41. skipNulls: false,
  42. strictNullHandling: false,
  43. };
  44. function is_non_nullish_primitive(v) {
  45. return (typeof v === 'string' ||
  46. typeof v === 'number' ||
  47. typeof v === 'boolean' ||
  48. typeof v === 'symbol' ||
  49. typeof v === 'bigint');
  50. }
  51. const sentinel = {};
  52. function inner_stringify(object, prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, encoder, filter, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, sideChannel) {
  53. let obj = object;
  54. let tmp_sc = sideChannel;
  55. let step = 0;
  56. let find_flag = false;
  57. while ((tmp_sc = tmp_sc.get(sentinel)) !== void undefined && !find_flag) {
  58. // Where object last appeared in the ref tree
  59. const pos = tmp_sc.get(object);
  60. step += 1;
  61. if (typeof pos !== 'undefined') {
  62. if (pos === step) {
  63. throw new RangeError('Cyclic object value');
  64. }
  65. else {
  66. find_flag = true; // Break while
  67. }
  68. }
  69. if (typeof tmp_sc.get(sentinel) === 'undefined') {
  70. step = 0;
  71. }
  72. }
  73. if (typeof filter === 'function') {
  74. obj = filter(prefix, obj);
  75. }
  76. else if (obj instanceof Date) {
  77. obj = serializeDate?.(obj);
  78. }
  79. else if (generateArrayPrefix === 'comma' && is_array(obj)) {
  80. obj = maybe_map(obj, function (value) {
  81. if (value instanceof Date) {
  82. return serializeDate?.(value);
  83. }
  84. return value;
  85. });
  86. }
  87. if (obj === null) {
  88. if (strictNullHandling) {
  89. return encoder && !encodeValuesOnly ?
  90. // @ts-expect-error
  91. encoder(prefix, defaults.encoder, charset, 'key', format)
  92. : prefix;
  93. }
  94. obj = '';
  95. }
  96. if (is_non_nullish_primitive(obj) || is_buffer(obj)) {
  97. if (encoder) {
  98. const key_value = encodeValuesOnly ? prefix
  99. // @ts-expect-error
  100. : encoder(prefix, defaults.encoder, charset, 'key', format);
  101. return [
  102. formatter?.(key_value) +
  103. '=' +
  104. // @ts-expect-error
  105. formatter?.(encoder(obj, defaults.encoder, charset, 'value', format)),
  106. ];
  107. }
  108. return [formatter?.(prefix) + '=' + formatter?.(String(obj))];
  109. }
  110. const values = [];
  111. if (typeof obj === 'undefined') {
  112. return values;
  113. }
  114. let obj_keys;
  115. if (generateArrayPrefix === 'comma' && is_array(obj)) {
  116. // we need to join elements in
  117. if (encodeValuesOnly && encoder) {
  118. // @ts-expect-error values only
  119. obj = maybe_map(obj, encoder);
  120. }
  121. obj_keys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
  122. }
  123. else if (is_array(filter)) {
  124. obj_keys = filter;
  125. }
  126. else {
  127. const keys = Object.keys(obj);
  128. obj_keys = sort ? keys.sort(sort) : keys;
  129. }
  130. const encoded_prefix = encodeDotInKeys ? String(prefix).replace(/\./g, '%2E') : String(prefix);
  131. const adjusted_prefix = commaRoundTrip && is_array(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;
  132. if (allowEmptyArrays && is_array(obj) && obj.length === 0) {
  133. return adjusted_prefix + '[]';
  134. }
  135. for (let j = 0; j < obj_keys.length; ++j) {
  136. const key = obj_keys[j];
  137. const value =
  138. // @ts-ignore
  139. typeof key === 'object' && typeof key.value !== 'undefined' ? key.value : obj[key];
  140. if (skipNulls && value === null) {
  141. continue;
  142. }
  143. // @ts-ignore
  144. const encoded_key = allowDots && encodeDotInKeys ? key.replace(/\./g, '%2E') : key;
  145. const key_prefix = is_array(obj) ?
  146. typeof generateArrayPrefix === 'function' ?
  147. generateArrayPrefix(adjusted_prefix, encoded_key)
  148. : adjusted_prefix
  149. : adjusted_prefix + (allowDots ? '.' + encoded_key : '[' + encoded_key + ']');
  150. sideChannel.set(object, step);
  151. const valueSideChannel = new WeakMap();
  152. valueSideChannel.set(sentinel, sideChannel);
  153. push_to_array(values, inner_stringify(value, key_prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys,
  154. // @ts-ignore
  155. generateArrayPrefix === 'comma' && encodeValuesOnly && is_array(obj) ? null : encoder, filter, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, valueSideChannel));
  156. }
  157. return values;
  158. }
  159. function normalize_stringify_options(opts = defaults) {
  160. if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
  161. throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
  162. }
  163. if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {
  164. throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');
  165. }
  166. if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
  167. throw new TypeError('Encoder has to be a function.');
  168. }
  169. const charset = opts.charset || defaults.charset;
  170. if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
  171. throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
  172. }
  173. let format = default_format;
  174. if (typeof opts.format !== 'undefined') {
  175. if (!has.call(formatters, opts.format)) {
  176. throw new TypeError('Unknown format option provided.');
  177. }
  178. format = opts.format;
  179. }
  180. const formatter = formatters[format];
  181. let filter = defaults.filter;
  182. if (typeof opts.filter === 'function' || is_array(opts.filter)) {
  183. filter = opts.filter;
  184. }
  185. let arrayFormat;
  186. if (opts.arrayFormat && opts.arrayFormat in array_prefix_generators) {
  187. arrayFormat = opts.arrayFormat;
  188. }
  189. else if ('indices' in opts) {
  190. arrayFormat = opts.indices ? 'indices' : 'repeat';
  191. }
  192. else {
  193. arrayFormat = defaults.arrayFormat;
  194. }
  195. if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
  196. throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
  197. }
  198. const allowDots = typeof opts.allowDots === 'undefined' ?
  199. !!opts.encodeDotInKeys === true ?
  200. true
  201. : defaults.allowDots
  202. : !!opts.allowDots;
  203. return {
  204. addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
  205. // @ts-ignore
  206. allowDots: allowDots,
  207. allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
  208. arrayFormat: arrayFormat,
  209. charset: charset,
  210. charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
  211. commaRoundTrip: !!opts.commaRoundTrip,
  212. delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
  213. encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
  214. encodeDotInKeys: typeof opts.encodeDotInKeys === 'boolean' ? opts.encodeDotInKeys : defaults.encodeDotInKeys,
  215. encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
  216. encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
  217. filter: filter,
  218. format: format,
  219. formatter: formatter,
  220. serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
  221. skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
  222. // @ts-ignore
  223. sort: typeof opts.sort === 'function' ? opts.sort : null,
  224. strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
  225. };
  226. }
  227. export function stringify(object, opts = {}) {
  228. let obj = object;
  229. const options = normalize_stringify_options(opts);
  230. let obj_keys;
  231. let filter;
  232. if (typeof options.filter === 'function') {
  233. filter = options.filter;
  234. obj = filter('', obj);
  235. }
  236. else if (is_array(options.filter)) {
  237. filter = options.filter;
  238. obj_keys = filter;
  239. }
  240. const keys = [];
  241. if (typeof obj !== 'object' || obj === null) {
  242. return '';
  243. }
  244. const generateArrayPrefix = array_prefix_generators[options.arrayFormat];
  245. const commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;
  246. if (!obj_keys) {
  247. obj_keys = Object.keys(obj);
  248. }
  249. if (options.sort) {
  250. obj_keys.sort(options.sort);
  251. }
  252. const sideChannel = new WeakMap();
  253. for (let i = 0; i < obj_keys.length; ++i) {
  254. const key = obj_keys[i];
  255. if (options.skipNulls && obj[key] === null) {
  256. continue;
  257. }
  258. push_to_array(keys, inner_stringify(obj[key], key,
  259. // @ts-expect-error
  260. generateArrayPrefix, commaRoundTrip, options.allowEmptyArrays, options.strictNullHandling, options.skipNulls, options.encodeDotInKeys, options.encode ? options.encoder : null, options.filter, options.sort, options.allowDots, options.serializeDate, options.format, options.formatter, options.encodeValuesOnly, options.charset, sideChannel));
  261. }
  262. const joined = keys.join(options.delimiter);
  263. let prefix = options.addQueryPrefix === true ? '?' : '';
  264. if (options.charsetSentinel) {
  265. if (options.charset === 'iso-8859-1') {
  266. // encodeURIComponent('&#10003;'), the "numeric entity" representation of a checkmark
  267. prefix += 'utf8=%26%2310003%3B&';
  268. }
  269. else {
  270. // encodeURIComponent('✓')
  271. prefix += 'utf8=%E2%9C%93&';
  272. }
  273. }
  274. return joined.length > 0 ? prefix + joined : '';
  275. }
  276. //# sourceMappingURL=stringify.mjs.map