query-string 6.13.8 → 9.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. package/base.d.ts +775 -0
  2. package/base.js +578 -0
  3. package/index.d.ts +16 -411
  4. package/index.js +2 -379
  5. package/license +1 -1
  6. package/package.json +26 -12
  7. package/readme.md +366 -56
package/index.js CHANGED
@@ -1,380 +1,3 @@
1
- 'use strict';
2
- const strictUriEncode = require('strict-uri-encode');
3
- const decodeComponent = require('decode-uri-component');
4
- const splitOnFirst = require('split-on-first');
1
+ import * as queryString from './base.js';
5
2
 
6
- const isNullOrUndefined = value => value === null || value === undefined;
7
-
8
- function encoderForArrayFormat(options) {
9
- switch (options.arrayFormat) {
10
- case 'index':
11
- return key => (result, value) => {
12
- const index = result.length;
13
-
14
- if (
15
- value === undefined ||
16
- (options.skipNull && value === null) ||
17
- (options.skipEmptyString && value === '')
18
- ) {
19
- return result;
20
- }
21
-
22
- if (value === null) {
23
- return [...result, [encode(key, options), '[', index, ']'].join('')];
24
- }
25
-
26
- return [
27
- ...result,
28
- [encode(key, options), '[', encode(index, options), ']=', encode(value, options)].join('')
29
- ];
30
- };
31
-
32
- case 'bracket':
33
- return key => (result, value) => {
34
- if (
35
- value === undefined ||
36
- (options.skipNull && value === null) ||
37
- (options.skipEmptyString && value === '')
38
- ) {
39
- return result;
40
- }
41
-
42
- if (value === null) {
43
- return [...result, [encode(key, options), '[]'].join('')];
44
- }
45
-
46
- return [...result, [encode(key, options), '[]=', encode(value, options)].join('')];
47
- };
48
-
49
- case 'comma':
50
- case 'separator':
51
- return key => (result, value) => {
52
- if (value === null || value === undefined || value.length === 0) {
53
- return result;
54
- }
55
-
56
- if (result.length === 0) {
57
- return [[encode(key, options), '=', encode(value, options)].join('')];
58
- }
59
-
60
- return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
61
- };
62
-
63
- default:
64
- return key => (result, value) => {
65
- if (
66
- value === undefined ||
67
- (options.skipNull && value === null) ||
68
- (options.skipEmptyString && value === '')
69
- ) {
70
- return result;
71
- }
72
-
73
- if (value === null) {
74
- return [...result, encode(key, options)];
75
- }
76
-
77
- return [...result, [encode(key, options), '=', encode(value, options)].join('')];
78
- };
79
- }
80
- }
81
-
82
- function parserForArrayFormat(options) {
83
- let result;
84
-
85
- switch (options.arrayFormat) {
86
- case 'index':
87
- return (key, value, accumulator) => {
88
- result = /\[(\d*)\]$/.exec(key);
89
-
90
- key = key.replace(/\[\d*\]$/, '');
91
-
92
- if (!result) {
93
- accumulator[key] = value;
94
- return;
95
- }
96
-
97
- if (accumulator[key] === undefined) {
98
- accumulator[key] = {};
99
- }
100
-
101
- accumulator[key][result[1]] = value;
102
- };
103
-
104
- case 'bracket':
105
- return (key, value, accumulator) => {
106
- result = /(\[\])$/.exec(key);
107
- key = key.replace(/\[\]$/, '');
108
-
109
- if (!result) {
110
- accumulator[key] = value;
111
- return;
112
- }
113
-
114
- if (accumulator[key] === undefined) {
115
- accumulator[key] = [value];
116
- return;
117
- }
118
-
119
- accumulator[key] = [].concat(accumulator[key], value);
120
- };
121
-
122
- case 'comma':
123
- case 'separator':
124
- return (key, value, accumulator) => {
125
- const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
126
- const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
127
- value = isEncodedArray ? decode(value, options) : value;
128
- const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
129
- accumulator[key] = newValue;
130
- };
131
-
132
- default:
133
- return (key, value, accumulator) => {
134
- if (accumulator[key] === undefined) {
135
- accumulator[key] = value;
136
- return;
137
- }
138
-
139
- accumulator[key] = [].concat(accumulator[key], value);
140
- };
141
- }
142
- }
143
-
144
- function validateArrayFormatSeparator(value) {
145
- if (typeof value !== 'string' || value.length !== 1) {
146
- throw new TypeError('arrayFormatSeparator must be single character string');
147
- }
148
- }
149
-
150
- function encode(value, options) {
151
- if (options.encode) {
152
- return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
153
- }
154
-
155
- return value;
156
- }
157
-
158
- function decode(value, options) {
159
- if (options.decode) {
160
- return decodeComponent(value);
161
- }
162
-
163
- return value;
164
- }
165
-
166
- function keysSorter(input) {
167
- if (Array.isArray(input)) {
168
- return input.sort();
169
- }
170
-
171
- if (typeof input === 'object') {
172
- return keysSorter(Object.keys(input))
173
- .sort((a, b) => Number(a) - Number(b))
174
- .map(key => input[key]);
175
- }
176
-
177
- return input;
178
- }
179
-
180
- function removeHash(input) {
181
- const hashStart = input.indexOf('#');
182
- if (hashStart !== -1) {
183
- input = input.slice(0, hashStart);
184
- }
185
-
186
- return input;
187
- }
188
-
189
- function getHash(url) {
190
- let hash = '';
191
- const hashStart = url.indexOf('#');
192
- if (hashStart !== -1) {
193
- hash = url.slice(hashStart);
194
- }
195
-
196
- return hash;
197
- }
198
-
199
- function extract(input) {
200
- input = removeHash(input);
201
- const queryStart = input.indexOf('?');
202
- if (queryStart === -1) {
203
- return '';
204
- }
205
-
206
- return input.slice(queryStart + 1);
207
- }
208
-
209
- function parseValue(value, options) {
210
- if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
211
- value = Number(value);
212
- } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
213
- value = value.toLowerCase() === 'true';
214
- }
215
-
216
- return value;
217
- }
218
-
219
- function parse(query, options) {
220
- options = Object.assign({
221
- decode: true,
222
- sort: true,
223
- arrayFormat: 'none',
224
- arrayFormatSeparator: ',',
225
- parseNumbers: false,
226
- parseBooleans: false
227
- }, options);
228
-
229
- validateArrayFormatSeparator(options.arrayFormatSeparator);
230
-
231
- const formatter = parserForArrayFormat(options);
232
-
233
- // Create an object with no prototype
234
- const ret = Object.create(null);
235
-
236
- if (typeof query !== 'string') {
237
- return ret;
238
- }
239
-
240
- query = query.trim().replace(/^[?#&]/, '');
241
-
242
- if (!query) {
243
- return ret;
244
- }
245
-
246
- for (const param of query.split('&')) {
247
- let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
248
-
249
- // Missing `=` should be `null`:
250
- // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
251
- value = value === undefined ? null : ['comma', 'separator'].includes(options.arrayFormat) ? value : decode(value, options);
252
- formatter(decode(key, options), value, ret);
253
- }
254
-
255
- for (const key of Object.keys(ret)) {
256
- const value = ret[key];
257
- if (typeof value === 'object' && value !== null) {
258
- for (const k of Object.keys(value)) {
259
- value[k] = parseValue(value[k], options);
260
- }
261
- } else {
262
- ret[key] = parseValue(value, options);
263
- }
264
- }
265
-
266
- if (options.sort === false) {
267
- return ret;
268
- }
269
-
270
- return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
271
- const value = ret[key];
272
- if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
273
- // Sort object keys, not values
274
- result[key] = keysSorter(value);
275
- } else {
276
- result[key] = value;
277
- }
278
-
279
- return result;
280
- }, Object.create(null));
281
- }
282
-
283
- exports.extract = extract;
284
- exports.parse = parse;
285
-
286
- exports.stringify = (object, options) => {
287
- if (!object) {
288
- return '';
289
- }
290
-
291
- options = Object.assign({
292
- encode: true,
293
- strict: true,
294
- arrayFormat: 'none',
295
- arrayFormatSeparator: ','
296
- }, options);
297
-
298
- validateArrayFormatSeparator(options.arrayFormatSeparator);
299
-
300
- const shouldFilter = key => (
301
- (options.skipNull && isNullOrUndefined(object[key])) ||
302
- (options.skipEmptyString && object[key] === '')
303
- );
304
-
305
- const formatter = encoderForArrayFormat(options);
306
-
307
- const objectCopy = {};
308
-
309
- for (const key of Object.keys(object)) {
310
- if (!shouldFilter(key)) {
311
- objectCopy[key] = object[key];
312
- }
313
- }
314
-
315
- const keys = Object.keys(objectCopy);
316
-
317
- if (options.sort !== false) {
318
- keys.sort(options.sort);
319
- }
320
-
321
- return keys.map(key => {
322
- const value = object[key];
323
-
324
- if (value === undefined) {
325
- return '';
326
- }
327
-
328
- if (value === null) {
329
- return encode(key, options);
330
- }
331
-
332
- if (Array.isArray(value)) {
333
- return value
334
- .reduce(formatter(key), [])
335
- .join('&');
336
- }
337
-
338
- return encode(key, options) + '=' + encode(value, options);
339
- }).filter(x => x.length > 0).join('&');
340
- };
341
-
342
- exports.parseUrl = (url, options) => {
343
- options = Object.assign({
344
- decode: true
345
- }, options);
346
-
347
- const [url_, hash] = splitOnFirst(url, '#');
348
-
349
- return Object.assign(
350
- {
351
- url: url_.split('?')[0] || '',
352
- query: parse(extract(url), options)
353
- },
354
- options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}
355
- );
356
- };
357
-
358
- exports.stringifyUrl = (object, options) => {
359
- options = Object.assign({
360
- encode: true,
361
- strict: true
362
- }, options);
363
-
364
- const url = removeHash(object.url).split('?')[0] || '';
365
- const queryFromUrl = exports.extract(object.url);
366
- const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false});
367
-
368
- const query = Object.assign(parsedQueryFromUrl, object.query);
369
- let queryString = exports.stringify(query, options);
370
- if (queryString) {
371
- queryString = `?${queryString}`;
372
- }
373
-
374
- let hash = getHash(object.url);
375
- if (object.fragmentIdentifier) {
376
- hash = `#${encode(object.fragmentIdentifier, options)}`;
377
- }
378
-
379
- return `${url}${queryString}${hash}`;
380
- };
3
+ export default queryString;
package/license CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (http://sindresorhus.com)
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.13.8",
3
+ "version": "9.3.1",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
@@ -10,8 +10,14 @@
10
10
  "email": "sindresorhus@gmail.com",
11
11
  "url": "https://sindresorhus.com"
12
12
  },
