Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
npm install query-string
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6,766 Stars
262 Commits
453 Forks
31 Watching
1 Branches
72 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (93.75%)
TypeScript (6.25%)
Cumulative downloads
Total Downloads
Last day
-2.3%
2,660,270
Compared to previous day
Last week
3.5%
14,195,012
Compared to previous week
Last month
13.6%
57,899,451
Compared to previous month
Last year
-10%
633,367,773
Compared to previous year
3
6
Parse and stringify URL query strings
1npm install query-string
[!WARNING] Remember the hyphen! Do not install the deprecated
querystring
package!
For browser usage, this package targets the latest version of Chrome, Firefox, and Safari.
1import queryString from 'query-string'; 2 3console.log(location.search); 4//=> '?foo=bar' 5 6const parsed = queryString.parse(location.search); 7console.log(parsed); 8//=> {foo: 'bar'} 9 10console.log(location.hash); 11//=> '#token=bada55cafe' 12 13const parsedHash = queryString.parse(location.hash); 14console.log(parsedHash); 15//=> {token: 'bada55cafe'} 16 17parsed.foo = 'unicorn'; 18parsed.ilike = 'pizza'; 19 20const stringified = queryString.stringify(parsed); 21//=> 'foo=unicorn&ilike=pizza' 22 23location.search = stringified; 24// note that `location.search` automatically prepends a question mark 25console.log(location.search); 26//=> '?foo=unicorn&ilike=pizza'
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)
and thus does not have a prototype
.
Type: object
Type: boolean
Default: true
Decode the keys and values. URL components are decoded with decode-uri-component
.
Type: string
Default: 'none'
'bracket'
: Parse arrays with bracket representation:1import queryString from 'query-string'; 2 3queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); 4//=> {foo: ['1', '2', '3']}
'index'
: Parse arrays with index representation:1import queryString from 'query-string'; 2 3queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); 4//=> {foo: ['1', '2', '3']}
'comma'
: Parse arrays with elements separated by comma:1import queryString from 'query-string'; 2 3queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); 4//=> {foo: ['1', '2', '3']}
'separator'
: Parse arrays with elements separated by a custom character:1import queryString from 'query-string'; 2 3queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); 4//=> {foo: ['1', '2', '3']}
'bracket-separator'
: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:1import queryString from 'query-string'; 2 3queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 4//=> {foo: []} 5 6queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 7//=> {foo: ['']} 8 9queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 10//=> {foo: ['1']} 11 12queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 13//=> {foo: ['1', '2', '3']} 14 15queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 16//=> {foo: ['1', '', 3, '', '', '6']} 17 18queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 19//=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
'colon-list-separator'
: Parse arrays with parameter names that are explicitly marked with :list
:1import queryString from 'query-string'; 2 3queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'}); 4//=> {foo: ['one', 'two']}
'none'
: Parse arrays with elements using duplicate keys:1import queryString from 'query-string'; 2 3queryString.parse('foo=1&foo=2&foo=3'); 4//=> {foo: ['1', '2', '3']}
Type: string
Default: ','
The character used to separate array elements when using {arrayFormat: 'separator'}
.
Type: Function | boolean
Default: true
Supports both Function
as a custom sorting function or false
to disable sorting.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.parse('foo=1', {parseNumbers: true}); 4//=> {foo: 1}
Parse the value as a number type instead of string type if it's a number.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.parse('foo=true', {parseBooleans: true}); 4//=> {foo: true}
Parse the value as a boolean type instead of string type if it's a boolean.
Type: object
Default: {}
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
.
Use this feature to override the type of a value. This can be useful when the type is ambiguous such as a phone number.
It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value.
Supported Types:
'string'
: Parse phoneNumber
as a string (overriding the parseNumbers
option):1import queryString from 'query-string'; 2 3queryString.parse('?phoneNumber=%2B380951234567&id=1', { 4 parseNumbers: true, 5 types: { 6 phoneNumber: 'string', 7 } 8}); 9//=> {phoneNumber: '+380951234567', id: 1}
'number'
: Parse age
as a number (even when parseNumbers
is false):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: 'number', 6 } 7}); 8//=> {age: 20, id: '01234', zipcode: '90210 }
'string[]'
: Parse items
as an array of strings (overriding the parseNumbers
option):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&items=1%2C2%2C3', { 4 parseNumbers: true, 5 types: { 6 items: 'string[]', 7 } 8}); 9//=> {age: 20, items: ['1', '2', '3']}
'number[]'
: Parse items
as an array of numbers (even when parseNumbers
is false):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&items=1%2C2%2C3', { 4 types: { 5 items: 'number[]', 6 } 7}); 8//=> {age: '20', items: [1, 2, 3]}
'Function'
: Provide a custom function as the parameter type. The parameter's value will equal the function's return value.1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: (value) => value * 2, 6 } 7}); 8//=> {age: 40, id: '01234', zipcode: '90210 }
NOTE: Array types (string[]
and number[]
) will have no effect if arrayFormat
is set to none
.
1queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', { 2 arrayFormat: 'none', 3 types: { 4 ids: 'number[]', 5 foods: 'string[]', 6 }, 7} 8//=> {ids:'001,002,003', foods:'apple,orange,mango'}
1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: (value) => value * 2, 6 } 7}); 8//=> {age: 40, id: '01234', zipcode: '90210 }
Parse the value as a boolean type instead of string type if it's a boolean.
Stringify an object into a query string and sorting the keys.
Type: object
Type: boolean
Default: true
Strictly encode URI components. It uses encodeURIComponent if set to false. You probably don't care about this option.
Type: boolean
Default: true
URL encode the keys and values.
Type: string
Default: 'none'
'bracket'
: Serialize arrays using bracket representation:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); 4//=> 'foo[]=1&foo[]=2&foo[]=3'
'index'
: Serialize arrays using index representation:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); 4//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
'comma'
: Serialize arrays by separating elements with comma:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); 4//=> 'foo=1,2,3' 5 6queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'}); 7//=> 'foo=1,,' 8// Note that typing information for null values is lost 9// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
'separator'
: Serialize arrays by separating elements with a custom character:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'}); 4//=> 'foo=1|2|3'
'bracket-separator'
: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:1import queryString from 'query-string'; 2 3queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 4//=> 'foo[]' 5 6queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 7//=> 'foo[]=' 8 9queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 10//=> 'foo[]=1' 11 12queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 13//=> 'foo[]=1|2|3' 14 15queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 16//=> 'foo[]=1||3|||6' 17 18queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true}); 19//=> 'foo[]=1||3|6' 20 21queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 22//=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
'colon-list-separator'
: Serialize arrays with parameter names that are explicitly marked with :list
:1import queryString from 'query-string'; 2 3queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'}); 4//=> 'foo:list=one&foo:list=two'
'none'
: Serialize arrays by using duplicate keys:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}); 4//=> 'foo=1&foo=2&foo=3'
Type: string
Default: ','
The character used to separate array elements when using {arrayFormat: 'separator'}
.
Type: Function | boolean
Supports both Function
as a custom sorting function or false
to disable sorting.
1import queryString from 'query-string'; 2 3const order = ['c', 'a', 'b']; 4 5queryString.stringify({a: 1, b: 2, c: 3}, { 6 sort: (a, b) => order.indexOf(a) - order.indexOf(b) 7}); 8//=> 'c=3&a=1&b=2'
1import queryString from 'query-string'; 2 3queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); 4//=> 'b=1&c=2&a=3'
If omitted, keys are sorted using Array#sort()
, which means, converting them to strings and comparing strings in Unicode code point order.
Skip keys with null
as the value.
Note that keys with undefined
as the value are always skipped.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.stringify({a: 1, b: undefined, c: null, d: 4}, { 4 skipNull: true 5}); 6//=> 'a=1&d=4'
1import queryString from 'query-string'; 2 3queryString.stringify({a: undefined, b: null}, { 4 skipNull: true 5}); 6//=> ''
Skip keys with an empty string as the value.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.stringify({a: 1, b: '', c: '', d: 4}, { 4 skipEmptyString: true 5}); 6//=> 'a=1&d=4'
1import queryString from 'query-string'; 2 3queryString.stringify({a: '', b: ''}, { 4 skipEmptyString: true 5}); 6//=> ''
Extract a query string from a URL that can be passed into .parse()
.
Note: This behaviour can be changed with the skipNull
option.
Extract the URL and the query string as an object.
Returns an object with a url
and query
property.
If the parseFragmentIdentifier
option is true
, the object will also contain a fragmentIdentifier
property.
1import queryString from 'query-string';
2
3queryString.parseUrl('https://foo.bar?foo=bar');
4//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
5
6queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
7//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
Type: object
The options are the same as for .parse()
.
Extra options are as below.
Parse the fragment identifier from the URL.
Type: boolean
Default: false
1import queryString from 'query-string';
2
3queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
4//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
Stringify an object into a URL with a query string and sorting the keys. The inverse of .parseUrl()
The options
are the same as for .stringify()
.
Returns a string with the URL and a query string.
Query items in the query
property overrides queries in the url
property.
The fragmentIdentifier
property overrides the fragment identifier in the url
property.
1queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
2//=> 'https://foo.bar?foo=bar'
3
4queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
5//=> 'https://foo.bar?foo=bar'
6
7queryString.stringifyUrl({
8 url: 'https://foo.bar',
9 query: {
10 top: 'foo'
11 },
12 fragmentIdentifier: 'bar'
13});
14//=> 'https://foo.bar?top=foo#bar'
Type: object
Type: string
The URL to stringify.
Type: object
Query items to add to the URL.
Pick query parameters from a URL.
Returns a string with the new URL.
1import queryString from 'query-string'; 2 3queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']); 4//=> 'https://foo.bar?foo=1#hello' 5 6queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true}); 7//=> 'https://foo.bar?bar=2#hello'
Exclude query parameters from a URL.
Returns a string with the new URL.
1import queryString from 'query-string'; 2 3queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']); 4//=> 'https://foo.bar?bar=2#hello' 5 6queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true}); 7//=> 'https://foo.bar?foo=1#hello'
Type: string
The URL containing the query parameters to filter.
Type: string[]
The names of the query parameters to filter based on the function used.
Type: (key, value) => boolean
A filter predicate that will be provided the name of each query parameter and its value. The parseNumbers
and parseBooleans
options also affect value
.
Type: object
Parse options and stringify options.
This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of edge cases.
You're much better off just converting the object to a JSON string:
1import queryString from 'query-string'; 2 3queryString.stringify({ 4 foo: 'bar', 5 nested: JSON.stringify({ 6 unicorn: 'cake' 7 }) 8}); 9//=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
However, there is support for multiple instances of the same key:
1import queryString from 'query-string'; 2 3queryString.parse('likes=cake&name=bob&likes=icecream'); 4//=> {likes: ['cake', 'icecream'], name: 'bob'} 5 6queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'}); 7//=> 'color=taupe&color=chartreuse&id=515'
Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
1import queryString from 'query-string'; 2 3queryString.stringify({foo: false}); 4//=> 'foo=false' 5 6queryString.stringify({foo: null}); 7//=> 'foo' 8 9queryString.stringify({foo: undefined}); 10//=> ''
+
as a space?See this answer.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
project is fuzzed
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
4 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More