Skip to content

Commit

Permalink
fix(parser): apply custom functions before general array logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Jul 27, 2016
1 parent 43c15d4 commit 3fcc0b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ module.exports.prototype.parse = function parse(query) {
return;
}

// custom functions
if (typeof this.custom[key] === 'function') {
this.custom[key].apply(null, [res, val]);
return;
}

// array key
if (val instanceof Array) {
if (this.ops.indexOf('$in') >= 0 && val.length > 0) {
Expand Down Expand Up @@ -267,12 +273,8 @@ module.exports.prototype.parse = function parse(query) {
return;
}

// custom functions
if (typeof this.custom[key] === 'function') {
this.custom[key](res, val);

// field exists query
} else if (!val) {
if (!val) {
res[key] = { $exists: true };

// query operators
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,5 +886,23 @@ describe('parse()', () => {
},
});
});

it('returns custom function query', () => {
mqs = new MongoQS({
custom: {
assigned: (query, input) => {
query['assigned.users._id'] = {
$in: input.map(id => parseInt(id, 10)),
};
},
},
});

assert.deepEqual(mqs.parse({
assigned: ['1111', '2222']
}), {
'assigned.users._id': { $in: [1111, 2222] },
});
});
});
});

0 comments on commit 3fcc0b7

Please sign in to comment.