Skip to content

Commit

Permalink
fix(eval): uses instead of strict boolean matching
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Apr 16, 2020
1 parent 0ade589 commit f30bfac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/__tests__/comparison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const exp = [
'colleague.age=*',
'!colour',
'new=true',
'old=false',
];

const stub = {
new: true,
colour: null,
old: false,
foo: 'bar',
age: 21,
language: 'en',
Expand Down Expand Up @@ -61,6 +63,7 @@ describe('Comparison', () => {
{ 'colleague.age': { $exists: true } },
{ 'colour': { $eq: null } },
{ 'new': true },
{ 'old': { $ne: true } },
],
}));

Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const moment = require('moment');

const toBoolean = (v) => v === 'true';
const toMongoBoolean = (exists) => (exists ? true : { $ne: true });

const validate = (v) => ({
alpha: () => new RegExp(/^[A-Z]+$/, 'gi').test(v),
Expand Down Expand Up @@ -104,7 +105,7 @@ const cast = (v) => {
const test = validate(v);
if (test.date()) return v;
if (test.numeric()) return parseFloat(v);
if (test.boolean()) return toBoolean(v);
if (test.boolean()) return toMongoBoolean(toBoolean(v));
return new RegExp(v, 'i');
};

Expand Down

0 comments on commit f30bfac

Please sign in to comment.