query-string
Advanced tools
Comparing version 6.8.3 to 6.9.0
@@ -15,2 +15,4 @@ export interface ParseOptions { | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); | ||
@@ -23,2 +25,4 @@ //=> {foo: ['1', '2', '3']} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); | ||
@@ -31,2 +35,4 @@ //=> {foo: ['1', '2', '3']} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); | ||
@@ -39,2 +45,4 @@ //=> {foo: ['1', '2', '3']} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo=1&foo=2&foo=3'); | ||
@@ -55,2 +63,4 @@ //=> {foo: ['1', '2', '3']} | ||
``` | ||
import queryString = require('query-string'); | ||
const order = ['c', 'a', 'b']; | ||
@@ -64,2 +74,6 @@ | ||
@example | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('?a=one&c=three&b=two', {sort: false}); | ||
@@ -77,3 +91,5 @@ //=> {a: 'one', c: 'three', b: 'two'} | ||
@example | ||
```js | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo=1', {parseNumbers: true}); | ||
@@ -92,2 +108,4 @@ //=> {foo: 1} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parse('foo=true', {parseBooleans: true}); | ||
@@ -128,2 +146,4 @@ //=> {foo: true} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.parseUrl('https://foo.bar?foo=bar'); | ||
@@ -156,2 +176,4 @@ //=> {url: 'https://foo.bar', query: {foo: 'bar'}} | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); | ||
@@ -164,2 +186,4 @@ //=> 'foo[]=1&foo[]=2&foo[]=3' | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); | ||
@@ -172,2 +196,4 @@ //=> 'foo[0]=1&foo[1]=2&foo[2]=3' | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); | ||
@@ -180,2 +206,4 @@ //=> 'foo=1,2,3' | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}); | ||
@@ -196,2 +224,4 @@ //=> 'foo=1&foo=2&foo=3' | ||
``` | ||
import queryString = require('query-string'); | ||
const order = ['c', 'a', 'b']; | ||
@@ -203,3 +233,8 @@ | ||
//=> 'c=3&a=1&b=2' | ||
``` | ||
@example | ||
``` | ||
import queryString = require('query-string'); | ||
queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); | ||
@@ -210,2 +245,26 @@ //=> '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; | ||
} | ||
@@ -223,3 +282,5 @@ | ||
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; |
24
index.js
@@ -11,3 +11,3 @@ 'use strict'; | ||
const index = result.length; | ||
if (value === undefined) { | ||
if (value === undefined || (options.skipNull && value === null)) { | ||
return result; | ||
@@ -28,3 +28,3 @@ } | ||
return key => (result, value) => { | ||
if (value === undefined) { | ||
if (value === undefined || (options.skipNull && value === null)) { | ||
return result; | ||
@@ -41,3 +41,3 @@ } | ||
case 'comma': | ||
return key => (result, value, index) => { | ||
return key => (result, value) => { | ||
if (value === null || value === undefined || value.length === 0) { | ||
@@ -47,3 +47,3 @@ return result; | ||
if (index === 0) { | ||
if (result.length === 0) { | ||
return [[encode(key, options), '=', encode(value, options)].join('')]; | ||
@@ -57,3 +57,3 @@ } | ||
return key => (result, value) => { | ||
if (value === undefined) { | ||
if (value === undefined || (options.skipNull && value === null)) { | ||
return result; | ||
@@ -214,3 +214,3 @@ } | ||
for (const param of input.split('&')) { | ||
let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '='); | ||
let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '='); | ||
@@ -266,4 +266,14 @@ // Missing `=` should be `null`: | ||
const formatter = encoderForArrayFormat(options); | ||
const keys = Object.keys(object); | ||
const objectCopy = Object.assign({}, object); | ||
if (options.skipNull) { | ||
for (const key of Object.keys(objectCopy)) { | ||
if (objectCopy[key] === undefined || objectCopy[key] === null) { | ||
delete objectCopy[key]; | ||
} | ||
} | ||
} | ||
const keys = Object.keys(objectCopy); | ||
if (options.sort !== false) { | ||
@@ -270,0 +280,0 @@ keys.sort(options.sort); |
{ | ||
"name": "query-string", | ||
"version": "6.8.3", | ||
"version": "6.9.0", | ||
"description": "Parse and stringify URL query strings", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -61,3 +61,3 @@ # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string) | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -69,3 +69,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `'none'` | ||
@@ -76,2 +76,4 @@ | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); | ||
@@ -84,2 +86,4 @@ //=> {foo: ['1', '2', '3']} | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); | ||
@@ -92,2 +96,4 @@ //=> {foo: ['1', '2', '3']} | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); | ||
@@ -100,2 +106,4 @@ //=> {foo: ['1', '2', '3']} | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo=1&foo=2&foo=3'); | ||
@@ -107,3 +115,3 @@ //=> {foo: ['1', '2', '3']} | ||
Type: `Function | boolean`<br> | ||
Type: `Function | boolean`\ | ||
Default: `true` | ||
@@ -115,6 +123,8 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo=1', {parseNumbers: true}); | ||
@@ -128,6 +138,8 @@ //=> {foo: 1} | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('foo=true', {parseBooleans: true}); | ||
@@ -139,3 +151,3 @@ //=> {foo: true} | ||
### .stringify(object, [options]) | ||
### .stringify(object, options?) | ||
@@ -150,3 +162,3 @@ Stringify an object into a query string and sorting the keys. | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -158,3 +170,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -166,3 +178,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `'none'` | ||
@@ -173,2 +185,4 @@ | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); | ||
@@ -181,2 +195,4 @@ //=> 'foo[]=1&foo[]=2&foo[]=3' | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); | ||
@@ -189,2 +205,4 @@ //=> 'foo[0]=1&foo[1]=2&foo[2]=3' | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); | ||
@@ -197,2 +215,4 @@ //=> 'foo=1,2,3' | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({foo: [1, 2, 3]}); | ||
@@ -209,2 +229,4 @@ //=> 'foo=1&foo=2&foo=3' | ||
```js | ||
const queryString = require('query-string'); | ||
const order = ['c', 'a', 'b']; | ||
@@ -219,2 +241,4 @@ | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); | ||
@@ -226,2 +250,29 @@ //=> 'b=1&c=2&a=3' | ||
##### 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 | ||
}); | ||
//=> '' | ||
``` | ||
### .extract(string) | ||
@@ -231,2 +282,4 @@ | ||
Note: This behaviour can be changed with the `skipNull` option. | ||
### .parseUrl(string, options?) | ||
@@ -241,2 +294,4 @@ | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parseUrl('https://foo.bar?foo=bar'); | ||
@@ -254,2 +309,4 @@ //=> {url: 'https://foo.bar', query: {foo: 'bar'}} | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({ | ||
@@ -267,2 +324,4 @@ foo: 'bar', | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.parse('likes=cake&name=bob&likes=icecream'); | ||
@@ -281,2 +340,4 @@ //=> {likes: ['cake', 'icecream'], name: 'bob'} | ||
```js | ||
const queryString = require('query-string'); | ||
queryString.stringify({foo: false}); | ||
@@ -293,12 +354,6 @@ //=> 'foo=false' | ||
--- | ||
## query-string for enterprise | ||
<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> | ||
Available as part of the Tidelift Subscription. | ||
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) |
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
23819
436
334