Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 1a3d564

Browse files
author
Hans Kristian Flaatten
committed
feat(query): add support for express array parsing
Express.js and Hapi.js use the "qs" query string parser module. This module does a lot of magic including removing trailing `[]` from end of array key names. Related: https://github.com/hapijs/qs
1 parent d384146 commit 1a3d564

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function MongoQS(opts) {
1111
this.custom = opts.custom || {};
1212

1313
this.keyRegex = opts.keyRegex || /^[a-zæøå0-9-_.]+$/i;
14-
this.arrRegex = opts.arrRegex || /^[a-zæøå0-9-_.]+\[\]$/i;
14+
this.arrRegex = opts.arrRegex || /^[a-zæøå0-9-_.]+(\[\])?$/i;
1515

1616
for (var param in this.custom) {
1717
switch (param) {
@@ -129,8 +129,9 @@ module.exports.prototype.parse = function(query) {
129129

130130
// array key
131131
if (val instanceof Array && this.arrRegex.test(key)) {
132-
if (this.ops.indexOf('$in') >= 0) {
133-
key = key.substr(0, key.length-2);
132+
if (this.ops.indexOf('$in') >= 0 && val.length > 0) {
133+
// remove [] at end of key name (unless it has already been removed)
134+
key = key.replace(/\[\]$/, '');
134135

135136
// $in query
136137
if (val[0][0] !== '!') {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"devDependencies": {
4444
"jshint": "^2.8.0",
4545
"mocha": "^2.3.4",
46+
"qs": "^5.2.0",
4647
"semantic-release": "^4.3.5"
4748
}
4849
}

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ describe('parse()', function() {
299299
});
300300
});
301301

302+
it('returns in array query with "qs" parser module (GH-06)', function() {
303+
var string = 'foo[]=10&foo[]=10.011&foo[]=bar';
304+
var params = require('qs').parse(string);
305+
306+
assert.deepEqual(qs.parse(params), {
307+
foo: {
308+
$in: [10, 10.011, 'bar']
309+
}
310+
});
311+
});
312+
302313
it('returns in array without any not in array query', function() {
303314
var string = 'foo[]=10&foo[]=!10.011&foo[]=!bar&foo[]=baz';
304315
var params = require('querystring').parse(string);
@@ -321,6 +332,18 @@ describe('parse()', function() {
321332
});
322333
});
323334

335+
it('returns not in array query with "gs" parser module (GH-06)', function() {
336+
var string = 'foo[]=!10&foo[]=!10.011&foo[]=!bar';
337+
var params = require('qs').parse(string);
338+
339+
assert.deepEqual(qs.parse(params), {
340+
foo: {
341+
$nin: [10, 10.011, 'bar']
342+
}
343+
});
344+
});
345+
346+
324347
it('returns not in array without any in array query', function() {
325348
var string = 'foo[]=!10&foo[]=10.011&foo[]=bar&foo[]=!baz';
326349
var params = require('querystring').parse(string);

0 commit comments

Comments
 (0)