query-string 9.1.1 → 9.2.0

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 (4) hide show
  1. package/base.d.ts +25 -11
  2. package/base.js +17 -1
  3. package/package.json +1 -1
  4. package/readme.md +30 -36
package/base.d.ts CHANGED
@@ -178,13 +178,13 @@ export type ParseOptions = {
178
178
  readonly parseFragmentIdentifier?: boolean;
179
179
 
180
180
  /**
181
- Specify a pre-defined schema to be used when parsing values. The types specified will take precedence over options such as: `parseNumber`, `parseBooleans`, and `arrayFormat`.
181
+ Specifies a schema for parsing query values with explicit type declarations. When defined, the types provided here take precedence over general parsing options such as `parseNumbers`, `parseBooleans`, and `arrayFormat`.
182
182
 
183
- Use this feature to override the type of a value. This can be useful when the type is ambiguous such as a phone number (see example 1 and 2).
183
+ Use this option to explicitly define the type of a specific parameter—particularly useful in cases where the type might otherwise be ambiguous (e.g., phone numbers or IDs).
184
184
 
185
- It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value (see example 4).
185
+ You can also provide a custom function to transform the value. The function will receive the raw string and should return the desired parsed result (see Example 4).
186
186
 
187
- NOTE: Array types (`string[]` and `number[]`) will have no effect if `arrayFormat` is set to `none` (see example 5).
187
+ NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`. (See Example 5.)
188
188
 
189
189
  @default {}
190
190
 
@@ -217,7 +217,7 @@ export type ParseOptions = {
217
217
  ```
218
218
 
219
219
  @example
220
- Parse `age` as a number, even when `parseNumber` is false:
220
+ Force `age` to be parsed as a number even when `parseNumbers` is false:
221
221
  ```
222
222
  import queryString from 'query-string';
223
223
 
@@ -226,11 +226,11 @@ export type ParseOptions = {
226
226
  age: 'number',
227
227
  }
228
228
  });
229
- //=> {age: 20, id: '01234', zipcode: '90210 }
229
+ //=> {age: 20, id: '01234', zipcode: '90210'}
230
230
  ```
231
231
 
232
232
  @example
233
- Parse `age` using a custom value parser:
233
+ Use a custom parser function to transform the value of `age`:
234
234
  ```
235
235
  import queryString from 'query-string';
236
236
 
@@ -239,11 +239,11 @@ export type ParseOptions = {
239
239
  age: (value) => value * 2,
240
240
  }
241
241
  });
242
- //=> {age: 40, id: '01234', zipcode: '90210 }
242
+ //=> {age: 40, id: '01234', zipcode: '90210'}
243
243
  ```
244
244
 
245
245
  @example
246
- Array types will have no effect when `arrayFormat` is set to `none`
246
+ Array types are ignored when `arrayFormat` is set to `'none'`:
247
247
  ```
248
248
  queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
249
249
  arrayFormat: 'none',
@@ -256,7 +256,7 @@ export type ParseOptions = {
256
256
  ```
257
257
 
258
258
  @example
259
- Parse a query utilizing all types:
259
+ Parse a query using multiple type definitions:
260
260
  ```
261
261
  import queryString from 'query-string';
262
262
 
@@ -273,10 +273,24 @@ export type ParseOptions = {
273
273
  });
274
274
  //=> {ids: '001,002,003', items: ['1', '2', '3'], price: '22.00', numbers: [1, 2, 3], double: 10, number: 20}
275
275
  ```
276
+
277
+ @example
278
+ Force `flagged` to be parsed as a boolean even when `parseBooleans` is false:
279
+ ```
280
+ queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
281
+ parseBooleans: false,
282
+ types: {
283
+ flagged: 'boolean',
284
+ isOkay: 'boolean',
285
+ },
286
+ });
287
+ //=> {isAdmin: 'true', flagged: true, isOkay: false}
288
+ ```
289
+ Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans.
276
290
  */
277
291
  readonly types?: Record<
278
292
  string,
279
- 'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown)
293
+ 'boolean' | 'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown)
280
294
  >;
281
295
  };
282
296
 
package/base.js CHANGED
@@ -309,14 +309,30 @@ function parseValue(value, options, type) {
309
309
  return type(value);
310
310
  }
311
311
 
312
- if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
312
+ if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
313
313
  return value.toLowerCase() === 'true';
314
314
  }
315
315
 
316
+ if (type === 'boolean' && value !== null && (value.toLowerCase() === '1' || value.toLowerCase() === '0')) {
317
+ return value.toLowerCase() === '1';
318
+ }
319
+
320
+ if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
321
+ return [value];
322
+ }
323
+
324
+ if (type === 'number[]' && options.arrayFormat !== 'none' && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
325
+ return [Number(value)];
326
+ }
327
+
316
328
  if (type === 'number' && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
317
329
  return Number(value);
318
330
  }
319
331
 
332
+ if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
333
+ return value.toLowerCase() === 'true';
334
+ }
335
+
320
336
  if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
