Skip to content

Commit 597f14a

Browse files
committed
Target Node.js 6 and modern browsers
1 parent 8b5d5ec commit 597f14a

File tree

6 files changed

+89
-103
lines changed

6 files changed

+89
-103
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ language: node_js
22
node_js:
33
- '8'
44
- '6'
5-
- '4'

index.js

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
11
'use strict';
2-
var strictUriEncode = require('strict-uri-encode');
3-
var objectAssign = require('object-assign');
4-
var decodeComponent = require('decode-uri-component');
2+
const strictUriEncode = require('strict-uri-encode');
3+
const decodeComponent = require('decode-uri-component');
54

6-
function encoderForArrayFormat(opts) {
7-
switch (opts.arrayFormat) {
5+
function encoderForArrayFormat(options) {
6+
switch (options.arrayFormat) {
87
case 'index':
9-
return function (key, value, index) {
8+
return (key, value, index) => {
109
return value === null ? [
11-
encode(key, opts),
10+
encode(key, options),
1211
'[',
1312
index,
1413
']'
1514
].join('') : [
16-
encode(key, opts),
15+
encode(key, options),
1716
'[',
18-
encode(index, opts),
17+
encode(index, options),
1918
']=',
20-
encode(value, opts)
19+
encode(value, options)
2120
].join('');
2221
};
23-
2422
case 'bracket':
25-
return function (key, value) {
26-
return value === null ? encode(key, opts) : [
27-
encode(key, opts),
23+
return (key, value) => {
24+
return value === null ? encode(key, options) : [
25+
encode(key, options),
2826
'[]=',
29-
encode(value, opts)
27+
encode(value, options)
3028
].join('');
3129
};
32-
3330
default:
34-
return function (key, value) {
35-
return value === null ? encode(key, opts) : [
36-
encode(key, opts),
31+
return (key, value) => {
32+
return value === null ? encode(key, options) : [
33+
encode(key, options),
3734
'=',
38-
encode(value, opts)
35+
encode(value, options)
3936
].join('');
4037
};
4138
}
4239
}
4340

44-
function parserForArrayFormat(opts) {
45-
var result;
41+
function parserForArrayFormat(options) {
42+
let result;
4643

47-
switch (opts.arrayFormat) {
44+
switch (options.arrayFormat) {
4845
case 'index':
49-
return function (key, value, accumulator) {
46+
return (key, value, accumulator) => {
5047
result = /\[(\d*)\]$/.exec(key);
5148

5249
key = key.replace(/\[\d*\]$/, '');
@@ -62,25 +59,25 @@ function parserForArrayFormat(opts) {
6259

6360
accumulator[key][result[1]] = value;
6461
};
65-
6662
case 'bracket':
67-
return function (key, value, accumulator) {
63+
return (key, value, accumulator) => {
6864
result = /(\[\])$/.exec(key);
6965
key = key.replace(/\[\]$/, '');
7066

7167
if (!result) {
7268
accumulator[key] = value;
7369
return;
74-
} else if (accumulator[key] === undefined) {
70+
}
71+
72+
if (accumulator[key] === undefined) {
7573
accumulator[key] = [value];
7674
return;
7775
}
7876

7977
accumulator[key] = [].concat(accumulator[key], value);
8078
};
81-
8279
default:
83-
return function (key, value, accumulator) {
80+
return (key, value, accumulator) => {
8481
if (accumulator[key] === undefined) {
8582
accumulator[key] = value;
8683
return;
@@ -91,9 +88,9 @@ function parserForArrayFormat(opts) {
9188
}
9289
}
9390

94-
function encode(value, opts) {
95-
if (opts.encode) {
96-
return opts.strict ? strictUriEncode(value) : encodeURIComponent(value);
91+
function encode(value, options) {
92+
if (options.encode) {
93+
return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
9794
}
9895

9996
return value;
@@ -102,65 +99,60 @@ function encode(value, opts) {
10299
function keysSorter(input) {
103100
if (Array.isArray(input)) {
104101
return input.sort();
105-
} else if (typeof input === 'object') {
106-
return keysSorter(Object.keys(input)).sort(function (a, b) {
107-
return Number(a) - Number(b);
108-
}).map(function (key) {
109-
return input[key];
110-
});
102+
}
103+
104+
if (typeof input === 'object') {
105+
return keysSorter(Object.keys(input))
106+
.sort((a, b) => Number(a) - Number(b))
107+
.map(key => input[key]);
111108
}
112109

113110
return input;
114111
}
115112

116-
function extract(str) {
117-
var queryStart = str.indexOf('?');
113+
function extract(input) {
114+
const queryStart = input.indexOf('?');
118115
if (queryStart === -1) {
119116
return '';
120117
}
121-
return str.slice(queryStart + 1);
118+
return input.slice(queryStart + 1);
122119
}
123120

124-
function parse(str, opts) {
125-
opts = objectAssign({arrayFormat: 'none'}, opts);
121+
function parse(input, options) {
122+
options = Object.assign({arrayFormat: 'none'}, options);
126123

127-
var formatter = parserForArrayFormat(opts);
124+
const formatter = parserForArrayFormat(options);
128125

129126
// Create an object with no prototype
130-
// https://github.com/sindresorhus/query-string/issues/47
131-
var ret = Object.create(null);
127+
const ret = Object.create(null);
132128

133-
if (typeof str !== 'string') {
129+
if (typeof input !== 'string') {
134130
return ret;
135131
}
136132

137-
str = str.trim().replace(/^[?#&]/, '');
133+
input = input.trim().replace(/^[?#&]/, '');
138134

139-
if (!str) {
135+
if (!input) {
140136
return ret;
141137
}
142138

143-
str.split('&').forEach(function (param) {
144-
var parts = param.replace(/\+/g, ' ').split('=');
145-
// Firefox (pre 40) decodes `%3D` to `=`
146-
// https://github.com/sindresorhus/query-string/pull/37
147-
var key = parts.shift();
148-
var val = parts.length > 0 ? parts.join('=') : undefined;
139+
for (const param of input.split('&')) {
140+
let [key, value] = param.replace(/\+/g, ' ').split('=');
149141

150-
// missing `=` should be `null`:
142+
// Missing `=` should be `null`:
151143
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
152-
val = val === undefined ? null : decodeComponent(val);
144+
value = value === undefined ? null : decodeComponent(value);
153145

154-
formatter(decodeComponent(key), val, ret);
155-
});
146+
formatter(decodeComponent(key), value, ret);
147+
}
156148

157-
return Object.keys(ret).sort().reduce(function (result, key) {
158-
var val = ret[key];
159-
if (Boolean(val) && typeof val === 'object' && !Array.isArray(val)) {
149+
return Object.keys(ret).sort().reduce((result, key) => {
150+
const value = ret[key];
151+
if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
160152
// Sort object keys, not values
161-
result[key] = keysSorter(val);
153+
result[key] = keysSorter(value);
162154
} else {
163-
result[key] = val;
155+
result[key] = value;
164156
}
165157

166158
return result;
@@ -170,55 +162,53 @@ function parse(str, opts) {
170162
exports.extract = extract;
171163
exports.parse = parse;
172164

173-
exports.stringify = function (obj, opts) {
174-
var defaults = {
165+
exports.stringify = (obj, options) => {
166+
const defaults = {
175167
encode: true,
176168
strict: true,
177169
arrayFormat: 'none'
178170
};
179171

180-
opts = objectAssign(defaults, opts);
172+
options = Object.assign(defaults, options);
181173

182-
if (opts.sort === false) {
183-
opts.sort = function () {};
174+
if (options.sort === false) {
175+
options.sort = () => {};
184176
}
185177

186-
var formatter = encoderForArrayFormat(opts);
178+
const formatter = encoderForArrayFormat(options);
187179

188-
return obj ? Object.keys(obj).sort(opts.sort).map(function (key) {
189-
var val = obj[key];
180+
return obj ? Object.keys(obj).sort(options.sort).map(key => {
181+
const value = obj[key];
190182

191-
if (val === undefined) {
183+
if (value === undefined) {
192184
return '';
193185
}
194186

195-
if (val === null) {
196-
return encode(key, opts);
187+
if (value === null) {
188+
return encode(key, options);
197189
}
198190

199-
if (Array.isArray(val)) {
200-
var result = [];
191+
if (Array.isArray(value)) {
192+
const result = [];
201193

202-
val.slice().forEach(function (val2) {
203-
if (val2 === undefined) {
204-
return;
194+
for (const value2 of value.slice()) {
195+
if (value2 === undefined) {
196+
continue;
205197
}
206198

207-
result.push(formatter(key, val2, result.length));
208-
});
199+
result.push(formatter(key, value2, result.length));
200+
}
209201

210202
return result.join('&');
211203
}
212204

213-
return encode(key, opts) + '=' + encode(val, opts);
214-
}).filter(function (x) {
215-
return x.length > 0;
216-
}).join('&') : '';
205+
return encode(key, options) + '=' + encode(value, options);
206+
}).filter(x => x.length > 0).join('&') : '';
217207
};
218208

219-
exports.parseUrl = function (str, opts) {
209+
exports.parseUrl = (input, options) => {
220210
return {
221-
url: str.split('?')[0] || '',
222-
query: parse(extract(str), opts)
211+
url: input.split('?')[0] || '',
212+
query: parse(extract(input), options)
223213
};
224214
};

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=0.10.0"
13+
"node": ">=6"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -27,19 +27,18 @@
2727
"param",
2828
"parameter",
2929
"url",
30-
"uri",
3130
"parse",
3231
"stringify",
3332
"encode",
34-
"decode"
33+
"decode",
34+
"searchparams"
3535
],
3636
"dependencies": {
3737
"decode-uri-component": "^0.2.0",
38-
"object-assign": "^4.1.0",
39-
"strict-uri-encode": "^1.0.0"
38+
"strict-uri-encode": "^2.0.0"
4039
},
4140
"devDependencies": {
42-
"ava": "^0.17.0",
43-
"xo": "^0.16.0"
41+
"ava": "*",
42+
"xo": "*"
4443
}
4544
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
$ npm install query-string
1616
```
1717

18+
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`.
19+
1820
<a href="https://www.patreon.com/sindresorhus">
1921
<img src="https://c5.patreon.com/external/logo/[email protected]" width="160">
2022
</a>

test/parse.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ test('query strings params including embedded `=`', t => {
7272
t.deepEqual(m.parse('?param=https%3A%2F%2Fsomeurl%3Fid%3D2837'), {param: 'https://someurl?id=2837'});
7373
});
7474

75-
test('query strings params including raw `=`', t => {
76-
t.deepEqual(m.parse('?param=https://someurl?id=2837'), {param: 'https://someurl?id=2837'});
77-
});
78-
7975
test('object properties', t => {
8076
t.falsy(m.parse().prototype);
8177
t.deepEqual(m.parse('hasOwnProperty=foo'), {hasOwnProperty: 'foo'});

test/stringify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ test('should not encode undefined values', t => {
5454
test('should encode null values as just a key', t => {
5555
t.is(m.stringify({
5656
'x y z': null,
57-
'abc': null,
58-
'foo': 'baz'
57+
abc: null,
58+
foo: 'baz'
5959
}), 'abc&foo=baz&x%20y%20z');
6060
});
6161

0 commit comments

Comments
 (0)