query-string
Advanced tools
Comparing version 6.10.1 to 6.11.0
@@ -39,2 +39,11 @@ export interface ParseOptions { | ||
- `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: | ||
@@ -49,5 +58,12 @@ | ||
*/ | ||
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. | ||
@@ -193,2 +209,11 @@ | ||
- `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: | ||
@@ -203,5 +228,12 @@ | ||
*/ | ||
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. | ||
@@ -208,0 +240,0 @@ |
24
index.js
@@ -39,2 +39,3 @@ 'use strict'; | ||
case 'comma': | ||
case 'separator': | ||
return key => (result, value) => { | ||
@@ -49,3 +50,3 @@ if (value === null || value === undefined || value.length === 0) { | ||
return [[result, encode(value, options)].join(',')]; | ||
return [[result, encode(value, options)].join(options.arrayFormatSeparator)]; | ||
}; | ||
@@ -109,5 +110,6 @@ | ||
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; | ||
@@ -128,2 +130,8 @@ }; | ||
function validateArrayFormatSeparator(value) { | ||
if (typeof value !== 'string' || value.length !== 1) { | ||
throw new TypeError('arrayFormatSeparator must be single character string'); | ||
} | ||
} | ||
function encode(value, options) { | ||
@@ -203,2 +211,3 @@ if (options.encode) { | ||
arrayFormat: 'none', | ||
arrayFormatSeparator: ',', | ||
parseNumbers: false, | ||
@@ -208,2 +217,4 @@ parseBooleans: false | ||
validateArrayFormatSeparator(options.arrayFormatSeparator); | ||
const formatter = parserForArrayFormat(options); | ||
@@ -229,3 +240,3 @@ | ||
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters | ||
value = value === undefined ? null : decode(value, options); | ||
value = value === undefined ? null : options.arrayFormat === 'comma' ? value : decode(value, options); | ||
formatter(decode(key, options), value, ret); | ||
@@ -273,5 +284,8 @@ } | ||
strict: true, | ||
arrayFormat: 'none' | ||
arrayFormat: 'none', | ||
arrayFormatSeparator: ',' | ||
}, options); | ||
validateArrayFormatSeparator(options.arrayFormatSeparator); | ||
const formatter = encoderForArrayFormat(options); | ||
@@ -278,0 +292,0 @@ |
{ | ||
"name": "query-string", | ||
"version": "6.10.1", | ||
"version": "6.11.0", | ||
"description": "Parse and stringify URL query strings", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -124,2 +124,11 @@ # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string) | ||
- `'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: | ||
@@ -134,2 +143,9 @@ | ||
##### arrayFormatSeparator | ||
Type: `string`\ | ||
Default: `','` | ||
The character used to separate array elements when using `{arrayFormat: 'separator'}`. | ||
##### sort | ||
@@ -233,2 +249,9 @@ | ||
##### arrayFormatSeparator | ||
Type: `string`\ | ||
Default: `','` | ||
The character used to separate array elements when using `{arrayFormat: 'separator'}`. | ||
##### sort | ||
@@ -235,0 +258,0 @@ |
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
28512
504
414