query-string 6.13.4 → 6.13.8
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.
- package/index.d.ts +8 -3
- package/index.js +20 -18
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -339,18 +339,23 @@ export interface StringifyOptions {
|
|
|
339
339
|
readonly skipEmptyString?: boolean;
|
|
340
340
|
}
|
|
341
341
|
|
|
342
|
-
export type Stringifiable = string | boolean | number;
|
|
342
|
+
export type Stringifiable = string | boolean | number | null | undefined;
|
|
343
343
|
|
|
344
344
|
export type StringifiableRecord = Record<
|
|
345
345
|
string,
|
|
346
|
-
Stringifiable | Stringifiable[]
|
|
346
|
+
Stringifiable | readonly Stringifiable[]
|
|
347
347
|
>;
|
|
348
348
|
|
|
349
349
|
/**
|
|
350
350
|
Stringify an object into a query string and sort the keys.
|
|
351
351
|
*/
|
|
352
352
|
export function stringify(
|
|
353
|
-
|
|
353
|
+
// TODO: Use the below instead when the following TS issues are fixed:
|
|
354
|
+
// - https://github.com/microsoft/TypeScript/issues/15300
|
|
355
|
+
// - https://github.com/microsoft/TypeScript/issues/42021
|
|
356
|
+
// Context: https://github.com/sindresorhus/query-string/issues/298
|
|
357
|
+
// object: StringifiableRecord,
|
|
358
|
+
object: Record<string, any>,
|
|
354
359
|
options?: StringifyOptions
|
|
355
360
|
): string;
|
|
356
361
|
|
package/index.js
CHANGED
|
@@ -122,8 +122,10 @@ function parserForArrayFormat(options) {
|
|
|
122
122
|
case 'comma':
|
|
123
123
|
case 'separator':
|
|
124
124
|
return (key, value, accumulator) => {
|
|
125
|
-
const isArray = typeof value === 'string' && value.
|
|
126
|
-
const
|
|
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);
|
|
127
129
|
accumulator[key] = newValue;
|
|
128
130
|
};
|
|
129
131
|
|
|
@@ -214,7 +216,7 @@ function parseValue(value, options) {
|
|
|
214
216
|
return value;
|
|
215
217
|
}
|
|
216
218
|
|
|
217
|
-
function parse(
|
|
219
|
+
function parse(query, options) {
|
|
218
220
|
options = Object.assign({
|
|
219
221
|
decode: true,
|
|
220
222
|
sort: true,
|
|
@@ -231,17 +233,17 @@ function parse(input, options) {
|
|
|
231
233
|
// Create an object with no prototype
|
|
232
234
|
const ret = Object.create(null);
|
|
233
235
|
|
|
234
|
-
if (typeof
|
|
236
|
+
if (typeof query !== 'string') {
|
|
235
237
|
return ret;
|
|
236
238
|
}
|
|
237
239
|
|
|
238
|
-
|
|
240
|
+
query = query.trim().replace(/^[?#&]/, '');
|
|
239
241
|
|
|
240
|
-
if (!
|
|
242
|
+
if (!query) {
|
|
241
243
|
return ret;
|
|
242
244
|
}
|
|
243
245
|
|
|
244
|
-
for (const param of
|
|
246
|
+
for (const param of query.split('&')) {
|
|
245
247
|
let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
|
|
246
248
|
|
|
247
249
|
// Missing `=` should be `null`:
|
|
@@ -337,41 +339,41 @@ exports.stringify = (object, options) => {
|
|
|
337
339
|
}).filter(x => x.length > 0).join('&');
|
|
338
340
|
};
|
|
339
341
|
|
|
340
|
-
exports.parseUrl = (
|
|
342
|
+
exports.parseUrl = (url, options) => {
|
|
341
343
|
options = Object.assign({
|
|
342
344
|
decode: true
|
|
343
345
|
}, options);
|
|
344
346
|
|
|
345
|
-
const [
|
|
347
|
+
const [url_, hash] = splitOnFirst(url, '#');
|
|
346
348
|
|
|
347
349
|
return Object.assign(
|
|
348
350
|
{
|
|
349
|
-
url:
|
|
350
|
-
query: parse(extract(
|
|
351
|
+
url: url_.split('?')[0] || '',
|
|
352
|
+
query: parse(extract(url), options)
|
|
351
353
|
},
|
|
352
354
|
options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}
|
|
353
355
|
);
|
|
354
356
|
};
|
|
355
357
|
|
|
356
|
-
exports.stringifyUrl = (
|
|
358
|
+
exports.stringifyUrl = (object, options) => {
|
|
357
359
|
options = Object.assign({
|
|
358
360
|
encode: true,
|
|
359
361
|
strict: true
|
|
360
362
|
}, options);
|
|
361
363
|
|
|
362
|
-
const url = removeHash(
|
|
363
|
-
const queryFromUrl = exports.extract(
|
|
364
|
+
const url = removeHash(object.url).split('?')[0] || '';
|
|
365
|
+
const queryFromUrl = exports.extract(object.url);
|
|
364
366
|
const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false});
|
|
365
367
|
|
|
366
|
-
const query = Object.assign(parsedQueryFromUrl,
|
|
368
|
+
const query = Object.assign(parsedQueryFromUrl, object.query);
|
|
367
369
|
let queryString = exports.stringify(query, options);
|
|
368
370
|
if (queryString) {
|
|
369
371
|
queryString = `?${queryString}`;
|
|
370
372
|
}
|
|
371
373
|
|
|
372
|
-
let hash = getHash(
|
|
373
|
-
if (
|
|
374
|
-
hash = `#${encode(
|
|
374
|
+
let hash = getHash(object.url);
|
|
375
|
+
if (object.fragmentIdentifier) {
|
|
376
|
+
hash = `#${encode(object.fragmentIdentifier, options)}`;
|
|
375
377
|
}
|
|
376
378
|
|
|
377
379
|
return `${url}${queryString}${hash}`;
|