Skip to content

Commit ff0e1c7

Browse files
add strict flag - resolves #42
1 parent 505e825 commit ff0e1c7

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22
var strictUriEncode = require('strict-uri-encode');
33

4+
function encode(value, strict) {
5+
return strict ? strictUriEncode(value) : encodeURIComponent(value);
6+
}
7+
48
exports.extract = function (str) {
59
return str.split('?')[1] || '';
610
};
@@ -45,7 +49,11 @@ exports.parse = function (str) {
4549
return ret;
4650
};
4751

48-
exports.stringify = function (obj) {
52+
exports.stringify = function (obj, opts) {
53+
opts = opts || {};
54+
55+
var strict = opts.strict !== false;
56+
4957
return obj ? Object.keys(obj).sort().map(function (key) {
5058
var val = obj[key];
5159

@@ -66,16 +74,16 @@ exports.stringify = function (obj) {
6674
}
6775

6876
if (val2 === null) {
69-
result.push(strictUriEncode(key));
77+
result.push(encode(key, strict));
7078
} else {
71-
result.push(strictUriEncode(key) + '=' + strictUriEncode(val2));
79+
result.push(encode(key, strict) + '=' + encode(val2, strict));
7280
}
7381
});
7482

7583
return result.join('&');
7684
}
7785

78-
return strictUriEncode(key) + '=' + strictUriEncode(val);
86+
return encode(key, strict) + '=' + encode(val, strict);
7987
}).filter(function (x) {
8088
return x.length > 0;
8189
}).join('&') : '';

readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
4747

4848
The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
4949

50-
### .stringify(*object*)
50+
### .stringify(*object*, *[options]*)
5151

5252
Stringify an object into a query string, sorting the keys.
5353

54+
#### strict
55+
56+
Type: `boolean`<br />
57+
Default: `true`
58+
59+
Strictly encode URI components with [strict-uri-encode](https://github.com/kevva/strict-uri-encode). It uses [encodeURIComponent](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent)
60+
if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
61+
5462
### .extract(*string*)
5563

5664
Extract a query string from a URL that can be passed into `.parse()`.

test/stringify.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ test('handle undefined values in array', t => {
4343
test('handle undefined and null values in array', t => {
4444
t.same(fn.stringify({foo: null, bar: [undefined, null, 'baz']}), 'bar=baz&bar&foo');
4545
});
46+
47+
test('strict encoding', t => {
48+
t.same(fn.stringify({foo: '\'bar\''}), 'foo=%27bar%27');
49+
t.same(fn.stringify({foo: ['\'bar\'', '!baz']}), 'foo=%21baz&foo=%27bar%27');
50+
});
51+
52+
test('loose encoding', t => {
53+
t.same(fn.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
54+
t.same(fn.stringify({foo: ['\'bar\'', '!baz']}, {strict: false}), 'foo=!baz&foo=\'bar\'');
55+
});

0 commit comments

Comments
 (0)