Skip to content

Commit

Permalink
fix: dynamic variable in query
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Aug 27, 2020
1 parent be8dc63 commit daaaa66
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/__tests__/comparison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe('Comparison', () => {
});

describe('query', () => {
it('should return truthy on dynamic matching', () =>
expect(
new Comparison(['dynamic={{age}}']).query(stub),
).toMatchObject({}));

it('should return as mongo query', () =>
expect(new Comparison(exp).query(stub)).toMatchObject({
$and: [
Expand Down
16 changes: 14 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@ class Comparison {
.filter(hasLength);
}

query() {
query(obj = {}) {
return {
$and: this.eligible.reduce((a, [key, value, fn]) => {
$and: this.eligible.reduce((a, params) => {
let key;
let value;
let fn;

if (Array.isArray(params)) {
[key, value, fn] = params;
} else if (isFn(params)) {
[key, value, fn] = params(obj);
} else {
return a;
}

const v = isSerialized(value) ? value.split(',') : value;
const name = Array.isArray(v) ? 'in' : fn.name;

Expand Down

0 comments on commit daaaa66

Please sign in to comment.