query-string
Advanced tools
Comparing version 5.1.1 to 6.0.0
168
index.js
'use strict'; | ||
var strictUriEncode = require('strict-uri-encode'); | ||
var objectAssign = require('object-assign'); | ||
var decodeComponent = require('decode-uri-component'); | ||
const strictUriEncode = require('strict-uri-encode'); | ||
const decodeComponent = require('decode-uri-component'); | ||
function encoderForArrayFormat(opts) { | ||
switch (opts.arrayFormat) { | ||
function encoderForArrayFormat(options) { | ||
switch (options.arrayFormat) { | ||
case 'index': | ||
return function (key, value, index) { | ||
return (key, value, index) => { | ||
return value === null ? [ | ||
encode(key, opts), | ||
encode(key, options), | ||
'[', | ||
@@ -16,25 +15,23 @@ index, | ||
].join('') : [ | ||
encode(key, opts), | ||
encode(key, options), | ||
'[', | ||
encode(index, opts), | ||
encode(index, options), | ||
']=', | ||
encode(value, opts) | ||
encode(value, options) | ||
].join(''); | ||
}; | ||
case 'bracket': | ||
return function (key, value) { | ||
return value === null ? encode(key, opts) : [ | ||
encode(key, opts), | ||
return (key, value) => { | ||
return value === null ? encode(key, options) : [ | ||
encode(key, options), | ||
'[]=', | ||
encode(value, opts) | ||
encode(value, options) | ||
].join(''); | ||
}; | ||
default: | ||
return function (key, value) { | ||
return value === null ? encode(key, opts) : [ | ||
encode(key, opts), | ||
return (key, value) => { | ||
return value === null ? encode(key, options) : [ | ||
encode(key, options), | ||
'=', | ||
encode(value, opts) | ||
encode(value, options) | ||
].join(''); | ||
@@ -45,8 +42,8 @@ }; | ||
function parserForArrayFormat(opts) { | ||
var result; | ||
function parserForArrayFormat(options) { | ||
let result; | ||
switch (opts.arrayFormat) { | ||
switch (options.arrayFormat) { | ||
case 'index': | ||
return function (key, value, accumulator) { | ||
return (key, value, accumulator) => { | ||
result = /\[(\d*)\]$/.exec(key); | ||
@@ -67,5 +64,4 @@ | ||
}; | ||
case 'bracket': | ||
return function (key, value, accumulator) { | ||
return (key, value, accumulator) => { | ||
result = /(\[\])$/.exec(key); | ||
@@ -77,3 +73,5 @@ key = key.replace(/\[\]$/, ''); | ||
return; | ||
} else if (accumulator[key] === undefined) { | ||
} | ||
if (accumulator[key] === undefined) { | ||
accumulator[key] = [value]; | ||
@@ -85,5 +83,4 @@ return; | ||
}; | ||
default: | ||
return function (key, value, accumulator) { | ||
return (key, value, accumulator) => { | ||
if (accumulator[key] === undefined) { | ||
@@ -99,5 +96,5 @@ accumulator[key] = value; | ||
function encode(value, opts) { | ||
if (opts.encode) { | ||
return opts.strict ? strictUriEncode(value) : encodeURIComponent(value); | ||
function encode(value, options) { | ||
if (options.encode) { | ||
return options.strict ? strictUriEncode(value) : encodeURIComponent(value); | ||
} | ||
@@ -111,61 +108,56 @@ | ||
return input.sort(); | ||
} else if (typeof input === 'object') { | ||
return keysSorter(Object.keys(input)).sort(function (a, b) { | ||
return Number(a) - Number(b); | ||
}).map(function (key) { | ||
return input[key]; | ||
}); | ||
} | ||
if (typeof input === 'object') { | ||
return keysSorter(Object.keys(input)) | ||
.sort((a, b) => Number(a) - Number(b)) | ||
.map(key => input[key]); | ||
} | ||
return input; | ||
} | ||
function extract(str) { | ||
var queryStart = str.indexOf('?'); | ||
function extract(input) { | ||
const queryStart = input.indexOf('?'); | ||
if (queryStart === -1) { | ||
return ''; | ||
} | ||
return str.slice(queryStart + 1); | ||
return input.slice(queryStart + 1); | ||
} | ||
function parse(str, opts) { | ||
opts = objectAssign({arrayFormat: 'none'}, opts); | ||
function parse(input, options) { | ||
options = Object.assign({arrayFormat: 'none'}, options); | ||
var formatter = parserForArrayFormat(opts); | ||
const formatter = parserForArrayFormat(options); | ||
// Create an object with no prototype | ||
// https://github.com/sindresorhus/query-string/issues/47 | ||
var ret = Object.create(null); | ||
const ret = Object.create(null); | ||
if (typeof str !== 'string') { | ||
if (typeof input !== 'string') { | ||
return ret; | ||
} | ||
str = str.trim().replace(/^[?#&]/, ''); | ||
input = input.trim().replace(/^[?#&]/, ''); | ||
if (!str) { | ||
if (!input) { | ||
return ret; | ||
} | ||
str.split('&').forEach(function (param) { | ||
var parts = param.replace(/\+/g, ' ').split('='); | ||
// Firefox (pre 40) decodes `%3D` to `=` | ||
// https://github.com/sindresorhus/query-string/pull/37 | ||
var key = parts.shift(); | ||
var val = parts.length > 0 ? parts.join('=') : undefined; | ||
for (const param of input.split('&')) { | ||
let [key, value] = param.replace(/\+/g, ' ').split('='); | ||
// missing `=` should be `null`: | ||
// Missing `=` should be `null`: | ||
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters | ||
val = val === undefined ? null : decodeComponent(val); | ||
value = value === undefined ? null : decodeComponent(value); | ||
formatter(decodeComponent(key), val, ret); | ||
}); | ||
formatter(decodeComponent(key), value, ret); | ||
} | ||
return Object.keys(ret).sort().reduce(function (result, key) { | ||
var val = ret[key]; | ||
if (Boolean(val) && typeof val === 'object' && !Array.isArray(val)) { | ||
return Object.keys(ret).sort().reduce((result, key) => { | ||
const value = ret[key]; | ||
if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) { | ||
// Sort object keys, not values | ||
result[key] = keysSorter(val); | ||
result[key] = keysSorter(value); | ||
} else { | ||
result[key] = val; | ||
result[key] = value; | ||
} | ||
@@ -180,4 +172,4 @@ | ||
exports.stringify = function (obj, opts) { | ||
var defaults = { | ||
exports.stringify = (obj, options) => { | ||
const defaults = { | ||
encode: true, | ||
@@ -188,31 +180,31 @@ strict: true, | ||
opts = objectAssign(defaults, opts); | ||
options = Object.assign(defaults, options); | ||
if (opts.sort === false) { | ||
opts.sort = function () {}; | ||
if (options.sort === false) { | ||
options.sort = () => {}; | ||
} | ||
var formatter = encoderForArrayFormat(opts); | ||
const formatter = encoderForArrayFormat(options); | ||
return obj ? Object.keys(obj).sort(opts.sort).map(function (key) { | ||
var val = obj[key]; | ||
return obj ? Object.keys(obj).sort(options.sort).map(key => { | ||
const value = obj[key]; | ||
if (val === undefined) { | ||
if (value === undefined) { | ||
return ''; | ||
} | ||
if (val === null) { | ||
return encode(key, opts); | ||
if (value === null) { | ||
return encode(key, options); | ||
} | ||
if (Array.isArray(val)) { | ||
var result = []; | ||
if (Array.isArray(value)) { | ||
const result = []; | ||
val.slice().forEach(function (val2) { | ||
if (val2 === undefined) { | ||
return; | ||
for (const value2 of value.slice()) { | ||
if (value2 === undefined) { | ||
continue; | ||
} | ||
result.push(formatter(key, val2, result.length)); | ||
}); | ||
result.push(formatter(key, value2, result.length)); | ||
} | ||
@@ -222,13 +214,11 @@ return result.join('&'); | ||
return encode(key, opts) + '=' + encode(val, opts); | ||
}).filter(function (x) { | ||
return x.length > 0; | ||
}).join('&') : ''; | ||
return encode(key, options) + '=' + encode(value, options); | ||
}).filter(x => x.length > 0).join('&') : ''; | ||
}; | ||
exports.parseUrl = function (str, opts) { | ||
exports.parseUrl = (input, options) => { | ||
return { | ||
url: str.split('?')[0] || '', | ||
query: parse(extract(str), opts) | ||
url: input.split('?')[0] || '', | ||
query: parse(extract(input), options) | ||
}; | ||
}; |
{ | ||
"name": "query-string", | ||
"version": "5.1.1", | ||
"description": "Parse and stringify URL query strings", | ||
"license": "MIT", | ||
"repository": "sindresorhus/query-string", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"browser", | ||
"querystring", | ||
"query", | ||
"string", | ||
"qs", | ||
"param", | ||
"parameter", | ||
"url", | ||
"uri", | ||
"parse", | ||
"stringify", | ||
"encode", | ||
"decode" | ||
], | ||
"dependencies": { | ||
"decode-uri-component": "^0.2.0", | ||
"object-assign": "^4.1.0", | ||
"strict-uri-encode": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"xo": "^0.16.0" | ||
} | ||
"name": "query-string", | ||
"version": "6.0.0", | ||
"description": "Parse and stringify URL query strings", | ||
"license": "MIT", | ||
"repository": "sindresorhus/query-string", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"browser", | ||
"querystring", | ||
"query", | ||
"string", | ||
"qs", | ||
"param", | ||
"parameter", | ||
"url", | ||
"parse", | ||
"stringify", | ||
"encode", | ||
"decode", | ||
"searchparams" | ||
], | ||
"dependencies": { | ||
"decode-uri-component": "^0.2.0", | ||
"strict-uri-encode": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
@@ -18,2 +18,4 @@ # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string) | ||
The latest version targets Node.js 6 or later and modern browsers. If you want support for older browsers, use version 5: `npm install query-string@5`. | ||
<a href="https://www.patreon.com/sindresorhus"> | ||
@@ -20,0 +22,0 @@ <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160"> |
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
2
227
12597
172
+ Addedstrict-uri-encode@2.0.0(transitive)
- Removedobject-assign@^4.1.0
- Removedobject-assign@4.1.1(transitive)
- Removedstrict-uri-encode@1.1.0(transitive)
Updatedstrict-uri-encode@^2.0.0