Skip to content

Commit 870577e

Browse files
committed
Enable using a RegExp as delimiter
This fixes a regression that was introduced in ljharb#16 which prevents anything but a string from being used as a delimiter. But the whole point of ljharb#12 was to allow users to specify a RegExp to use in queryString.split(delimiter).
1 parent 124ea73 commit 870577e

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

lib/parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ module.exports = function (str, options) {
133133
}
134134

135135
options = options || {};
136-
options.delimiter = typeof options.delimiter === 'string' ? options.delimiter : internals.delimiter;
136+
options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
137137
options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
138138
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
139139
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;

lib/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ exports.compact = function (obj) {
131131

132132
return compacted;
133133
};
134+
135+
exports.isRegExp = function (obj) {
136+
return Object.prototype.toString.call(obj) === '[object RegExp]';
137+
};

test/parse.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,19 @@ describe('#parse', function () {
247247
done();
248248
});
249249

250-
it('parses a string with an alternative delimiter', function (done) {
250+
it('parses a string with an alternative string delimiter', function (done) {
251251

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

256-
it('does not use non-string objects as delimiters', function (done) {
256+
it('parses a string with an alternative RegExp delimiter', function (done) {
257+
258+
expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' });
259+
done();
260+
});
261+
262+
it('does not use non-splittable objects as delimiters', function (done) {
257263

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

0 commit comments

Comments
 (0)