diff options
Diffstat (limited to 'node_modules/query-string')
-rw-r--r-- | node_modules/query-string/index.d.ts | 191 | ||||
-rw-r--r-- | node_modules/query-string/index.js | 144 | ||||
-rw-r--r-- | node_modules/query-string/license | 2 | ||||
-rw-r--r-- | node_modules/query-string/package.json | 28 | ||||
-rw-r--r-- | node_modules/query-string/readme.md | 243 |
5 files changed, 539 insertions, 69 deletions
diff --git a/node_modules/query-string/index.d.ts b/node_modules/query-string/index.d.ts index a5bd661c2..b3a3c99af 100644 --- a/node_modules/query-string/index.d.ts +++ b/node_modules/query-string/index.d.ts @@ -12,6 +12,8 @@ export interface ParseOptions { - `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']} ``` @@ -19,6 +21,8 @@ export interface ParseOptions { - `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']} ``` @@ -26,18 +30,38 @@ export interface ParseOptions { - `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']} + ``` + - `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' | 'none'; + readonly arrayFormat?: 'bracket' | 'index' | 'comma' | '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. @@ -48,16 +72,22 @@ export interface ParseOptions { @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'} + //=> {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'} + //=> {a: 'one', c: 'three', b: 'two'} ``` */ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false; @@ -68,7 +98,9 @@ export interface ParseOptions { @default false @example - ```js + ``` + import queryString = require('query-string'); + queryString.parse('foo=1', {parseNumbers: true}); //=> {foo: 1} ``` @@ -82,11 +114,28 @@ export interface ParseOptions { @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<T = string> { @@ -108,17 +157,31 @@ 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; @@ -144,6 +207,8 @@ export interface StringifyOptions { - `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' ``` @@ -151,6 +216,8 @@ export interface StringifyOptions { - `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' ``` @@ -158,18 +225,38 @@ export interface StringifyOptions { - `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' ``` + - `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' + ``` + - `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' | 'none'; + readonly arrayFormat?: 'bracket' | 'index' | 'comma' | '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. @@ -180,18 +267,76 @@ export interface StringifyOptions { @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' + //=> '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' + //=> '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; } /** @@ -204,5 +349,37 @@ export function stringify( /** 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; + +/** +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: ParsedUrl, + options?: StringifyOptions +): string; diff --git a/node_modules/query-string/index.js b/node_modules/query-string/index.js index 35c13e1a6..de1d7b182 100644 --- a/node_modules/query-string/index.js +++ b/node_modules/query-string/index.js @@ -3,12 +3,19 @@ const strictUriEncode = require('strict-uri-encode'); const decodeComponent = require('decode-uri-component'); const splitOnFirst = require('split-on-first'); +const isNullOrUndefined = value => value === null || value === undefined; + function encoderForArrayFormat(options) { switch (options.arrayFormat) { case 'index': return key => (result, value) => { const index = result.length; - if (value === undefined) { + + if ( + value === undefined || + (options.skipNull && value === null) || + (options.skipEmptyString && value === '') + ) { return result; } @@ -24,7 +31,11 @@ function encoderForArrayFormat(options) { case 'bracket': return key => (result, value) => { - if (value === undefined) { + if ( + value === undefined || + (options.skipNull && value === null) || + (options.skipEmptyString && value === '') + ) { return result; } @@ -36,21 +47,26 @@ function encoderForArrayFormat(options) { }; case 'comma': - return key => (result, value, index) => { + case 'separator': + return key => (result, value) => { if (value === null || value === undefined || value.length === 0) { return result; } - if (index === 0) { + if (result.length === 0) { return [[encode(key, options), '=', encode(value, options)].join('')]; } - return [[result, encode(value, options)].join(',')]; + return [[result, encode(value, options)].join(options.arrayFormatSeparator)]; }; default: return key => (result, value) => { - if (value === undefined) { + if ( + value === undefined || + (options.skipNull && value === null) || + (options.skipEmptyString && value === '') + ) { return result; } @@ -104,9 +120,10 @@ function parserForArrayFormat(options) { }; case 'comma': + case 'separator': return (key, value, accumulator) => { - const isArray = typeof value === 'string' && value.split('').indexOf(',') > -1; - const newValue = isArray ? value.split(',') : value; + const isArray = typeof value === 'string' && value.split('').indexOf(options.arrayFormatSeparator) > -1; + const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options); accumulator[key] = newValue; }; @@ -122,6 +139,12 @@ function parserForArrayFormat(options) { } } +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); @@ -161,6 +184,16 @@ function removeHash(input) { 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('?'); @@ -171,15 +204,28 @@ function extract(input) { 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(input, 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 @@ -196,19 +242,23 @@ function parse(input, options) { } for (const param of input.split('&')) { - let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '='); + 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 : decode(value, options); + value = value === undefined ? null : ['comma', 'separator'].includes(options.arrayFormat) ? value : decode(value, options); + formatter(decode(key, options), value, ret); + } - 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'; + 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); } - - formatter(decode(key, options), value, ret); } if (options.sort === false) { @@ -239,11 +289,28 @@ exports.stringify = (object, options) => { options = Object.assign({ encode: true, strict: true, - arrayFormat: 'none' + arrayFormat: 'none', + arrayFormatSeparator: ',' }, options); + validateArrayFormatSeparator(options.arrayFormatSeparator); + + const shouldFilter = key => ( + (options.skipNull && isNullOrUndefined(object[key])) || + (options.skipEmptyString && object[key] === '') + ); + const formatter = encoderForArrayFormat(options); - const keys = Object.keys(object); + + 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); @@ -271,8 +338,41 @@ exports.stringify = (object, options) => { }; exports.parseUrl = (input, options) => { - return { - url: removeHash(input).split('?')[0] || '', - query: parse(extract(input), options) - }; + options = Object.assign({ + decode: true + }, options); + + const [url, hash] = splitOnFirst(input, '#'); + + return Object.assign( + { + url: url.split('?')[0] || '', + query: parse(extract(input), options) + }, + options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {} + ); +}; + +exports.stringifyUrl = (input, options) => { + options = Object.assign({ + encode: true, + strict: true + }, options); + + const url = removeHash(input.url).split('?')[0] || ''; + const queryFromUrl = exports.extract(input.url); + const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false}); + + const query = Object.assign(parsedQueryFromUrl, input.query); + let queryString = exports.stringify(query, options); + if (queryString) { + queryString = `?${queryString}`; + } + + let hash = getHash(input.url); + if (input.fragmentIdentifier) { + hash = `#${encode(input.fragmentIdentifier, options)}`; + } + + return `${url}${queryString}${hash}`; }; diff --git a/node_modules/query-string/license b/node_modules/query-string/license index e7af2f771..e464bf785 100644 --- a/node_modules/query-string/license +++ b/node_modules/query-string/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (http://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/node_modules/query-string/package.json b/node_modules/query-string/package.json index 4df6b20ef..d37a6d341 100644 --- a/node_modules/query-string/package.json +++ b/node_modules/query-string/package.json @@ -1,32 +1,31 @@ { - "_from": "query-string@6.8.2", - "_id": "query-string@6.8.2", + "_from": "query-string@^6.8.2", + "_id": "query-string@6.13.1", "_inBundle": false, - "_integrity": "sha512-J3Qi8XZJXh93t2FiKyd/7Ec6GNifsjKXUsVFkSBj/kjLsDylWhnCz4NT1bkPcKotttPW+QbKGqqPH8OoI2pdqw==", + "_integrity": "sha512-RfoButmcK+yCta1+FuU8REvisx1oEzhMKwhLUNcepQTPGcNMp1sIqjnfCtfnvGSQZQEhaBHvccujtWoUV3TTbA==", "_location": "/query-string", "_phantomChildren": {}, "_requested": { - "type": "version", + "type": "range", "registry": true, - "raw": "query-string@6.8.2", + "raw": "query-string@^6.8.2", "name": "query-string", "escapedName": "query-string", - "rawSpec": "6.8.2", + "rawSpec": "^6.8.2", "saveSpec": null, - "fetchSpec": "6.8.2" + "fetchSpec": "^6.8.2" }, "_requiredBy": [ - "#USER", "/" ], - "_resolved": "https://registry.npmjs.org/query-string/-/query-string-6.8.2.tgz", - "_shasum": "36cb7e452ae11a4b5e9efee83375e0954407b2f6", - "_spec": "query-string@6.8.2", + "_resolved": "https://registry.npmjs.org/query-string/-/query-string-6.13.1.tgz", + "_shasum": "d913ccfce3b4b3a713989fe6d39466d92e71ccad", + "_spec": "query-string@^6.8.2", "_where": "/Users/isaacs/dev/npm/cli", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "bugs": { "url": "https://github.com/sindresorhus/query-string/issues" @@ -41,6 +40,7 @@ "description": "Parse and stringify URL query strings", "devDependencies": { "ava": "^1.4.1", + "benchmark": "^2.1.4", "deep-equal": "^1.0.1", "fast-check": "^1.5.0", "tsd": "^0.7.3", @@ -53,6 +53,7 @@ "index.js", "index.d.ts" ], + "funding": "https://github.com/sponsors/sindresorhus", "homepage": "https://github.com/sindresorhus/query-string#readme", "keywords": [ "browser", @@ -76,7 +77,8 @@ "url": "git+https://github.com/sindresorhus/query-string.git" }, "scripts": { + "benchmark": "node benchmark.js", "test": "xo && ava && tsd" }, - "version": "6.8.2" + "version": "6.13.1" } diff --git a/node_modules/query-string/readme.md b/node_modules/query-string/readme.md index 297a47d5a..46e58b5b0 100644 --- a/node_modules/query-string/readme.md +++ b/node_modules/query-string/readme.md @@ -1,7 +1,30 @@ -# query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string) +# query-string [![Build Status](https://travis-ci.com/sindresorhus/query-string.svg?branch=master)](https://travis-ci.com/github/sindresorhus/query-string) [![](https://badgen.net/bundlephobia/minzip/query-string)](https://bundlephobia.com/result?p=query-string) > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string) +<br> + +--- + +<div align="center"> + <p> + <p> + <sup> + <a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a> + </sup> + </p> + <sup>Special thanks to:</sup> + <br> + <br> + <a href="https://standardresume.co"> + <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="200"/> + </a> + </p> +</div> + +--- + +<br> ## Install @@ -9,8 +32,7 @@ $ npm install query-string ``` -This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app v1](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`. - +This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, if your project is using create-react-app v1, use version 5: `npm install query-string@5`. ## Usage @@ -43,7 +65,6 @@ console.log(location.search); //=> '?foo=unicorn&ilike=pizza' ``` - ## API ### .parse(string, options?) @@ -58,19 +79,21 @@ Type: `object` ##### decode -Type: `boolean`<br> +Type: `boolean`\ Default: `true` Decode the keys and values. URL components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component). ##### arrayFormat -Type: `string`<br> +Type: `string`\ Default: `'none'` - `'bracket'`: Parse arrays with bracket representation: ```js +const queryString = require('query-string'); + queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); //=> {foo: ['1', '2', '3']} ``` @@ -78,6 +101,8 @@ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); - `'index'`: Parse arrays with index representation: ```js +const queryString = require('query-string'); + queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); //=> {foo: ['1', '2', '3']} ``` @@ -85,30 +110,52 @@ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); - `'comma'`: Parse arrays with elements separated by comma: ```js +const 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: + +```js +const queryString = require('query-string'); + +queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); +//=> {foo: ['1', '2', '3']} +``` + - `'none'`: Parse arrays with elements using duplicate keys: ```js +const queryString = require('query-string'); + queryString.parse('foo=1&foo=2&foo=3'); //=> {foo: ['1', '2', '3']} ``` +##### arrayFormatSeparator + +Type: `string`\ +Default: `','` + +The character used to separate array elements when using `{arrayFormat: 'separator'}`. + ##### sort -Type: `Function | boolean`<br> +Type: `Function | boolean`\ Default: `true` Supports both `Function` as a custom sorting function or `false` to disable sorting. ##### parseNumbers -Type: `boolean`<br> +Type: `boolean`\ Default: `false` ```js +const queryString = require('query-string'); + queryString.parse('foo=1', {parseNumbers: true}); //=> {foo: 1} ``` @@ -117,17 +164,19 @@ Parse the value as a number type instead of string type if it's a number. ##### parseBooleans -Type: `boolean`<br> +Type: `boolean`\ Default: `false` ```js +const queryString = require('query-string'); + queryString.parse('foo=true', {parseBooleans: true}); //=> {foo: true} ``` Parse the value as a boolean type instead of string type if it's a boolean. -### .stringify(object, [options]) +### .stringify(object, options?) Stringify an object into a query string and sorting the keys. @@ -137,26 +186,28 @@ Type: `object` ##### strict -Type: `boolean`<br> +Type: `boolean`\ Default: `true` 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. ##### encode -Type: `boolean`<br> +Type: `boolean`\ Default: `true` [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values. ##### arrayFormat -Type: `string`<br> +Type: `string`\ Default: `'none'` - `'bracket'`: Serialize arrays using bracket representation: ```js +const queryString = require('query-string'); + queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); //=> 'foo[]=1&foo[]=2&foo[]=3' ``` @@ -164,6 +215,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); - `'index'`: Serialize arrays using index representation: ```js +const queryString = require('query-string'); + queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); //=> 'foo[0]=1&foo[1]=2&foo[2]=3' ``` @@ -171,6 +224,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); - `'comma'`: Serialize arrays by separating elements with comma: ```js +const queryString = require('query-string'); + queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); //=> 'foo=1,2,3' ``` @@ -178,10 +233,19 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); - `'none'`: Serialize arrays by using duplicate keys: ```js +const queryString = require('query-string'); + queryString.stringify({foo: [1, 2, 3]}); //=> 'foo=1&foo=2&foo=3' ``` +##### arrayFormatSeparator + +Type: `string`\ +Default: `','` + +The character used to separate array elements when using `{arrayFormat: 'separator'}`. + ##### sort Type: `Function | boolean` @@ -189,6 +253,8 @@ Type: `Function | boolean` Supports both `Function` as a custom sorting function or `false` to disable sorting. ```js +const queryString = require('query-string'); + const order = ['c', 'a', 'b']; queryString.stringify({a: 1, b: 2, c: 3}, { @@ -198,29 +264,156 @@ queryString.stringify({a: 1, b: 2, c: 3}, { ``` ```js +const queryString = require('query-string'); + queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); //=> '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. +##### skipNull + +Skip keys with `null` as the value. + +Note that keys with `undefined` as the value are always skipped. + +Type: `boolean`\ +Default: `false` + +```js +const queryString = require('query-string'); + +queryString.stringify({a: 1, b: undefined, c: null, d: 4}, { + skipNull: true +}); +//=> 'a=1&d=4' +``` + +```js +const queryString = require('query-string'); + +queryString.stringify({a: undefined, b: null}, { + skipNull: true +}); +//=> '' +``` + +##### skipEmptyString + +Skip keys with an empty string as the value. + +Type: `boolean`\ +Default: `false` + +```js +const queryString = require('query-string'); + +queryString.stringify({a: 1, b: '', c: '', d: 4}, { + skipEmptyString: true +}); +//=> 'a=1&d=4' +``` + +```js +const queryString = require('query-string'); + +queryString.stringify({a: '', b: ''}, { + skipEmptyString: true +}); +//=> '' +``` + ### .extract(string) Extract a query string from a URL that can be passed into `.parse()`. +Note: This behaviour can be changed with the `skipNull` option. + ### .parseUrl(string, options?) Extract the URL and the query string as an object. -The `options` are the same as for `.parse()`. - Returns an object with a `url` and `query` property. +If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property. + ```js +const 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'} +``` + +#### options + +Type: `object` + +The options are the same as for `.parse()`. + +Extra options are as below. + +##### parseFragmentIdentifier + +Parse the fragment identifier from the URL. + +Type: `boolean`\ +Default: `false` + +```js +const queryString = require('query-string'); + +queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true}); +//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'} ``` +### .stringifyUrl(object, options?) + +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) + +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. + +```js +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' +``` + +#### object + +Type: `object` + +##### url + +Type: `string` + +The URL to stringify. + +##### query + +Type: `object` + +Query items to add to the URL. ## Nesting @@ -229,6 +422,8 @@ This module intentionally doesn't support nesting as it's not spec'd and varies You're much better off just converting the object to a JSON string: ```js +const queryString = require('query-string'); + queryString.stringify({ foo: 'bar', nested: JSON.stringify({ @@ -241,6 +436,8 @@ queryString.stringify({ However, there is support for multiple instances of the same key: ```js +const queryString = require('query-string'); + queryString.parse('likes=cake&name=bob&likes=icecream'); //=> {likes: ['cake', 'icecream'], name: 'bob'} @@ -248,12 +445,13 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'}); //=> 'color=taupe&color=chartreuse&id=515' ``` - ## Falsy values 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: ```js +const queryString = require('query-string'); + queryString.stringify({foo: false}); //=> 'foo=false' @@ -264,15 +462,8 @@ queryString.stringify({foo: undefined}); //=> '' ``` +## query-string for enterprise ---- +Available as part of the Tidelift Subscription. -<div align="center"> - <b> - <a href="https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> - </b> - <br> - <sub> - Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. - </sub> -</div> +The maintainers of query-string and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) |