Skip to content

Commit

Permalink
feat: ! to signify negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Feb 3, 2020
1 parent 52a3e43 commit 41cae41
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/__tests__/comparison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const exp = [
'language=EN',
'bestFriend.name=jon',
'colleague.age=*',
'!colour',
];

const stub = {
colour: null,
foo: 'bar',
age: 21,
language: 'en',
Expand All @@ -29,6 +31,7 @@ describe('Comparison', () => {
expect(Comparison.isValid('<')).toBeTruthy();
expect(Comparison.isValid('>=')).toBeTruthy();
expect(Comparison.isValid('<=')).toBeTruthy();
expect(Comparison.isValid('!foo')).toBeTruthy();
});
});

Expand All @@ -38,7 +41,9 @@ describe('Comparison', () => {

it('should return falsy', () =>
expect(
new Comparison(['age>22', 'language=FR']).eval(stub),
new Comparison(['age>22', 'language=FR', 'color=red']).eval(
stub,
),
).toBeFalsy());
});

Expand All @@ -52,6 +57,7 @@ describe('Comparison', () => {
{ language: /EN/gi },
{ 'bestFriend.name': /jon/gi },
{ 'colleague.age': { $exists: true } },
{ 'colour': { $or: [{ $exists: false }, { $eq: null }] } },
],
}));

Expand Down
11 changes: 11 additions & 0 deletions lib/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ describe('Utils', () => {
it('should return truthy on equal value', () =>
expect(Utils.isGreaterThanOrEqualTo(1, 2)).toBeFalsy());
});

describe('isEmpty', () => {
it('should return truthy on null', () =>
expect(Utils.isEmpty(null)).toBeTruthy());

it('should return truthy on undefined', () =>
expect(Utils.isEmpty(undefined)).toBeTruthy());

it('should return falsy on false', () =>
expect(Utils.isEmpty(false)).toBeFalsy());
});
});
29 changes: 18 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
isLessThan,
isGreaterThanOrEqualTo,
isLessThanOrEqualTo,
isEmpty,
cast,
} = require('./utils');

Expand All @@ -19,6 +20,7 @@ const ops = {

const getAssignment = {
'equals': cast,
'isEmpty': cast,
'isLessThan': (v) => ({
$lt: cast(v),
}),
Expand All @@ -37,18 +39,23 @@ const keys = Object.keys(ops);
const re = new RegExp(`[${keys.map((v) => `(${v})`).join('')}]`, 'i');

const hasLength = (v) => Array.isArray(v) && v.length;
const hasNegation = (v) => typeof v === 'string' && v.startsWith('!');

const getOp = (v) =>
keys.reduce(
(a, c) =>
v.includes(c) && !hasLength(a)
? v
.split(re)
.filter(Boolean)
.concat(ops[c])
: a,
[],
);
hasNegation(v)
? // reference the cast function conditionals
// this leverages how wildcards work, just the inverse
[v.replace('!', ''), '!', isEmpty]
: keys.reduce(
(a, c) =>
v.includes(c) && !hasLength(a)
? v
.split(re)
.filter(Boolean)
.concat(ops[c])
: a,
[],
);

class Comparison {
constructor(exp, locale = 'en') {
Expand All @@ -57,7 +64,7 @@ class Comparison {
}

static isValid(v) {
return re.test(v);
return hasNegation(v) || re.test(v);
}

get eligible() {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,24 @@ const isLessThanOrEqualTo = (a, b, locale) =>

const cast = (v) => {
if (v === '*') return { $exists: true };
if (v === '!') return { $or: [{ $exists: false }, { $eq: null }] };

const test = validate(v);
if (test.date()) return v;
if (test.numeric()) return parseFloat(v);
return new RegExp(v, 'i');
};

const isEmpty = (v) =>
v === undefined || v === null || v === 'undefined' || v === 'null';

module.exports = {
equals,
isGreaterThan,
isLessThan,
isGreaterThanOrEqualTo,
isLessThanOrEqualTo,
isEmpty,
validate,
cast,
};

0 comments on commit 41cae41

Please sign in to comment.