![Strapi](https://sindresorhus.com/assets/thanks/strapi-logo-white-bg.png)
From d2c3596a24ff1b4a82bf9351a7e0ab236f614ce8 Mon Sep 17 00:00:00 2001
From: Victor Zhuravlev <11217819+viczhuravlev@users.noreply.github.com>
Date: Thu, 1 Dec 2022 20:13:03 +0300
Subject: [PATCH 22/50] Update dependency (#353)
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index fd0b303..d529167 100644
--- a/package.json
+++ b/package.json
@@ -38,7 +38,7 @@
"filter"
],
"dependencies": {
- "decode-uri-component": "^0.2.0",
+ "decode-uri-component": "^0.2.1",
"filter-obj": "^1.1.0",
"split-on-first": "^1.0.0",
"strict-uri-encode": "^2.0.0"
From 88129068bc733bcb16ba0422468e7fff74dc712e Mon Sep 17 00:00:00 2001
From: Sindre Sorhus
Date: Fri, 2 Dec 2022 00:14:40 +0700
Subject: [PATCH 23/50] 7.1.2
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index d529167..c04181d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "query-string",
- "version": "7.1.1",
+ "version": "7.1.2",
"description": "Parse and stringify URL query strings",
"license": "MIT",
"repository": "sindresorhus/query-string",
From 7537410e80d04c9a4c46958575ea4a834fb2cf33 Mon Sep 17 00:00:00 2001
From: Sindre Sorhus
Date: Fri, 2 Dec 2022 00:17:58 +0700
Subject: [PATCH 24/50] Fix CI
---
.github/workflows/main.yml | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index b85fc2a..ca8ed6f 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -14,10 +14,9 @@ jobs:
- 12
- 10
- 8
- - 6
steps:
- - uses: actions/checkout@v2
- - uses: actions/setup-node@v1
+ - uses: actions/checkout@v3
+ - uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
From e60e797f884f8c08389c68c9cff3efc09e2f59e2 Mon Sep 17 00:00:00 2001
From: krudos
Date: Fri, 2 Dec 2022 18:42:14 +0100
Subject: [PATCH 25/50] Update dependencies (#354)
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index c04181d..4393feb 100644
--- a/package.json
+++ b/package.json
@@ -38,7 +38,7 @@
"filter"
],
"dependencies": {
- "decode-uri-component": "^0.2.1",
+ "decode-uri-component": "^0.2.2",
"filter-obj": "^1.1.0",
"split-on-first": "^1.0.0",
"strict-uri-encode": "^2.0.0"
From 5beef41d37f18916dc15bded8fac803a33153aa2 Mon Sep 17 00:00:00 2001
From: Sindre Sorhus
Date: Sat, 3 Dec 2022 00:43:23 +0700
Subject: [PATCH 26/50] 7.1.3
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 4393feb..d87d6df 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "query-string",
- "version": "7.1.2",
+ "version": "7.1.3",
"description": "Parse and stringify URL query strings",
"license": "MIT",
"repository": "sindresorhus/query-string",
From 16a7b8f37f8b88f1785b68efca57e55ae96904e2 Mon Sep 17 00:00:00 2001
From: HanBinKim <37659866+hanbin9775@users.noreply.github.com>
Date: Mon, 12 Dec 2022 23:18:54 +0900
Subject: [PATCH 27/50] Fix encoding of `fragmentIdentifier` (#355)
---
index.js | 4 +++-
test/stringify-url.js | 1 +
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/index.js b/index.js
index d45e67d..129ea26 100644
--- a/index.js
+++ b/index.js
@@ -455,7 +455,9 @@ exports.stringifyUrl = (object, options) => {
let hash = getHash(object.url);
if (object.fragmentIdentifier) {
- hash = `#${options[encodeFragmentIdentifier] ? encode(object.fragmentIdentifier, options) : object.fragmentIdentifier}`;
+ const urlObjectForFragmentEncode = new URL(url);
+ urlObjectForFragmentEncode.hash = object.fragmentIdentifier;
+ hash = options[encodeFragmentIdentifier] ? urlObjectForFragmentEncode.hash : `#${object.fragmentIdentifier}`;
}
return `${url}${queryString}${hash}`;
diff --git a/test/stringify-url.js b/test/stringify-url.js
index 2b3c372..9b6ff43 100644
--- a/test/stringify-url.js
+++ b/test/stringify-url.js
@@ -26,6 +26,7 @@ test('stringify URL with fragment identifier', t => {
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#abc', query: {}, fragmentIdentifier: 'top'}), 'https://foo.bar/#top');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {}}), 'https://foo.bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {}, fragmentIdentifier: 'foo bar'}), 'https://foo.bar#foo%20bar');
+ t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}, fragmentIdentifier: '/foo/bar'}), 'https://foo.bar/#/foo/bar');
});
test('skipEmptyString:: stringify URL with a query string', t => {
From c2ab7cd102fbc96d374c6a55deea20f1bf2929c4 Mon Sep 17 00:00:00 2001
From: Sindre Sorhus
Date: Mon, 12 Dec 2022 15:25:00 +0100
Subject: [PATCH 28/50] Require Node.js 14 and move to ESM
---
.github/workflows/main.yml | 5 +-
base.d.ts | 561 ++++++++++++++++++++++++++++++++++++
base.js | 517 +++++++++++++++++++++++++++++++++
benchmark.js | 7 +-
index.d.ts | 574 +------------------------------------
index.js | 485 +------------------------------
index.test-d.ts | 62 ++--
license | 2 +-
package.json | 31 +-
readme.md | 81 +++---
test/exclude.js | 4 +-
test/extract.js | 10 +-
test/parse-url.js | 11 +-
test/parse.js | 91 +++---
test/pick.js | 4 +-
test/properties.js | 23 +-
test/stringify-url.js | 46 +--
test/stringify.js | 154 +++++-----
18 files changed, 1361 insertions(+), 1307 deletions(-)
create mode 100644 base.d.ts
create mode 100644 base.js
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ca8ed6f..d50ada6 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -10,10 +10,9 @@ jobs:
fail-fast: false
matrix:
node-version:
+ - 18
+ - 16
- 14
- - 12
- - 10
- - 8
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
diff --git a/base.d.ts b/base.d.ts
new file mode 100644
index 0000000..cf3b592
--- /dev/null
+++ b/base.d.ts
@@ -0,0 +1,561 @@
+export type ParseOptions = {
+ /**
+ Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
+
+ @default true
+ */
+ readonly decode?: boolean;
+
+ /**
+ @default 'none'
+
+ - `bracket`: Parse arrays with bracket representation:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
+ //=> {foo: ['1', '2', '3']}
+ ```
+
+ - `index`: Parse arrays with index representation:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
+ //=> {foo: ['1', '2', '3']}
+ ```
+
+ - `comma`: Parse arrays with elements separated by comma:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
+ //=> {foo: ['1', '2', '3']}
+ ```
+
+ - `separator`: Parse arrays with elements separated by a custom character:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['1', '2', '3']}
+ ```
+
+ - `bracket-separator`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: []}
+
+ queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['']}
+
+ queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['1']}
+
+ queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['1', '2', '3']}
+
+ queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['1', '', 3, '', '', '6']}
+
+ queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
+ ```
+
+ - `colon-list-separator`: Parse arrays with parameter names that are explicitly marked with `:list`:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'});
+ //=> {foo: ['one', 'two']}
+ ```
+
+ - `none`: Parse arrays with elements using duplicate keys:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo=1&foo=2&foo=3');
+ //=> {foo: ['1', '2', '3']}
+ ```
+ */
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
+
+ /**
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
+
+ @default ,
+ */
+ readonly arrayFormatSeparator?: string;
+
+ /**
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
+
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
+
+ @default true
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ const order = ['c', 'a', 'b'];
+
+ queryString.parse('?a=one&b=two&c=three', {
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
+ });
+ //=> {c: 'three', a: 'one', b: 'two'}
+ ```
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('?a=one&c=three&b=two', {sort: false});
+ //=> {a: 'one', c: 'three', b: 'two'}
+ ```
+ */
+ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
+
+ /**
+ Parse the value as a number type instead of string type if it's a number.
+
+ @default false
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo=1', {parseNumbers: true});
+ //=> {foo: 1}
+ ```
+ */
+ readonly parseNumbers?: boolean;
+
+ /**
+ Parse the value as a boolean type instead of string type if it's a boolean.
+
+ @default false
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.parse('foo=true', {parseBooleans: true});
+ //=> {foo: true}
+ ```
+ */
+ readonly parseBooleans?: boolean;
+
+ /**
+ Parse the fragment identifier from the URL and add it to result object.
+
+ @default false
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
+ ```
+ */
+ readonly parseFragmentIdentifier?: boolean;
+};
+
+export type ParsedQuery = Record>;
+
+/**
+Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
+
+The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
+
+@param query - The query string to parse.
+*/
+export function parse(query: string, options: {parseBooleans: true; parseNumbers: true} & ParseOptions): ParsedQuery;
+export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery;
+export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery;
+export function parse(query: string, options?: ParseOptions): ParsedQuery;
+
+export type ParsedUrl = {
+ readonly url: string;
+ readonly query: ParsedQuery;
+
+ /**
+ The fragment identifier of the URL.
+
+ Present when the `parseFragmentIdentifier` option is `true`.
+ */
+ readonly fragmentIdentifier?: string;
+};
+
+/**
+Extract the URL and the query string as an object.
+
+If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
+
+@param url - The URL to parse.
+
+@example
+```
+import queryString from 'query-string';
+
+queryString.parseUrl('https://foo.bar?foo=bar');
+//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
+
+queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
+//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
+```
+*/
+export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
+
+export type StringifyOptions = {
+ /**
+ Strictly encode URI components with [`strict-uri-encode`](https://github.com/kevva/strict-uri-encode). It uses [`encodeURIComponent`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
+
+ @default true
+ */
+ readonly strict?: boolean;
+
+ /**
+ [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
+
+ @default true
+ */
+ readonly encode?: boolean;
+
+ /**
+ @default 'none'
+
+ - `bracket`: Serialize arrays using bracket representation:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
+ //=> 'foo[]=1&foo[]=2&foo[]=3'
+ ```
+
+ - `index`: Serialize arrays using index representation:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
+ ```
+
+ - `comma`: Serialize arrays by separating elements with comma:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
+ //=> 'foo=1,2,3'
+
+ queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
+ //=> 'foo=1,,'
+ // Note that typing information for null values is lost
+ // and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
+ ```
+
+ - `separator`: Serialize arrays by separating elements with character:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
+ //=> 'foo=1|2|3'
+ ```
+
+ - `bracket-separator`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]'
+
+ queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]='
+
+ queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]=1'
+
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]=1|2|3'
+
+ queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]=1||3|||6'
+
+ queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
+ //=> 'foo[]=1||3|6'
+
+ queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
+ //=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
+ ```
+
+ - `colon-list-separator`: Serialize arrays with parameter names that are explicitly marked with `:list`:
+
+ ```js
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'});
+ //=> 'foo:list=one&foo:list=two'
+ ```
+
+ - `none`: Serialize arrays by using duplicate keys:
+
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({foo: [1, 2, 3]});
+ //=> 'foo=1&foo=2&foo=3'
+ ```
+ */
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
+
+ /**
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
+
+ @default ,
+ */
+ readonly arrayFormatSeparator?: string;
+
+ /**
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
+
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
+
+ @default true
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ const order = ['c', 'a', 'b'];
+
+ queryString.stringify({a: 1, b: 2, c: 3}, {
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
+ });
+ //=> 'c=3&a=1&b=2'
+ ```
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
+ //=> 'b=1&c=2&a=3'
+ ```
+ */
+ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
+
+ /**
+ Skip keys with `null` as the value.
+
+ Note that keys with `undefined` as the value are always skipped.
+
+ @default false
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
+ skipNull: true
+ });
+ //=> 'a=1&d=4'
+
+ queryString.stringify({a: undefined, b: null}, {
+ skipNull: true
+ });
+ //=> ''
+ ```
+ */
+ readonly skipNull?: boolean;
+
+ /**
+ Skip keys with an empty string as the value.
+
+ @default false
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({a: 1, b: '', c: '', d: 4}, {
+ skipEmptyString: true
+ });
+ //=> 'a=1&d=4'
+ ```
+
+ @example
+ ```
+ import queryString from 'query-string';
+
+ queryString.stringify({a: '', b: ''}, {
+ skipEmptyString: true
+ });
+ //=> ''
+ ```
+ */
+ readonly skipEmptyString?: boolean;
+};
+
+export type Stringifiable = string | boolean | number | null | undefined; // eslint-disable-line @typescript-eslint/ban-types
+
+export type StringifiableRecord = Record<
+string,
+Stringifiable | readonly Stringifiable[]
+>;
+
+/**
+Stringify an object into a query string and sort the keys.
+*/
+export function stringify(
+ // TODO: Use the below instead when the following TS issues are fixed:
+ // - https://github.com/microsoft/TypeScript/issues/15300
+ // - https://github.com/microsoft/TypeScript/issues/42021
+ // Context: https://github.com/sindresorhus/query-string/issues/298
+ // object: StringifiableRecord,
+ object: Record,
+ options?: StringifyOptions
+): string;
+
+/**
+Extract a query string from a URL that can be passed into `.parse()`.
+
+Note: This behaviour can be changed with the `skipNull` option.
+*/
+export function extract(url: string): string;
+
+export type UrlObject = {
+ readonly url: string;
+
+ /**
+ Overrides queries in the `url` property.
+ */
+ readonly query?: StringifiableRecord;
+
+ /**
+ Overrides the fragment identifier in the `url` property.
+ */
+ readonly fragmentIdentifier?: string;
+};
+
+/**
+Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
+
+Query items in the `query` property overrides queries in the `url` property.
+
+The `fragmentIdentifier` property overrides the fragment identifier in the `url` property.
+
+@example
+```
+queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+
+queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+
+queryString.stringifyUrl({
+ url: 'https://foo.bar',
+ query: {
+ top: 'foo'
+ },
+ fragmentIdentifier: 'bar'
+});
+//=> 'https://foo.bar?top=foo#bar'
+```
+*/
+export function stringifyUrl(
+ object: UrlObject,
+ options?: StringifyOptions
+): string;
+
+/**
+Pick query parameters from a URL.
+
+@param url - The URL containing the query parameters to pick.
+@param keys - The names of the query parameters to keep. All other query parameters will be removed from the URL.
+@param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
+
+@returns The URL with the picked query parameters.
+
+@example
+```
+queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']);
+//=> 'https://foo.bar?foo=1#hello'
+
+queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
+//=> 'https://foo.bar?bar=2#hello'
+```
+*/
+export function pick(
+ url: string,
+ keys: readonly string[],
+ options?: ParseOptions & StringifyOptions
+): string;
+export function pick(
+ url: string,
+ filter: (key: string, value: string | boolean | number) => boolean,
+ options?: {parseBooleans: true; parseNumbers: true} & ParseOptions & StringifyOptions
+): string;
+export function pick(
+ url: string,
+ filter: (key: string, value: string | boolean) => boolean,
+ options?: {parseBooleans: true} & ParseOptions & StringifyOptions
+): string;
+export function pick(
+ url: string,
+ filter: (key: string, value: string | number) => boolean,
+ options?: {parseNumbers: true} & ParseOptions & StringifyOptions
+): string;
+
+/**
+Exclude query parameters from a URL. Like `.pick()` but reversed.
+
+@param url - The URL containing the query parameters to exclude.
+@param keys - The names of the query parameters to remove. All other query parameters will remain in the URL.
+@param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
+
+@returns The URL without the excluded the query parameters.
+
+@example
+```
+queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']);
+//=> 'https://foo.bar?bar=2#hello'
+
+queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
+//=> 'https://foo.bar?foo=1#hello'
+```
+*/
+export function exclude(
+ url: string,
+ keys: readonly string[],
+ options?: ParseOptions & StringifyOptions
+): string;
+export function exclude(
+ url: string,
+ filter: (key: string, value: string | boolean | number) => boolean,
+ options?: {parseBooleans: true; parseNumbers: true} & ParseOptions & StringifyOptions
+): string;
+export function exclude(
+ url: string,
+ filter: (key: string, value: string | boolean) => boolean,
+ options?: {parseBooleans: true} & ParseOptions & StringifyOptions
+): string;
+export function exclude(
+ url: string,
+ filter: (key: string, value: string | number) => boolean,
+ options?: {parseNumbers: true} & ParseOptions & StringifyOptions
+): string;
diff --git a/base.js b/base.js
new file mode 100644
index 0000000..ba41935
--- /dev/null
+++ b/base.js
@@ -0,0 +1,517 @@
+import decodeComponent from 'decode-uri-component';
+import splitOnFirst from 'split-on-first';
+import {includeKeys} from 'filter-obj';
+
+const isNullOrUndefined = value => value === null || value === undefined;
+
+// eslint-disable-next-line unicorn/prefer-code-point
+const strictUriEncode = string => encodeURIComponent(string).replace(/[!'()*]/g, x => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
+
+const encodeFragmentIdentifier = Symbol('encodeFragmentIdentifier');
+
+function encoderForArrayFormat(options) {
+ switch (options.arrayFormat) {
+ case 'index': {
+ return key => (result, value) => {
+ const index = result.length;
+
+ if (
+ value === undefined
+ || (options.skipNull && value === null)
+ || (options.skipEmptyString && value === '')
+ ) {
+ return result;
+ }
+
+ if (value === null) {
+ return [
+ ...result, [encode(key, options), '[', index, ']'].join(''),
+ ];
+ }
+
+ return [
+ ...result,
+ [encode(key, options), '[', encode(index, options), ']=', encode(value, options)].join(''),
+ ];
+ };
+ }
+
+ case 'bracket': {
+ return key => (result, value) => {
+ if (
+ value === undefined
+ || (options.skipNull && value === null)
+ || (options.skipEmptyString && value === '')
+ ) {
+ return result;
+ }
+
+ if (value === null) {
+ return [
+ ...result,
+ [encode(key, options), '[]'].join(''),
+ ];
+ }
+
+ return [
+ ...result,
+ [encode(key, options), '[]=', encode(value, options)].join(''),
+ ];
+ };
+ }
+
+ case 'colon-list-separator': {
+ return key => (result, value) => {
+ if (
+ value === undefined
+ || (options.skipNull && value === null)
+ || (options.skipEmptyString && value === '')
+ ) {
+ return result;
+ }
+
+ if (value === null) {
+ return [
+ ...result,
+ [encode(key, options), ':list='].join(''),
+ ];
+ }
+
+ return [
+ ...result,
+ [encode(key, options), ':list=', encode(value, options)].join(''),
+ ];
+ };
+ }
+
+ case 'comma':
+ case 'separator':
+ case 'bracket-separator': {
+ const keyValueSep = options.arrayFormat === 'bracket-separator'
+ ? '[]='
+ : '=';
+
+ return key => (result, value) => {
+ if (
+ value === undefined
+ || (options.skipNull && value === null)
+ || (options.skipEmptyString && value === '')
+ ) {
+ return result;
+ }
+
+ // Translate null to an empty string so that it doesn't serialize as 'null'
+ value = value === null ? '' : value;
+
+ if (result.length === 0) {
+ return [[encode(key, options), keyValueSep, encode(value, options)].join('')];
+ }
+
+ return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
+ };
+ }
+
+ default: {
+ return key => (result, value) => {
+ if (
+ value === undefined
+ || (options.skipNull && value === null)
+ || (options.skipEmptyString && value === '')
+ ) {
+ return result;
+ }
+
+ if (value === null) {
+ return [
+ ...result,
+ encode(key, options),
+ ];
+ }
+
+ return [
+ ...result,
+ [encode(key, options), '=', encode(value, options)].join(''),
+ ];
+ };
+ }
+ }
+}
+
+function parserForArrayFormat(options) {
+ let result;
+
+ switch (options.arrayFormat) {
+ case 'index': {
+ return (key, value, accumulator) => {
+ result = /\[(\d*)]$/.exec(key);
+
+ key = key.replace(/\[\d*]$/, '');
+
+ if (!result) {
+ accumulator[key] = value;
+ return;
+ }
+
+ if (accumulator[key] === undefined) {
+ accumulator[key] = {};
+ }
+
+ accumulator[key][result[1]] = value;
+ };
+ }
+
+ case 'bracket': {
+ return (key, value, accumulator) => {
+ result = /(\[])$/.exec(key);
+ key = key.replace(/\[]$/, '');
+
+ if (!result) {
+ accumulator[key] = value;
+ return;
+ }
+
+ if (accumulator[key] === undefined) {
+ accumulator[key] = [value];
+ return;
+ }
+
+ accumulator[key] = [...accumulator[key], value];
+ };
+ }
+
+ case 'colon-list-separator': {
+ return (key, value, accumulator) => {
+ result = /(:list)$/.exec(key);
+ key = key.replace(/:list$/, '');
+
+ if (!result) {
+ accumulator[key] = value;
+ return;
+ }
+
+ if (accumulator[key] === undefined) {
+ accumulator[key] = [value];
+ return;
+ }
+
+ accumulator[key] = [...accumulator[key], value];
+ };
+ }
+
+ case 'comma':
+ case 'separator': {
+ return (key, value, accumulator) => {
+ const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
+ const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
+ value = isEncodedArray ? decode(value, options) : value;
+ const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : (value === null ? value : decode(value, options));
+ accumulator[key] = newValue;
+ };
+ }
+
+ case 'bracket-separator': {
+ return (key, value, accumulator) => {
+ const isArray = /(\[])$/.test(key);
+ key = key.replace(/\[]$/, '');
+
+ if (!isArray) {
+ accumulator[key] = value ? decode(value, options) : value;
+ return;
+ }
+
+ const arrayValue = value === null
+ ? []
+ : value.split(options.arrayFormatSeparator).map(item => decode(item, options));
+
+ if (accumulator[key] === undefined) {
+ accumulator[key] = arrayValue;
+ return;
+ }
+
+ accumulator[key] = [...accumulator[key], ...arrayValue];
+ };
+ }
+
+ default: {
+ return (key, value, accumulator) => {
+ if (accumulator[key] === undefined) {
+ accumulator[key] = value;
+ return;
+ }
+
+ accumulator[key] = [...[accumulator[key]].flat(), value];
+ };
+ }
+ }
+}
+
+function validateArrayFormatSeparator(value) {
+ if (typeof value !== 'string' || value.length !== 1) {
+ throw new TypeError('arrayFormatSeparator must be single character string');
+ }
+}
+
+function encode(value, options) {
+ if (options.encode) {
+ return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
+ }
+
+ return value;
+}
+
+function decode(value, options) {
+ if (options.decode) {
+ return decodeComponent(value);
+ }
+
+ return value;
+}
+
+function keysSorter(input) {
+ if (Array.isArray(input)) {
+ return input.sort();
+ }
+
+ if (typeof input === 'object') {
+ return keysSorter(Object.keys(input))
+ .sort((a, b) => Number(a) - Number(b))
+ .map(key => input[key]);
+ }
+
+ return input;
+}
+
+function removeHash(input) {
+ const hashStart = input.indexOf('#');
+ if (hashStart !== -1) {
+ input = input.slice(0, hashStart);
+ }
+
+ return input;
+}
+
+function getHash(url) {
+ let hash = '';
+ const hashStart = url.indexOf('#');
+ if (hashStart !== -1) {
+ hash = url.slice(hashStart);
+ }
+
+ return hash;
+}
+
+function parseValue(value, options) {
+ if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
+ value = Number(value);
+ } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
+ value = value.toLowerCase() === 'true';
+ }
+
+ return value;
+}
+
+export function extract(input) {
+ input = removeHash(input);
+ const queryStart = input.indexOf('?');
+ if (queryStart === -1) {
+ return '';
+ }
+
+ return input.slice(queryStart + 1);
+}
+
+export function parse(query, options) {
+ options = {
+ decode: true,
+ sort: true,
+ arrayFormat: 'none',
+ arrayFormatSeparator: ',',
+ parseNumbers: false,
+ parseBooleans: false,
+ ...options,
+ };
+
+ validateArrayFormatSeparator(options.arrayFormatSeparator);
+
+ const formatter = parserForArrayFormat(options);
+
+ // Create an object with no prototype
+ const returnValue = Object.create(null);
+
+ if (typeof query !== 'string') {
+ return returnValue;
+ }
+
+ query = query.trim().replace(/^[?#&]/, '');
+
+ if (!query) {
+ return returnValue;
+ }
+
+ for (const parameter of query.split('&')) {
+ if (parameter === '') {
+ continue;
+ }
+
+ let [key, value] = splitOnFirst(options.decode ? parameter.replace(/\+/g, ' ') : parameter, '=');
+
+ // Missing `=` should be `null`:
+ // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
+ value = value === undefined ? null : (['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options));
+ formatter(decode(key, options), value, returnValue);
+ }
+
+ for (const [key, value] of Object.entries(returnValue)) {
+ if (typeof value === 'object' && value !== null) {
+ for (const [key2, value2] of Object.entries(value)) {
+ value[key2] = parseValue(value2, options);
+ }
+ } else {
+ returnValue[key] = parseValue(value, options);
+ }
+ }
+
+ if (options.sort === false) {
+ return returnValue;
+ }
+
+ // TODO: Remove the use of `reduce`.
+ // eslint-disable-next-line unicorn/no-array-reduce
+ return (options.sort === true ? Object.keys(returnValue).sort() : Object.keys(returnValue).sort(options.sort)).reduce((result, key) => {
+ const value = returnValue[key];
+ if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
+ // Sort object keys, not values
+ result[key] = keysSorter(value);
+ } else {
+ result[key] = value;
+ }
+
+ return result;
+ }, Object.create(null));
+}
+
+export function stringify(object, options) {
+ if (!object) {
+ return '';
+ }
+
+ options = {encode: true,
+ strict: true,
+ arrayFormat: 'none',
+ arrayFormatSeparator: ',', ...options};
+
+ validateArrayFormatSeparator(options.arrayFormatSeparator);
+
+ const shouldFilter = key => (
+ (options.skipNull && isNullOrUndefined(object[key]))
+ || (options.skipEmptyString && object[key] === '')
+ );
+
+ const formatter = encoderForArrayFormat(options);
+
+ const objectCopy = {};
+
+ for (const [key, value] of Object.entries(object)) {
+ if (!shouldFilter(key)) {
+ objectCopy[key] = value;
+ }
+ }
+
+ const keys = Object.keys(objectCopy);
+
+ if (options.sort !== false) {
+ keys.sort(options.sort);
+ }
+
+ return keys.map(key => {
+ const value = object[key];
+
+ if (value === undefined) {
+ return '';
+ }
+
+ if (value === null) {
+ return encode(key, options);
+ }
+
+ if (Array.isArray(value)) {
+ if (value.length === 0 && options.arrayFormat === 'bracket-separator') {
+ return encode(key, options) + '[]';
+ }
+
+ return value
+ .reduce(formatter(key), [])
+ .join('&');
+ }
+
+ return encode(key, options) + '=' + encode(value, options);
+ }).filter(x => x.length > 0).join('&');
+}
+
+export function parseUrl(url, options) {
+ options = {
+ decode: true,
+ ...options,
+ };
+
+ const [url_, hash] = splitOnFirst(url, '#');
+
+ return {
+ url: url_?.split('?')?.[0] ?? '',
+ query: parse(extract(url), options),
+ ...(options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}),
+ };
+}
+
+export function stringifyUrl(object, options) {
+ options = {
+ encode: true,
+ strict: true,
+ [encodeFragmentIdentifier]: true,
+ ...options,
+ };
+
+ const url = removeHash(object.url).split('?')[0] || '';
+ const queryFromUrl = extract(object.url);
+
+ const query = {
+ ...parse(queryFromUrl, {sort: false}),
+ ...object.query,
+ };
+
+ let queryString = stringify(query, options);
+ if (queryString) {
+ queryString = `?${queryString}`;
+ }
+
+ let hash = getHash(object.url);
+ if (object.fragmentIdentifier) {
+ const urlObjectForFragmentEncode = new URL(url);
+ urlObjectForFragmentEncode.hash = object.fragmentIdentifier;
+ hash = options[encodeFragmentIdentifier] ? urlObjectForFragmentEncode.hash : `#${object.fragmentIdentifier}`;
+ }
+
+ return `${url}${queryString}${hash}`;
+}
+
+export function pick(input, filter, options) {
+ options = {
+ parseFragmentIdentifier: true,
+ [encodeFragmentIdentifier]: false,
+ ...options,
+ };
+
+ const {url, query, fragmentIdentifier} = parseUrl(input, options);
+
+ return stringifyUrl({
+ url,
+ query: includeKeys(query, filter),
+ fragmentIdentifier,
+ }, options);
+}
+
+export function exclude(input, filter, options) {
+ const exclusionFilter = Array.isArray(filter) ? key => !filter.includes(key) : (key, value) => !filter(key, value);
+
+ return pick(input, exclusionFilter, options);
+}
diff --git a/benchmark.js b/benchmark.js
index b152460..0822c68 100644
--- a/benchmark.js
+++ b/benchmark.js
@@ -1,6 +1,5 @@
-'use strict';
-const Benchmark = require('benchmark');
-const queryString = require('.');
+import Benchmark from 'benchmark';
+import queryString from './index.js';
const {stringify, stringifyUrl} = queryString;
const suite = new Benchmark.Suite();
@@ -13,7 +12,7 @@ const TEST_OBJECT = {
published: true,
symbols: 'πµ',
chapters: [1, 2, 3],
- none: null
+ none: null,
};
const TEST_HOST = 'https://foo.bar/';
const TEST_STRING = stringify(TEST_OBJECT);
diff --git a/index.d.ts b/index.d.ts
index 59de603..a99eacb 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,563 +1,11 @@
-export interface ParseOptions {
- /**
- Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
-
- @default true
- */
- readonly decode?: boolean;
-
- /**
- @default 'none'
-
- - `bracket`: Parse arrays with bracket representation:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
- //=> {foo: ['1', '2', '3']}
- ```
-
- - `index`: Parse arrays with index representation:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
- //=> {foo: ['1', '2', '3']}
- ```
-
- - `comma`: Parse arrays with elements separated by comma:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
- //=> {foo: ['1', '2', '3']}
- ```
-
- - `separator`: Parse arrays with elements separated by a custom character:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
- //=> {foo: ['1', '2', '3']}
- ```
-
- - `bracket-separator`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: []}
-
- queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: ['']}
-
- queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: ['1']}
-
- queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: ['1', '2', '3']}
-
- queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: ['1', '', 3, '', '', '6']}
-
- queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
- ```
-
- - `colon-list-separator`: Parse arrays with parameter names that are explicitly marked with `:list`:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'});
- //=> {foo: ['one', 'two']}
- ```
-
- - `none`: Parse arrays with elements using duplicate keys:
-
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo=1&foo=2&foo=3');
- //=> {foo: ['1', '2', '3']}
- ```
- */
- readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
-
- /**
- The character used to separate array elements when using `{arrayFormat: 'separator'}`.
-
- @default ,
- */
- readonly arrayFormatSeparator?: string;
-
- /**
- Supports both `Function` as a custom sorting function or `false` to disable sorting.
-
- If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
-
- @default true
-
- @example
- ```
- import queryString = require('query-string');
-
- const order = ['c', 'a', 'b'];
-
- queryString.parse('?a=one&b=two&c=three', {
- sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
- });
- //=> {c: 'three', a: 'one', b: 'two'}
- ```
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.parse('?a=one&c=three&b=two', {sort: false});
- //=> {a: 'one', c: 'three', b: 'two'}
- ```
- */
- readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
-
- /**
- Parse the value as a number type instead of string type if it's a number.
-
- @default false
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo=1', {parseNumbers: true});
- //=> {foo: 1}
- ```
- */
- readonly parseNumbers?: boolean;
-
- /**
- Parse the value as a boolean type instead of string type if it's a boolean.
-
- @default false
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.parse('foo=true', {parseBooleans: true});
- //=> {foo: true}
- ```
- */
- readonly parseBooleans?: boolean;
-
- /**
- Parse the fragment identifier from the URL and add it to result object.
-
- @default false
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
- //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
- ```
- */
- readonly parseFragmentIdentifier?: boolean;
-}
-
-export interface ParsedQuery {
- [key: string]: T | null | Array;
-}
-
-/**
-Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
-
-The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
-
-@param query - The query string to parse.
-*/
-export function parse(query: string, options: {parseBooleans: true, parseNumbers: true} & ParseOptions): ParsedQuery;
-export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery;
-export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery;
-export function parse(query: string, options?: ParseOptions): ParsedQuery;
-
-export interface ParsedUrl {
- readonly url: string;
- readonly query: ParsedQuery;
-
- /**
- The fragment identifier of the URL.
-
- Present when the `parseFragmentIdentifier` option is `true`.
- */
- readonly fragmentIdentifier?: string;
-}
-
-/**
-Extract the URL and the query string as an object.
-
-If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
-
-@param url - The URL to parse.
-
-@example
-```
-import queryString = require('query-string');
-
-queryString.parseUrl('https://foo.bar?foo=bar');
-//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
-
-queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
-//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
-```
-*/
-export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
-
-export interface StringifyOptions {
- /**
- Strictly encode URI components with [`strict-uri-encode`](https://github.com/kevva/strict-uri-encode). It uses [`encodeURIComponent`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
-
- @default true
- */
- readonly strict?: boolean;
-
- /**
- [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
-
- @default true
- */
- readonly encode?: boolean;
-
- /**
- @default 'none'
-
- - `bracket`: Serialize arrays using bracket representation:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
- //=> 'foo[]=1&foo[]=2&foo[]=3'
- ```
-
- - `index`: Serialize arrays using index representation:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
- //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
- ```
-
- - `comma`: Serialize arrays by separating elements with comma:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
- //=> 'foo=1,2,3'
-
- queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
- //=> 'foo=1,,'
- // Note that typing information for null values is lost
- // and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
- ```
-
- - `separator`: Serialize arrays by separating elements with character:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
- //=> 'foo=1|2|3'
- ```
-
- - `bracket-separator`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]'
-
- queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]='
-
- queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]=1'
-
- queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]=1|2|3'
-
- queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]=1||3|||6'
-
- queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
- //=> 'foo[]=1||3|6'
-
- queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
- //=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
- ```
-
- - `colon-list-separator`: Serialize arrays with parameter names that are explicitly marked with `:list`:
-
- ```js
- import queryString = require('query-string');
-
- queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'});
- //=> 'foo:list=one&foo:list=two'
- ```
-
- - `none`: Serialize arrays by using duplicate keys:
-
- ```
- import queryString = require('query-string');
-
- queryString.stringify({foo: [1, 2, 3]});
- //=> 'foo=1&foo=2&foo=3'
- ```
- */
- readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
-
- /**
- The character used to separate array elements when using `{arrayFormat: 'separator'}`.
-
- @default ,
- */
- readonly arrayFormatSeparator?: string;
-
- /**
- Supports both `Function` as a custom sorting function or `false` to disable sorting.
-
- If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
-
- @default true
-
- @example
- ```
- import queryString = require('query-string');
-
- const order = ['c', 'a', 'b'];
-
- queryString.stringify({a: 1, b: 2, c: 3}, {
- sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
- });
- //=> 'c=3&a=1&b=2'
- ```
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
- //=> 'b=1&c=2&a=3'
- ```
- */
- readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
-
- /**
- Skip keys with `null` as the value.
-
- Note that keys with `undefined` as the value are always skipped.
-
- @default false
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
- skipNull: true
- });
- //=> 'a=1&d=4'
-
- queryString.stringify({a: undefined, b: null}, {
- skipNull: true
- });
- //=> ''
- ```
- */
- readonly skipNull?: boolean;
-
- /**
- Skip keys with an empty string as the value.
-
- @default false
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.stringify({a: 1, b: '', c: '', d: 4}, {
- skipEmptyString: true
- });
- //=> 'a=1&d=4'
- ```
-
- @example
- ```
- import queryString = require('query-string');
-
- queryString.stringify({a: '', b: ''}, {
- skipEmptyString: true
- });
- //=> ''
- ```
- */
- readonly skipEmptyString?: boolean;
-}
-
-export type Stringifiable = string | boolean | number | null | undefined;
-
-export type StringifiableRecord = Record<
- string,
- Stringifiable | readonly Stringifiable[]
->;
-
-/**
-Stringify an object into a query string and sort the keys.
-*/
-export function stringify(
- // TODO: Use the below instead when the following TS issues are fixed:
- // - https://github.com/microsoft/TypeScript/issues/15300
- // - https://github.com/microsoft/TypeScript/issues/42021
- // Context: https://github.com/sindresorhus/query-string/issues/298
- // object: StringifiableRecord,
- object: Record,
- options?: StringifyOptions
-): string;
-
-/**
-Extract a query string from a URL that can be passed into `.parse()`.
-
-Note: This behaviour can be changed with the `skipNull` option.
-*/
-export function extract(url: string): string;
-
-export interface UrlObject {
- readonly url: string;
-
- /**
- Overrides queries in the `url` property.
- */
- readonly query?: StringifiableRecord;
-
- /**
- Overrides the fragment identifier in the `url` property.
- */
- readonly fragmentIdentifier?: string;
-}
-
-/**
-Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
-
-Query items in the `query` property overrides queries in the `url` property.
-
-The `fragmentIdentifier` property overrides the fragment identifier in the `url` property.
-
-@example
-```
-queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
-//=> 'https://foo.bar?foo=bar'
-
-queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
-//=> 'https://foo.bar?foo=bar'
-
-queryString.stringifyUrl({
- url: 'https://foo.bar',
- query: {
- top: 'foo'
- },
- fragmentIdentifier: 'bar'
-});
-//=> 'https://foo.bar?top=foo#bar'
-```
-*/
-export function stringifyUrl(
- object: UrlObject,
- options?: StringifyOptions
-): string;
-
-/**
-Pick query parameters from a URL.
-
-@param url - The URL containing the query parameters to pick.
-@param keys - The names of the query parameters to keep. All other query parameters will be removed from the URL.
-@param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
-
-@returns The URL with the picked query parameters.
-
-@example
-```
-queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']);
-//=> 'https://foo.bar?foo=1#hello'
-
-queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
-//=> 'https://foo.bar?bar=2#hello'
-```
-*/
-export function pick(
- url: string,
- keys: readonly string[],
- options?: ParseOptions & StringifyOptions
-): string
-export function pick(
- url: string,
- filter: (key: string, value: string | boolean | number) => boolean,
- options?: {parseBooleans: true, parseNumbers: true} & ParseOptions & StringifyOptions
-): string
-export function pick(
- url: string,
- filter: (key: string, value: string | boolean) => boolean,
- options?: {parseBooleans: true} & ParseOptions & StringifyOptions
-): string
-export function pick(
- url: string,
- filter: (key: string, value: string | number) => boolean,
- options?: {parseNumbers: true} & ParseOptions & StringifyOptions
-): string
-
-/**
-Exclude query parameters from a URL. Like `.pick()` but reversed.
-
-@param url - The URL containing the query parameters to exclude.
-@param keys - The names of the query parameters to remove. All other query parameters will remain in the URL.
-@param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
-
-@returns The URL without the excluded the query parameters.
-
-@example
-```
-queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']);
-//=> 'https://foo.bar?bar=2#hello'
-
-queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
-//=> 'https://foo.bar?foo=1#hello'
-```
-*/
-export function exclude(
- url: string,
- keys: readonly string[],
- options?: ParseOptions & StringifyOptions
-): string
-export function exclude(
- url: string,
- filter: (key: string, value: string | boolean | number) => boolean,
- options?: {parseBooleans: true, parseNumbers: true} & ParseOptions & StringifyOptions
-): string
-export function exclude(
- url: string,
- filter: (key: string, value: string | boolean) => boolean,
- options?: {parseBooleans: true} & ParseOptions & StringifyOptions
-): string
-export function exclude(
- url: string,
- filter: (key: string, value: string | number) => boolean,
- options?: {parseNumbers: true} & ParseOptions & StringifyOptions
-): string
+export * as default from './base.js';
+
+export {
+ type ParseOptions,
+ type ParsedQuery,
+ type ParsedUrl,
+ type StringifyOptions,
+ type Stringifiable,
+ type StringifiableRecord,
+ type UrlObject,
+} from './base.js';
diff --git a/index.js b/index.js
index 129ea26..36a7b1f 100644
--- a/index.js
+++ b/index.js
@@ -1,484 +1 @@
-'use strict';
-const strictUriEncode = require('strict-uri-encode');
-const decodeComponent = require('decode-uri-component');
-const splitOnFirst = require('split-on-first');
-const filterObject = require('filter-obj');
-
-const isNullOrUndefined = value => value === null || value === undefined;
-
-const encodeFragmentIdentifier = Symbol('encodeFragmentIdentifier');
-
-function encoderForArrayFormat(options) {
- switch (options.arrayFormat) {
- case 'index':
- return key => (result, value) => {
- const index = result.length;
-
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
-
- if (value === null) {
- return [...result, [encode(key, options), '[', index, ']'].join('')];
- }
-
- return [
- ...result,
- [encode(key, options), '[', encode(index, options), ']=', encode(value, options)].join('')
- ];
- };
-
- case 'bracket':
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
-
- if (value === null) {
- return [...result, [encode(key, options), '[]'].join('')];
- }
-
- return [...result, [encode(key, options), '[]=', encode(value, options)].join('')];
- };
-
- case 'colon-list-separator':
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
-
- if (value === null) {
- return [...result, [encode(key, options), ':list='].join('')];
- }
-
- return [...result, [encode(key, options), ':list=', encode(value, options)].join('')];
- };
-
- case 'comma':
- case 'separator':
- case 'bracket-separator': {
- const keyValueSep = options.arrayFormat === 'bracket-separator' ?
- '[]=' :
- '=';
-
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
-
- // Translate null to an empty string so that it doesn't serialize as 'null'
- value = value === null ? '' : value;
-
- if (result.length === 0) {
- return [[encode(key, options), keyValueSep, encode(value, options)].join('')];
- }
-
- return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
- };
- }
-
- default:
- return key => (result, value) => {
- if (
- value === undefined ||
- (options.skipNull && value === null) ||
- (options.skipEmptyString && value === '')
- ) {
- return result;
- }
-
- if (value === null) {
- return [...result, encode(key, options)];
- }
-
- return [...result, [encode(key, options), '=', encode(value, options)].join('')];
- };
- }
-}
-
-function parserForArrayFormat(options) {
- let result;
-
- switch (options.arrayFormat) {
- case 'index':
- return (key, value, accumulator) => {
- result = /\[(\d*)\]$/.exec(key);
-
- key = key.replace(/\[\d*\]$/, '');
-
- if (!result) {
- accumulator[key] = value;
- return;
- }
-
- if (accumulator[key] === undefined) {
- accumulator[key] = {};
- }
-
- accumulator[key][result[1]] = value;
- };
-
- case 'bracket':
- return (key, value, accumulator) => {
- result = /(\[\])$/.exec(key);
- key = key.replace(/\[\]$/, '');
-
- if (!result) {
- accumulator[key] = value;
- return;
- }
-
- if (accumulator[key] === undefined) {
- accumulator[key] = [value];
- return;
- }
-
- accumulator[key] = [].concat(accumulator[key], value);
- };
-
- case 'colon-list-separator':
- return (key, value, accumulator) => {
- result = /(:list)$/.exec(key);
- key = key.replace(/:list$/, '');
-
- if (!result) {
- accumulator[key] = value;
- return;
- }
-
- if (accumulator[key] === undefined) {
- accumulator[key] = [value];
- return;
- }
-
- accumulator[key] = [].concat(accumulator[key], value);
- };
-
- case 'comma':
- case 'separator':
- return (key, value, accumulator) => {
- const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
- const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
- value = isEncodedArray ? decode(value, options) : value;
- const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
- accumulator[key] = newValue;
- };
-
- case 'bracket-separator':
- return (key, value, accumulator) => {
- const isArray = /(\[\])$/.test(key);
- key = key.replace(/\[\]$/, '');
-
- if (!isArray) {
- accumulator[key] = value ? decode(value, options) : value;
- return;
- }
-
- const arrayValue = value === null ?
- [] :
- value.split(options.arrayFormatSeparator).map(item => decode(item, options));
-
- if (accumulator[key] === undefined) {
- accumulator[key] = arrayValue;
- return;
- }
-
- accumulator[key] = [].concat(accumulator[key], arrayValue);
- };
-
- default:
- return (key, value, accumulator) => {
- if (accumulator[key] === undefined) {
- accumulator[key] = value;
- return;
- }
-
- accumulator[key] = [].concat(accumulator[key], value);
- };
- }
-}
-
-function validateArrayFormatSeparator(value) {
- if (typeof value !== 'string' || value.length !== 1) {
- throw new TypeError('arrayFormatSeparator must be single character string');
- }
-}
-
-function encode(value, options) {
- if (options.encode) {
- return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
- }
-
- return value;
-}
-
-function decode(value, options) {
- if (options.decode) {
- return decodeComponent(value);
- }
-
- return value;
-}
-
-function keysSorter(input) {
- if (Array.isArray(input)) {
- return input.sort();
- }
-
- if (typeof input === 'object') {
- return keysSorter(Object.keys(input))
- .sort((a, b) => Number(a) - Number(b))
- .map(key => input[key]);
- }
-
- return input;
-}
-
-function removeHash(input) {
- const hashStart = input.indexOf('#');
- if (hashStart !== -1) {
- input = input.slice(0, hashStart);
- }
-
- return input;
-}
-
-function getHash(url) {
- let hash = '';
- const hashStart = url.indexOf('#');
- if (hashStart !== -1) {
- hash = url.slice(hashStart);
- }
-
- return hash;
-}
-
-function extract(input) {
- input = removeHash(input);
- const queryStart = input.indexOf('?');
- if (queryStart === -1) {
- return '';
- }
-
- return input.slice(queryStart + 1);
-}
-
-function parseValue(value, options) {
- if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
- value = Number(value);
- } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
- value = value.toLowerCase() === 'true';
- }
-
- return value;
-}
-
-function parse(query, options) {
- options = Object.assign({
- decode: true,
- sort: true,
- arrayFormat: 'none',
- arrayFormatSeparator: ',',
- parseNumbers: false,
- parseBooleans: false
- }, options);
-
- validateArrayFormatSeparator(options.arrayFormatSeparator);
-
- const formatter = parserForArrayFormat(options);
-
- // Create an object with no prototype
- const ret = Object.create(null);
-
- if (typeof query !== 'string') {
- return ret;
- }
-
- query = query.trim().replace(/^[?#&]/, '');
-
- if (!query) {
- return ret;
- }
-
- for (const param of query.split('&')) {
- if (param === '') {
- continue;
- }
-
- let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
-
- // Missing `=` should be `null`:
- // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
- value = value === undefined ? null : ['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options);
- formatter(decode(key, options), value, ret);
- }
-
- for (const key of Object.keys(ret)) {
- const value = ret[key];
- if (typeof value === 'object' && value !== null) {
- for (const k of Object.keys(value)) {
- value[k] = parseValue(value[k], options);
- }
- } else {
- ret[key] = parseValue(value, options);
- }
- }
-
- if (options.sort === false) {
- return ret;
- }
-
- return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
- const value = ret[key];
- if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
- // Sort object keys, not values
- result[key] = keysSorter(value);
- } else {
- result[key] = value;
- }
-
- return result;
- }, Object.create(null));
-}
-
-exports.extract = extract;
-exports.parse = parse;
-
-exports.stringify = (object, options) => {
- if (!object) {
- return '';
- }
-
- options = Object.assign({
- encode: true,
- strict: true,
- arrayFormat: 'none',
- arrayFormatSeparator: ','
- }, options);
-
- validateArrayFormatSeparator(options.arrayFormatSeparator);
-
- const shouldFilter = key => (
- (options.skipNull && isNullOrUndefined(object[key])) ||
- (options.skipEmptyString && object[key] === '')
- );
-
- const formatter = encoderForArrayFormat(options);
-
- const objectCopy = {};
-
- for (const key of Object.keys(object)) {
- if (!shouldFilter(key)) {
- objectCopy[key] = object[key];
- }
- }
-
- const keys = Object.keys(objectCopy);
-
- if (options.sort !== false) {
- keys.sort(options.sort);
- }
-
- return keys.map(key => {
- const value = object[key];
-
- if (value === undefined) {
- return '';
- }
-
- if (value === null) {
- return encode(key, options);
- }
-
- if (Array.isArray(value)) {
- if (value.length === 0 && options.arrayFormat === 'bracket-separator') {
- return encode(key, options) + '[]';
- }
-
- return value
- .reduce(formatter(key), [])
- .join('&');
- }
-
- return encode(key, options) + '=' + encode(value, options);
- }).filter(x => x.length > 0).join('&');
-};
-
-exports.parseUrl = (url, options) => {
- options = Object.assign({
- decode: true
- }, options);
-
- const [url_, hash] = splitOnFirst(url, '#');
-
- return Object.assign(
- {
- url: url_.split('?')[0] || '',
- query: parse(extract(url), options)
- },
- options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}
- );
-};
-
-exports.stringifyUrl = (object, options) => {
- options = Object.assign({
- encode: true,
- strict: true,
- [encodeFragmentIdentifier]: true
- }, options);
-
- const url = removeHash(object.url).split('?')[0] || '';
- const queryFromUrl = exports.extract(object.url);
- const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false});
-
- const query = Object.assign(parsedQueryFromUrl, object.query);
- let queryString = exports.stringify(query, options);
- if (queryString) {
- queryString = `?${queryString}`;
- }
-
- let hash = getHash(object.url);
- if (object.fragmentIdentifier) {
- const urlObjectForFragmentEncode = new URL(url);
- urlObjectForFragmentEncode.hash = object.fragmentIdentifier;
- hash = options[encodeFragmentIdentifier] ? urlObjectForFragmentEncode.hash : `#${object.fragmentIdentifier}`;
- }
-
- return `${url}${queryString}${hash}`;
-};
-
-exports.pick = (input, filter, options) => {
- options = Object.assign({
- parseFragmentIdentifier: true,
- [encodeFragmentIdentifier]: false
- }, options);
-
- const {url, query, fragmentIdentifier} = exports.parseUrl(input, options);
- return exports.stringifyUrl({
- url,
- query: filterObject(query, filter),
- fragmentIdentifier
- }, options);
-};
-
-exports.exclude = (input, filter, options) => {
- const exclusionFilter = Array.isArray(filter) ? key => !filter.includes(key) : (key, value) => !filter(key, value);
-
- return exports.pick(input, exclusionFilter, options);
-};
+export * as default from './base.js';
diff --git a/index.test-d.ts b/index.test-d.ts
index 2032584..bcb9702 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
-import * as queryString from '.';
+import queryString from './index.js';
// Stringify
expectType(
@@ -9,14 +9,14 @@ expectType(
num: 123,
numArray: [456],
bool: true,
- boolArray: [false]
- })
+ boolArray: [false],
+ }),
);
expectType(queryString.stringify({foo: 'bar'}, {strict: false}));
expectType(queryString.stringify({foo: 'bar'}, {encode: false}));
expectType(
- queryString.stringify({foo: 'bar'}, {arrayFormat: 'bracket'})
+ queryString.stringify({foo: 'bar'}, {arrayFormat: 'bracket'}),
);
expectType(queryString.stringify({foo: 'bar'}, {arrayFormat: 'index'}));
expectType(queryString.stringify({foo: 'bar'}, {arrayFormat: 'none'}));
@@ -30,18 +30,18 @@ expectType(
{foo: 'bar'},
{
sort: (itemLeft, itemRight) =>
- order.indexOf(itemLeft) - order.indexOf(itemRight)
- }
- )
+ order.indexOf(itemLeft) - order.indexOf(itemRight),
+ },
+ ),
);
// Ensure it accepts an `interface`.
-interface Query {
+type Query = {
foo: string;
-}
+};
const query: Query = {
- foo: 'bar'
+ foo: 'bar',
};
queryString.stringify(query);
@@ -50,56 +50,56 @@ queryString.stringify(query);
expectType(queryString.parse('?foo=bar'));
expectType(
- queryString.parse('?foo=bar', {decode: false})
+ queryString.parse('?foo=bar', {decode: false}),
);
expectType(
- queryString.parse('?foo=bar', {arrayFormat: 'bracket'})
+ queryString.parse('?foo=bar', {arrayFormat: 'bracket'}),
);
expectType(
- queryString.parse('?foo=bar', {arrayFormat: 'index'})
+ queryString.parse('?foo=bar', {arrayFormat: 'index'}),
);
expectType(
- queryString.parse('?foo=bar', {arrayFormat: 'none'})
+ queryString.parse('?foo=bar', {arrayFormat: 'none'}),
);
expectType(
- queryString.parse('?foo=bar', {arrayFormat: 'comma'})
+ queryString.parse('?foo=bar', {arrayFormat: 'comma'}),
);
expectType>(
- queryString.parse('?foo=1', {parseNumbers: true})
+ queryString.parse('?foo=1', {parseNumbers: true}),
);
expectType>(
- queryString.parse('?foo=true', {parseBooleans: true})
+ queryString.parse('?foo=true', {parseBooleans: true}),
);
expectType>(
- queryString.parse('?foo=true', {parseBooleans: true, parseNumbers: true})
+ queryString.parse('?foo=true', {parseBooleans: true, parseNumbers: true}),
);
// Parse URL
expectType(queryString.parseUrl('?foo=bar'));
expectType(
- queryString.parseUrl('?foo=bar', {decode: false})
+ queryString.parseUrl('?foo=bar', {decode: false}),
);
expectType(
- queryString.parseUrl('?foo=bar', {arrayFormat: 'bracket'})
+ queryString.parseUrl('?foo=bar', {arrayFormat: 'bracket'}),
);
expectType(
- queryString.parseUrl('?foo=bar', {arrayFormat: 'index'})
+ queryString.parseUrl('?foo=bar', {arrayFormat: 'index'}),
);
expectType(
- queryString.parseUrl('?foo=bar', {arrayFormat: 'none'})
+ queryString.parseUrl('?foo=bar', {arrayFormat: 'none'}),
);
expectType(
- queryString.parseUrl('?foo=bar', {arrayFormat: 'comma'})
+ queryString.parseUrl('?foo=bar', {arrayFormat: 'comma'}),
);
expectType(
- queryString.parseUrl('?foo=1', {parseNumbers: true})
+ queryString.parseUrl('?foo=1', {parseNumbers: true}),
);
expectType(
- queryString.parseUrl('?foo=true', {parseBooleans: true})
+ queryString.parseUrl('?foo=true', {parseBooleans: true}),
);
expectType(
- queryString.parseUrl('?foo=true#bar', {parseFragmentIdentifier: true})
+ queryString.parseUrl('?foo=true#bar', {parseFragmentIdentifier: true}),
);
// Extract
@@ -114,19 +114,19 @@ expectType(
1,
true,
null,
- undefined
+ undefined,
],
fooNumber: 1,
fooBoolean: true,
fooNull: null,
fooUndefined: undefined,
- fooString: 'hi'
+ fooString: 'hi',
},
- })
+ }),
);
// Pick
-expectType(queryString.pick('http://foo.bar/?abc=def&hij=klm', ['abc']))
+expectType(queryString.pick('http://foo.bar/?abc=def&hij=klm', ['abc']));
// Exclude
-expectType(queryString.exclude('http://foo.bar/?abc=def&hij=klm', ['abc']))
+expectType(queryString.exclude('http://foo.bar/?abc=def&hij=klm', ['abc']));
diff --git a/license b/license
index e464bf7..fa7ceba 100644
--- a/license
+++ b/license
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) Sindre Sorhus (http://sindresorhus.com)
+Copyright (c) Sindre Sorhus (https://sindresorhus.com)
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:
diff --git a/package.json b/package.json
index d87d6df..009aa61 100644
--- a/package.json
+++ b/package.json
@@ -10,8 +10,13 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
+ "type": "module",
+ "exports": {
+ "types": "./index.d.ts",
+ "default": "./index.js"
+ },
"engines": {
- "node": ">=6"
+ "node": ">=14.16"
},
"scripts": {
"benchmark": "node benchmark.js",
@@ -19,7 +24,9 @@
},
"files": [
"index.js",
- "index.d.ts"
+ "index.d.ts",
+ "base.js",
+ "base.d.ts"
],
"keywords": [
"browser",
@@ -39,16 +46,20 @@
],
"dependencies": {
"decode-uri-component": "^0.2.2",
- "filter-obj": "^1.1.0",
- "split-on-first": "^1.0.0",
- "strict-uri-encode": "^2.0.0"
+ "filter-obj": "^5.1.0",
+ "split-on-first": "^1.0.0"
},
"devDependencies": {
- "ava": "^1.4.1",
+ "ava": "^5.1.0",
"benchmark": "^2.1.4",
- "deep-equal": "^1.0.1",
- "fast-check": "^1.5.0",
- "tsd": "^0.7.3",
- "xo": "^0.24.0"
+ "deep-equal": "^2.1.0",
+ "fast-check": "^3.4.0",
+ "tsd": "^0.25.0",
+ "xo": "^0.53.1"
+ },
+ "tsd": {
+ "compilerOptions": {
+ "module": "node16"
+ }
}
}
diff --git a/readme.md b/readme.md
index 5721ca0..85d9a71 100644
--- a/readme.md
+++ b/readme.md
@@ -31,15 +31,6 @@
It’s 100% JavaScript, fully customizable, and developer-first.