Socket
Socket
Sign inDemoInstall

query-string

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

query-string - npm Package Compare versions

Comparing version 4.2.3 to 4.3.0

135

index.js

@@ -5,2 +5,88 @@ 'use strict';

function encoderForArrayFormat(opts) {
switch (opts.arrayFormat) {
case 'index':
return function (key, value, index) {
return value === null ? [
encode(key, opts),
'[',
index,
']'
].join('') : [
encode(key, opts),
'[',
encode(index, opts),
']=',
encode(value, opts)
].join('');
};
case 'bracket':
return function (key, value) {
return value === null ? encode(key, opts) : [
encode(key, opts),
'[]=',
encode(value, opts)
].join('');
};
default:
return function (key, value) {
return value === null ? encode(key, opts) : [
encode(key, opts),
'=',
encode(value, opts)
].join('');
};
}
}
function parserForArrayFormat(opts) {
var result;
switch (opts.arrayFormat) {
case 'index':
return function (key, value, accumulator) {
result = /\[(\d*)]$/.exec(key);
key = key.replace(/\[\d*]$/, '');
if (!result) {
accumulator[key] = value;
return;
}
if (accumulator[key] === undefined) {
accumulator[key] = {};
}
accumulator[key][result[1]] = value;
};
case 'bracket':
return function (key, value, accumulator) {
result = /(\[])$/.exec(key);
key = key.replace(/\[]$/, '');
if (!result || accumulator[key] === undefined) {
accumulator[key] = value;
return;
}
accumulator[key] = [].concat(accumulator[key], value);
};
default:
return function (key, value, accumulator) {
if (accumulator[key] === undefined) {
accumulator[key] = value;
return;
}
accumulator[key] = [].concat(accumulator[key], value);
};
}
}
function encode(value, opts) {

@@ -14,2 +100,16 @@ if (opts.encode) {

function sorter(input) {
if (Array.isArray(input)) {
return input.sort();
} else if (typeof input === 'object') {
return sorter(Object.keys(input)).sort(function (a, b) {
return Number(a) - Number(b);
}).map(function (key) {
return input[key];
});
}
return input;
}
exports.extract = function (str) {

@@ -19,3 +119,7 @@ return str.split('?')[1] || '';

exports.parse = function (str) {
exports.parse = function (str, opts) {
opts = objectAssign({arrayFormat: 'none'}, opts);
var formatter = parserForArrayFormat(opts);
// Create an object with no prototype

@@ -42,4 +146,2 @@ // https://github.com/sindresorhus/query-string/issues/47

key = decodeURIComponent(key);
// missing `=` should be `null`:

@@ -49,12 +151,14 @@ // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters

if (ret[key] === undefined) {
ret[key] = val;
} else if (Array.isArray(ret[key])) {
ret[key].push(val);
formatter(decodeURIComponent(key), val, ret);
});
return Object.keys(ret).sort().reduce(function (result, key) {
if (Boolean(ret[key]) && typeof ret[key] === 'object') {
result[key] = sorter(ret[key]);
} else {
ret[key] = [ret[key], val];
result[key] = ret[key];
}
});
return ret;
return result;
}, Object.create(null));
};

@@ -65,3 +169,4 @@

encode: true,
strict: true
strict: true,
arrayFormat: 'none'
};

@@ -71,2 +176,4 @@

var formatter = encoderForArrayFormat(opts);
return obj ? Object.keys(obj).sort().map(function (key) {

@@ -91,7 +198,3 @@ var val = obj[key];

if (val2 === null) {
result.push(encode(key, opts));
} else {
result.push(encode(key, opts) + '=' + encode(val2, opts));
}
result.push(formatter(key, val2, result.length));
});

@@ -98,0 +201,0 @@

6

package.json
{
"name": "query-string",
"version": "4.2.3",
"version": "4.3.0",
"description": "Parse and stringify URL query strings",

@@ -41,5 +41,5 @@ "license": "MIT",

"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^0.17.0",
"xo": "^0.16.0"
}
}

@@ -7,3 +7,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.</p>
<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. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React course</a>.</p>

@@ -54,3 +54,3 @@ ---

### .parse(*string*)
### .parse(*string*, *[options]*)

@@ -61,2 +61,30 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.

#### arrayFormat
Type: `string`<br>
Default: `'none'`
Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array 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]
```
- `index`: stands for parsing taking the index into account, such as:
```js
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
//=> foo: [1,2,3]
```
- `none`: is the **default** option and removes any bracket representation, such as:
```js
queryString.parse('foo=1&foo=2&foo=3');
//=> foo: [1,2,3]
```
### .stringify(*object*, *[options]*)

@@ -68,3 +96,3 @@

Type: `boolean`<br />
Type: `boolean`<br>
Default: `true`

@@ -82,2 +110,30 @@

#### arrayFormat
Type: `string`<br>
Default: `'none'`
Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array 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`: stands for parsing taking the index into account, such as:
```js
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:
```js
queryString.stringify({foo: [1,2,3]});
// => foo=1&foo=2&foo=3
```
### .extract(*string*)

@@ -84,0 +140,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc