Skip to content

Commit

Permalink
Merge pull request #22 from mjackson/regexp-delim
Browse files Browse the repository at this point in the history
Enable using a RegExp as delimiter
  • Loading branch information
nlf committed Aug 25, 2014
2 parents 124ea73 + 870577e commit 0869f50
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module.exports = function (str, options) {
}

options = options || {};
options.delimiter = typeof options.delimiter === 'string' ? options.delimiter : internals.delimiter;
options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
Expand Down
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ exports.compact = function (obj) {

return compacted;
};

exports.isRegExp = function (obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
};
10 changes: 8 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,19 @@ describe('#parse', function () {
done();
});

it('parses a string with an alternative delimiter', function (done) {
it('parses a string with an alternative string delimiter', function (done) {

expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' });
done();
});

it('does not use non-string objects as delimiters', function (done) {
it('parses a string with an alternative RegExp delimiter', function (done) {

expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' });
done();
});

it('does not use non-splittable objects as delimiters', function (done) {

expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' });
done();
Expand Down

0 comments on commit 0869f50

Please sign in to comment.