13
+ "type": "module",
14
+ "exports": {
15
+ "types": "./index.d.ts",
16
+ "default": "./index.js"
17
+ },
18
+ "sideEffects": false,
13
19
  "engines": {
14
- "node": ">=6"
20
+ "node": ">=18"
15
21
  },
16
22
  "scripts": {
17
23
  "benchmark": "node benchmark.js",
@@ -19,7 +25,9 @@
19
25
  },
20
26
  "files": [
21
27
  "index.js",
22
- "index.d.ts"
28
+ "index.d.ts",
29
+ "base.js",
30
+ "base.d.ts"
23
31
  ],
24
32
  "keywords": [
25
33
  "browser",
@@ -34,19 +42,25 @@
34
42
  "stringify",
35
43
  "encode",
36
44
  "decode",
37
- "searchparams"
45
+ "searchparams",
46
+ "filter"
38
47
  ],
39
48
  "dependencies": {
40
- "decode-uri-component": "^0.2.0",
41
- "split-on-first": "^1.0.0",
42
- "strict-uri-encode": "^2.0.0"
49
+ "decode-uri-component": "^0.4.1",
50
+ "filter-obj": "^5.1.0",
51
+ "split-on-first": "^3.0.0"
43
52
  },
44
53
  "devDependencies": {
45
- "ava": "^1.4.1",
54
+ "ava": "^6.1.1",
46
55
  "benchmark": "^2.1.4",
47
- "deep-equal": "^1.0.1",
48
- "fast-check": "^1.5.0",
49
- "tsd": "^0.7.3",
50
- "xo": "^0.24.0"
56
+ "deep-equal": "^2.2.3",
57
+ "fast-check": "^3.15.1",
58
+ "tsd": "^0.30.7",
59
+ "xo": "^0.57.0"
60
+ },
61
+ "tsd": {
62
+ "compilerOptions": {
63
+ "module": "node16"
64
+ }
51
65
  }
52
66
  }