Skip to content

Commit

Permalink
add strict flag - resolves #42
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Mar 29, 2016
1 parent 505e825 commit ff0e1c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';
var strictUriEncode = require('strict-uri-encode');

function encode(value, strict) {
return strict ? strictUriEncode(value) : encodeURIComponent(value);
}

exports.extract = function (str) {
return str.split('?')[1] || '';
};
Expand Down Expand Up @@ -45,7 +49,11 @@ exports.parse = function (str) {
return ret;
};

exports.stringify = function (obj) {
exports.stringify = function (obj, opts) {
opts = opts || {};

var strict = opts.strict !== false;

return obj ? Object.keys(obj).sort().map(function (key) {
var val = obj[key];

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

if (val2 === null) {
result.push(strictUriEncode(key));
result.push(encode(key, strict));
} else {
result.push(strictUriEncode(key) + '=' + strictUriEncode(val2));
result.push(encode(key, strict) + '=' + encode(val2, strict));
}
});

return result.join('&');
}

return strictUriEncode(key) + '=' + strictUriEncode(val);
return encode(key, strict) + '=' + encode(val, strict);
}).filter(function (x) {
return x.length > 0;
}).join('&') : '';
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can

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`.

### .stringify(*object*)
### .stringify(*object*, *[options]*)

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

#### strict

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

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)
if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.

### .extract(*string*)

Extract a query string from a URL that can be passed into `.parse()`.
Expand Down
10 changes: 10 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ test('handle undefined values in array', t => {
test('handle undefined and null values in array', t => {
t.same(fn.stringify({foo: null, bar: [undefined, null, 'baz']}), 'bar=baz&bar&foo');
});

test('strict encoding', t => {
t.same(fn.stringify({foo: '\'bar\''}), 'foo=%27bar%27');
t.same(fn.stringify({foo: ['\'bar\'', '!baz']}), 'foo=%21baz&foo=%27bar%27');
});

test('loose encoding', t => {
t.same(fn.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
t.same(fn.stringify({foo: ['\'bar\'', '!baz']}, {strict: false}), 'foo=!baz&foo=\'bar\'');
});

0 comments on commit ff0e1c7

Please sign in to comment.