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

Commit 3fcc0b7

Browse files
author
Hans Kristian Flaatten
committed
fix(parser): apply custom functions before general array logic
1 parent 43c15d4 commit 3fcc0b7

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ module.exports.prototype.parse = function parse(query) {
230230
return;
231231
}
232232

233+
// custom functions
234+
if (typeof this.custom[key] === 'function') {
235+
this.custom[key].apply(null, [res, val]);
236+
return;
237+
}
238+
233239
// array key
234240
if (val instanceof Array) {
235241
if (this.ops.indexOf('$in') >= 0 && val.length > 0) {
@@ -267,12 +273,8 @@ module.exports.prototype.parse = function parse(query) {
267273
return;
268274
}
269275

270-
// custom functions
271-
if (typeof this.custom[key] === 'function') {
272-
this.custom[key](res, val);
273-
274276
// field exists query
275-
} else if (!val) {
277+
if (!val) {
276278
res[key] = { $exists: true };
277279

278280
// query operators

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,5 +886,23 @@ describe('parse()', () => {
886886
},
887887
});
888888
});
889+
890+
it('returns custom function query', () => {
891+
mqs = new MongoQS({
892+
custom: {
893+
assigned: (query, input) => {
894+
query['assigned.users._id'] = {
895+
$in: input.map(id => parseInt(id, 10)),
896+
};
897+
},
898+
},
899+
});
900+
901+
assert.deepEqual(mqs.parse({
902+
assigned: ['1111', '2222']
903+
}), {
904+
'assigned.users._id': { $in: [1111, 2222] },
905+
});
906+
});
889907
});
890908
});

0 commit comments

Comments
 (0)