query-string 9.1.2 → 9.2.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 (4) hide show
  1. package/base.d.ts +25 -11
  2. package/base.js +9 -1
  3. package/package.json +1 -1
  4. package/readme.md +25 -7
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,6 +309,14 @@ function parseValue(value, options, type) {
309
309
  return type(value);
310
310
  }
311
311
 
312
+ if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
313
+ return value.toLowerCase() === 'true';
314
+ }
315
+
316
+ if (type === 'boolean' && value !== null && (value.toLowerCase() === '1' || value.toLowerCase() === '0')) {
317
+ return value.toLowerCase() === '1';
318
+ }
319
+
312
320
  if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
313
321
  return [value];
314
322
  }
@@ -508,7 +516,7 @@ export function stringifyUrl(object, options) {
508
516
  const queryFromUrl = extract(object.url);
509
517
 
510
518
  const query = {
511
- ...parse(queryFromUrl, {sort: false}),
519
+ ...parse(queryFromUrl, {sort: false, ...options}),
512
520
  ...object.query,
513
521
  };
514
522
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "9.1.2",
3
+ "version": "9.2.1",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
package/readme.md CHANGED
@@ -193,14 +193,32 @@ Parse the value as a boolean type instead of string type if it's a boolean.
193
193
  Type: `object`\
194
194
  Default: `{}`
195
195
 
196
- Specify a pre-defined schema to be used when parsing values. The types specified will take precedence over options such as: `parseNumbers`, `parseBooleans`, and `arrayFormat`.
197
196
 
198
- 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.
199
202
 
200
- It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value.
201
203
 
202
204
  Supported Types:
203
205
 
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
+
204
222
  - `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option):
205
223
 
206
224
  ```js
@@ -225,7 +243,7 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
225
243
  age: 'number',
226
244
  }
227
245
  });
228
- //=> {age: 20, id: '01234', zipcode: '90210 }
246
+ //=> {age: 20, id: '01234', zipcode: '90210'}
229
247
  ```
230
248
 
231
249
  - `'string[]'`: Parse `items` as an array of strings (overriding the `parseNumbers` option):
@@ -265,10 +283,10 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
265
283
  age: (value) => value * 2,
266
284
  }
267
285
  });
268
- //=> {age: 40, id: '01234', zipcode: '90210 }
286
+ //=> {age: 40, id: '01234', zipcode: '90210'}
269
287
  ```
270
288
 
271
- 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'`.
272
290
 
273
291
  ```js
274
292
  queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
@@ -291,7 +309,7 @@ queryString.parse('?age=20&id=01234&zipcode=90210', {
291
309
  age: (value) => value * 2,
292
310
  }
293
311
  });
294
- //=> {age: 40, id: '01234', zipcode: '90210 }
312
+ //=> {age: 40, id: '01234', zipcode: '90210'}
295
313
  ```
296
314
 
297
315
  Parse the value as a boolean type instead of string type if it's a boolean.