321
337
  return Number(value);
322
338
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "9.1.1",
3
+ "version": "9.2.0",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
package/readme.md CHANGED
@@ -2,30 +2,6 @@
2
2
 
3
3
  > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
4
4
 
5
- <br>
6
-
7
- ---
8
-
9
- <div align="center">
10
- <p>
11
- <p>
12
- <sup>
13
- <a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
14
- </sup>
15
- </p>
16
- <sup>Special thanks to:</sup>
17
- <br>
18
- <br>
19
- <a href="https://standardresume.co/tech">
20
- <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="180"/>
21
- </a>
22
- </p>
23
- </div>
24
-
25
- ---
26
-
27
- <br>
28
-
29
5
  ## Install
30
6
 
31
7
  ```sh
@@ -217,15 +193,33 @@ Parse the value as a boolean type instead of string type if it's a boolean.
217
193
  Type: `object`\
218
194
  Default: `{}`
219
195
 
220
- Specify a pre-defined schema to be used when parsing values. The types specified will take precedence over options such as: `parseNumber`, `parseBooleans`, and `arrayFormat`.
221
196
 
222
- Use this feature to override the type of a value. This can be useful when the type is ambiguous such as a phone number.
197
+ Specifies a schema for parsing query values with explicit type declarations. When defined, the types provided here take precedence over general parsing options such as `parseNumbers`, `parseBooleans`, and `arrayFormat`.
198
+
199
+ Use this option to explicitly define the type of a specific parameter—particularly useful in cases where the type might otherwise be ambiguous (e.g., phone numbers or IDs).
200
+
201
+ You can also provide a custom function to transform the value. The function will receive the raw string and should return the desired parsed result.
223
202
 
224
- It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value.
225
203
 
226
204
  Supported Types:
227
205
 
228
- - `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumber` option):
206
+
207
+ - `'boolean'`: Parse `flagged` as a boolean (overriding the `parseBooleans` option):
208
+
209
+ ```js
210
+ queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
211
+ parseBooleans: false,
212
+ types: {
213
+ flagged: 'boolean',
214
+ isOkay: 'boolean',
215
+ },
216
+ });
217
+ //=> {isAdmin: 'true', flagged: true, isOkay: false}
218
+ ```
219
+
220
+ Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans.
221
+
222
+ - `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option):
229
223
 
230
224
  ```js
231
225
  import queryString from 'query-string';
@@ -239,7 +233,7 @@ queryString.parse('?phoneNumber=%2B380951234567&id=1', {
239
233
  //=> {phoneNumber: '+380951234567', id: 1}
240
234
  ```
241
235
 
242
- - `'number'`: Parse `age` as a number (even when `parseNumber` is false):
236
+ - `'number'`: Parse `age` as a number (even when `parseNumbers` is false):
243
237
 
244
238
  ```js
245
239
  import queryString from 'query-string';
@@ -249,16 +243,16 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
249
243
  age: 'number',
250
244
  }
251
245
  });
252
- //=> {age: 20, id: '01234', zipcode: '90210 }
246
+ //=> {age: 20, id: '01234', zipcode: '90210'}
253
247
  ```
254
248
 
255
- - `'string[]'`: Parse `items` as an array of strings (overriding the `parseNumber` option):
249
+ - `'string[]'`: Parse `items` as an array of strings (overriding the `parseNumbers` option):
256
250
 
257
251
  ```js
258
252
  import queryString from 'query-string';
259
253
 
260
254
  queryString.parse('?age=20&items=1%2C2%2C3', {
261
- parseNumber: true,
255
+ parseNumbers: true,
262
256
  types: {
263
257
  items: 'string[]',
264
258
  }
@@ -266,7 +260,7 @@ queryString.parse('?age=20&items=1%2C2%2C3', {
266
260
  //=> {age: 20, items: ['1', '2', '3']}
267
261
  ```
268
262
 
269
- - `'number[]'`: Parse `items` as an array of numbers (even when `parseNumber` is false):
263
+ - `'number[]'`: Parse `items` as an array of numbers (even when `parseNumbers` is false):
270
264
 
271
265
  ```js
272
266
  import queryString from 'query-string';
@@ -289,10 +283,10 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
289
283
  age: (value) => value * 2,
290
284
  }
291
285
  });
292
- //=> {age: 40, id: '01234', zipcode: '90210 }
286
+ //=> {age: 40, id: '01234', zipcode: '90210'}
293
287
  ```
294
288
 
295
- NOTE: Array types (`string[]` and `number[]`) will have no effect if `arrayFormat` is set to `none`.
289
+ NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`.
296
290
 
297
291
  ```js
298
292
  queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
@@ -315,7 +309,7 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
315
309
  age: (value) => value * 2,
316
310
  }
317
311
  });
318
- //=> {age: 40, id: '01234', zipcode: '90210 }
312
+ //=> {age: 40, id: '01234', zipcode: '90210'}
319
313
  ```
320
314
 
321
315
  Parse the value as a boolean type instead of string type if it's a boolean.