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

Commit 992591b

Browse files
committed
Merge pull request #7 from Turistforeningen/feat/in-express-qs-parser
Add support for Express.js / Hapi.js querystring parser (qs)
2 parents d384146 + 59d89b5 commit 992591b

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ useful when building an API and accepting various user specificed queries.
3232
| not exists | `?foo=!` | `{ foo: { $exists: false }}` |
3333
| greater than | `?foo=>10` | `{ foo: { $gt: 10 }}` |
3434
| less than | `?foo=<10` | `{ foo: { $lt: 10 }}` |
35-
| starts with | `?foo=^bar` | `{ foo: { $regex: "^foo", $options: "i" }}` |
36-
| ends with | `?foo=$bar` | `{ foo: { $regex: "foo$", $options: "i" }}` |
37-
| contains | `?foo=~bar` | `{ foo: { $regex: "foo", $options: "i" }}` |
35+
| starts with | `?foo=^bar` | `{ foo: { $regex: "^bar", $options: "i" }}` |
36+
| ends with | `?foo=$bar` | `{ foo: { $regex: "bar$", $options: "i" }}` |
37+
| contains | `?foo=~bar` | `{ foo: { $regex: "bar", $options: "i" }}` |
3838
| in array | `?foo[]=bar&foo[]=baz` | `{ foo: { $in: ['bar', 'baz'] }}` |
3939
| not in array | `?foo[]=!bar&foo[]=!baz` | `{ foo: { $nin: ['bar', 'baz'] }}` |
4040

@@ -44,8 +44,8 @@ useful when building an API and accepting various user specificed queries.
4444

4545
| operation | query string | query object |
4646
|-----------|---------------|--------------|
47-
| bbox | `?bbox=~0,1,2,3` | `{ geojson: { $geoWithin: { $geometry: { … } } } }` |
48-
| near | `?near=~0,1` | `{ geojson: { $near: { $geometry: { … } } } }` |
47+
| bbox | `?bbox=0,1,2,3` | `{ geojson: { $geoWithin: { $geometry: { … } } } }` |
48+
| near | `?near=0,1` | `{ geojson: { $near: { $geometry: { … } } } }` |
4949

5050
* Custom query functions
5151
* `after` (date)

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 (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 (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)