Skip to content

Commit 97d1a63

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

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

index.js

Lines changed: 11 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,10 @@ exports.parse = function (str) {
4549
return ret;
4650
};
4751

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

@@ -66,16 +73,16 @@ exports.stringify = function (obj) {
6673
}
6774

6875
if (val2 === null) {
69-
result.push(strictUriEncode(key));
76+
result.push(encode(key, opts.strict));
7077
} else {
71-
result.push(strictUriEncode(key) + '=' + strictUriEncode(val2));
78+
result.push(encode(key, opts.strict) + '=' + encode(val2, opts.strict));
7279
}
7380
});
7481

7582
return result.join('&');
7683
}
7784

78-
return strictUriEncode(key) + '=' + strictUriEncode(val);
85+
return encode(key, opts.strict) + '=' + encode(val, opts.strict);
7986
}).filter(function (x) {
8087
return x.length > 0;
8188
}).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`
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.
61+
5462
### .extract(*string*)
5563

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

test/stringify.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ 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+
});
50+
51+
test('loose encoding', t => {
52+
t.same(fn.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
53+
});

0 commit comments

Comments
 (0)