query-string
Advanced tools
Comparing version 6.3.0 to 6.4.0
@@ -10,29 +10,33 @@ export interface ParseOptions { | ||
/** | ||
* Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation. | ||
* | ||
* @default 'none' | ||
* | ||
* - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as: | ||
* - `bracket`: Parse arrays with bracket representation: | ||
* | ||
* | ||
* queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); | ||
* //=> foo: [1,2,3] | ||
* //=> foo: [1, 2, 3] | ||
* | ||
* - `index`: stands for parsing taking the index into account, such as: | ||
* - `index`: Parse arrays with index representation: | ||
* | ||
* | ||
* queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); | ||
* //=> foo: [1,2,3] | ||
* //=> foo: [1, 2, 3] | ||
* | ||
* - `none`: is the **default** option and removes any bracket representation, such as: | ||
* - `comma`: Parse arrays with elements separated by comma: | ||
* | ||
* | ||
* queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); | ||
* //=> foo: [1, 2, 3] | ||
* | ||
* - `none`: Parse arrays with elements using duplicate keys: | ||
* | ||
* | ||
* queryString.parse('foo=1&foo=2&foo=3'); | ||
* //=> foo: [1,2,3] | ||
* //=> foo: [1, 2, 3] | ||
*/ | ||
readonly arrayFormat?: 'bracket' | 'index' | 'none'; | ||
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none'; | ||
} | ||
export interface ParsedQuery { | ||
readonly [key: string]: string | string[] | undefined; | ||
readonly [key: string]: string | string[] | null | undefined; | ||
} | ||
@@ -82,25 +86,29 @@ | ||
/** | ||
* Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation. | ||
* | ||
* @default 'none' | ||
* | ||
* - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as: | ||
* - `bracket`: Serialize arrays using bracket representation: | ||
* | ||
* | ||
* queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'bracket'}); | ||
* // => foo[]=1&foo[]=2&foo[]=3 | ||
* queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); | ||
* //=> 'foo[]=1&foo[]=2&foo[]=3' | ||
* | ||
* - `index`: stands for parsing taking the index into account, such as: | ||
* - `index`: Serialize arrays using index representation: | ||
* | ||
* | ||
* queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'index'}); | ||
* // => foo[0]=1&foo[1]=2&foo[3]=3 | ||
* queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); | ||
* //=> 'foo[0]=1&foo[1]=2&foo[3]=3' | ||
* | ||
* - `none`: is the **default** option and removes any bracket representation, such as: | ||
* - `comma`: Serialize arrays by separating elements with comma: | ||
* | ||
* | ||
* queryString.stringify({foo: [1,2,3]}); | ||
* // => foo=1&foo=2&foo=3 | ||
* queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); | ||
* //=> 'foo=1,2,3' | ||
* | ||
* - `none`: Serialize arrays by using duplicate keys: | ||
* | ||
* | ||
* queryString.stringify({foo: [1, 2, 3]}); | ||
* //=> 'foo=1&foo=2&foo=3' | ||
*/ | ||
readonly arrayFormat?: 'bracket' | 'index' | 'none'; | ||
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none'; | ||
@@ -115,8 +123,8 @@ /** | ||
* const order = ['c', 'a', 'b']; | ||
* queryString.stringify({ a: 1, b: 2, c: 3}, { | ||
* sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight) | ||
* queryString.stringify({a: 1, b: 2, c: 3}, { | ||
* sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight) | ||
* }); | ||
* // => 'c=3&a=1&b=2' | ||
* | ||
* queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false}); | ||
* queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); | ||
* // => 'b=1&c=2&a=3' | ||
@@ -128,3 +136,3 @@ */ | ||
/** | ||
* Stringify an object into a query string, sorting the keys. | ||
* Stringify an object into a query string and sorting the keys. | ||
*/ | ||
@@ -131,0 +139,0 @@ export function stringify( |
106
index.js
@@ -8,33 +8,55 @@ 'use strict'; | ||
case 'index': | ||
return (key, value, index) => { | ||
return value === null ? [ | ||
encode(key, options), | ||
'[', | ||
index, | ||
']' | ||
].join('') : [ | ||
encode(key, options), | ||
'[', | ||
encode(index, options), | ||
']=', | ||
encode(value, options) | ||
].join(''); | ||
return key => (result, value) => { | ||
const index = result.length; | ||
if (value === undefined) { | ||
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, value) => { | ||
return value === null ? [encode(key, options), '[]'].join('') : [ | ||
encode(key, options), | ||
'[]=', | ||
encode(value, options) | ||
].join(''); | ||
return key => (result, value) => { | ||
if (value === undefined) { | ||
return result; | ||
} | ||
if (value === null) { | ||
return [...result, [encode(key, options), '[]'].join('')]; | ||
} | ||
return [...result, [encode(key, options), '[]=', encode(value, options)].join('')]; | ||
}; | ||
case 'comma': | ||
return key => (result, value, index) => { | ||
if (!value) { | ||
return result; | ||
} | ||
if (index === 0) { | ||
return [[encode(key, options), '=', encode(value, options)].join('')]; | ||
} | ||
return [[result, encode(value, options)].join(',')]; | ||
}; | ||
default: | ||
return (key, value) => { | ||
return value === null ? encode(key, options) : [ | ||
encode(key, options), | ||
'=', | ||
encode(value, options) | ||
].join(''); | ||
return key => (result, value) => { | ||
if (value === undefined) { | ||
return result; | ||
} | ||
if (value === null) { | ||
return [...result, encode(key, options)]; | ||
} | ||
return [...result, [encode(key, options), '=', encode(value, options)].join('')]; | ||
}; | ||
@@ -84,2 +106,9 @@ } | ||
case 'comma': | ||
return (key, value, accumulator) => { | ||
const isArray = typeof value === 'string' && value.split('').indexOf(',') > -1; | ||
const newValue = isArray ? value.split(',') : value; | ||
accumulator[key] = newValue; | ||
}; | ||
default: | ||
@@ -137,3 +166,6 @@ return (key, value, accumulator) => { | ||
function parse(input, options) { | ||
options = Object.assign({decode: true, arrayFormat: 'none'}, options); | ||
options = Object.assign({ | ||
decode: true, | ||
arrayFormat: 'none' | ||
}, options); | ||
@@ -181,4 +213,4 @@ const formatter = parserForArrayFormat(options); | ||
exports.stringify = (obj, options) => { | ||
if (!obj) { | ||
exports.stringify = (object, options) => { | ||
if (!object) { | ||
return ''; | ||
@@ -194,3 +226,3 @@ } | ||
const formatter = encoderForArrayFormat(options); | ||
const keys = Object.keys(obj); | ||
const keys = Object.keys(object); | ||
@@ -202,3 +234,3 @@ if (options.sort !== false) { | ||
return keys.map(key => { | ||
const value = obj[key]; | ||
const value = object[key]; | ||
@@ -214,13 +246,5 @@ if (value === undefined) { | ||
if (Array.isArray(value)) { | ||
const result = []; | ||
for (const value2 of value.slice()) { | ||
if (value2 === undefined) { | ||
continue; | ||
} | ||
result.push(formatter(key, value2, result.length)); | ||
} | ||
return result.join('&'); | ||
return value | ||
.reduce(formatter(key), []) | ||
.join('&'); | ||
} | ||
@@ -227,0 +251,0 @@ |
{ | ||
"name": "query-string", | ||
"version": "6.3.0", | ||
"version": "6.4.0", | ||
"description": "Parse and stringify URL query strings", | ||
@@ -42,3 +42,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"ava": "^1.2.1", | ||
"ava": "^1.3.1", | ||
"deep-equal": "^1.0.1", | ||
@@ -45,0 +45,0 @@ "fast-check": "^1.5.0", |
106
readme.md
@@ -5,9 +5,3 @@ # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string) | ||
--- | ||
<p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.<br>Also check out his <a href="https://LearnNode.com/friend/AWESOME">Node.js</a>, <a href="https://ReactForBeginners.com/friend/AWESOME">React</a>, <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> courses.</p> | ||
--- | ||
## Install | ||
@@ -60,3 +54,3 @@ | ||
### .parse(*string*, *[options]*) | ||
### .parse(string, [options]) | ||
@@ -67,50 +61,62 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly. | ||
#### decode | ||
#### options | ||
Type: `Object` | ||
##### decode | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component). | ||
Decode the keys and values. URL components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component). | ||
#### arrayFormat | ||
##### arrayFormat | ||
Type: `string`<br> | ||
Default: `'none'` | ||
Default: `none` | ||
Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation. | ||
- `bracket`: Parse arrays with bracket representation: | ||
- `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as: | ||
```js | ||
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); | ||
//=> foo: [1,2,3] | ||
//=> foo: [1, 2, 3] | ||
``` | ||
- `index`: stands for parsing taking the index into account, such as: | ||
- `index`: Parse arrays with index representation: | ||
```js | ||
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); | ||
//=> foo: [1,2,3] | ||
//=> foo: [1, 2, 3] | ||
``` | ||
- `none`: is the **default** option and removes any bracket representation, such as: | ||
- `comma`: Parse arrays with elements separated by comma: | ||
```js | ||
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); | ||
//=> foo: [1, 2, 3] | ||
``` | ||
- `none`: Parse arrays with elements using duplicate keys: | ||
```js | ||
queryString.parse('foo=1&foo=2&foo=3'); | ||
//=> foo: [1,2,3] | ||
//=> foo: [1, 2, 3] | ||
``` | ||
### .stringify(*object*, *[options]*) | ||
### .stringify(object, [options]) | ||
Stringify an object into a query string, sorting the keys. | ||
Stringify an object into a query string and sorting the keys. | ||
#### strict | ||
#### options | ||
Type: `Object` | ||
##### strict | ||
Type: `boolean`<br> | ||
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. | ||
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 | ||
##### encode | ||
@@ -122,33 +128,38 @@ Type: `boolean`<br> | ||
#### arrayFormat | ||
##### arrayFormat | ||
Type: `string`<br> | ||
Default: `'none'` | ||
Default: `none` | ||
Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation. | ||
- `bracket`: Serialize arrays using bracket representation: | ||
- `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as: | ||
```js | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); | ||
//=> 'foo[]=1&foo[]=2&foo[]=3' | ||
``` | ||
- `index`: Serialize arrays using index representation: | ||
```js | ||
queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'bracket'}); | ||
// => foo[]=1&foo[]=2&foo[]=3 | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); | ||
//=> 'foo[0]=1&foo[1]=2&foo[3]=3' | ||
``` | ||
- `index`: stands for parsing taking the index into account, such as: | ||
- `comma`: Serialize arrays by separating elements with comma: | ||
```js | ||
queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'index'}); | ||
// => foo[0]=1&foo[1]=2&foo[3]=3 | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); | ||
//=> 'foo=1,2,3' | ||
``` | ||
- `none`: is the __default__ option and removes any bracket representation, such as: | ||
- `none`: Serialize arrays by using duplicate keys: | ||
```js | ||
queryString.stringify({foo: [1,2,3]}); | ||
// => foo=1&foo=2&foo=3 | ||
queryString.stringify({foo: [1, 2, 3]}); | ||
//=> 'foo=1&foo=2&foo=3' | ||
``` | ||
#### sort | ||
##### sort | ||
Type: `Function` `boolean` | ||
Type: `Function | boolean` | ||
@@ -159,20 +170,21 @@ Supports both `Function` as a custom sorting function or `false` to disable sorting. | ||
const order = ['c', 'a', 'b']; | ||
queryString.stringify({ a: 1, b: 2, c: 3}, { | ||
sort: (m, n) => order.indexOf(m) - order.indexOf(n) | ||
queryString.stringify({a: 1, b: 2, c: 3}, { | ||
sort: (a, b) => order.indexOf(a) - order.indexOf(b) | ||
}); | ||
// => 'c=3&a=1&b=2' | ||
//=> 'c=3&a=1&b=2' | ||
``` | ||
```js | ||
queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false}); | ||
// => 'b=1&c=2&a=3' | ||
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. | ||
If omitted, keys are sorted using `Array#sort()`, which means, converting them to strings and comparing strings in Unicode code point order. | ||
### .extract(*string*) | ||
### .extract(string) | ||
Extract a query string from a URL that can be passed into `.parse()`. | ||
### .parseUrl(*string*, *[options]*) | ||
### .parseUrl(string, [options]) | ||
@@ -179,0 +191,0 @@ Extract the URL and the query string as an object. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17661
335
244