Skip to content

Commit 1391652

Browse files
authored
Merge pull request #10 from easeq/master
Fixes #9
2 parents 6102e67 + eeba640 commit 1391652

File tree

9 files changed

+77
-123
lines changed

9 files changed

+77
-123
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,13 @@ when(function(data) {
117117
There are 2 types of logical rules:
118118
- ```and``` - checks whether all the conditions evaluate to true.
119119
- ```or``` - checks whether atleast one condition evaluates to true.
120+
- ```not``` - returns the opposite of the evaluated comparison rule. This logical rule takes only one comparison rule.
120121

121122
## Comparison rules
122123

123124
Following are the available comparison rules:
124125
- ```is``` - returns true if the object value strictly matches the comparison value.
125-
- ```isNot``` - returns true if the object value does not match the comparison value.
126126
- ```isOfType``` - returns true if the object value matches the specified type (Ex: string, undefined, etc.).
127-
- ```isNotOfType``` - returns true if the object value does not matche the specified type (Ex: string, undefined, etc.).
128127
- ```anyOf``` - returns true if at least one of the comparison values matches the object key value. The comparison value needs to be an array.
129128
- ```allOf``` - returns true if all the object key value matches the comparison values. The comparison value needs to be an array.
130129
- ```noneOf``` - returns true if none of the comparison values match the object key value. The comparison value needs to be an array.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flipbyte/when-condition",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Check conditional statements and return true/false",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@ const get = (obj, path, defaultValue = undefined) => (
44
.reduce((a, c) => (a && Object.hasOwnProperty.call(a, c)) ? a[c] : defaultValue, obj)
55
);
66

7-
const logicalOperators = ['and', 'or'];
8-
97
var rules = {
108
is: function(key, value, data) {
119
return get(data, key) === value;
1210
},
13-
isNot: function(key, value, data) {
14-
return get(data, key) != value;
15-
},
1611
isOfType: function(key, value, data) {
1712
return typeof get(data, key) === value;
1813
},
19-
isNotOfType: function(key, value, data) {
20-
return typeof get(data, key) !== value;
21-
},
2214
allOf: function(key, values, data) {
2315
if(!Array.isArray(values)) {
2416
throw Error('"allOf" condition requires an array as #3 argument');
@@ -55,12 +47,28 @@ var rules = {
5547
lte: function(key, value, data) {
5648
return get(data, key) <= value
5749
},
50+
};
51+
52+
const logicalRules = {
53+
and: function(data) {
54+
return !data.includes(false);
55+
},
56+
or: function(data) {
57+
return data.includes(true);
58+
},
59+
not: function(data) {
60+
if (data.length !== 1) {
61+
throw Error('"not" can have only one comparison rule, multiple rules given');
62+
}
63+
64+
return !data[0];
65+
}
5866
}
5967

6068
const isValidCondition = (conditions) => {
6169
if(Array.isArray(conditions)
6270
&& Array.isArray(conditions[1])
63-
&& logicalOperators.includes(conditions[0])
71+
&& (conditions[0] && logicalRules[conditions[0].toLowerCase()])
6472
) {
6573
return true;
6674
}
@@ -76,20 +84,16 @@ const processRule = ([condition, key, value], data) => {
7684
return rules[condition](key, value, data);
7785
}
7886

79-
const processCondition = (condition, data) => {
80-
if(condition.toLowerCase() === 'or') {
81-
return data.includes(true);
82-
}
83-
84-
return !data.includes(false);
85-
}
87+
const processCondition = (condition, data) => (
88+
logicalRules[condition.toLowerCase()](data)
89+
);
8690

8791
const when = (conditions, data) => {
8892
if (typeof conditions === 'function') {
8993
return conditions(data);
9094
}
9195

92-
if(!isValidCondition(conditions)) {
96+
if (!isValidCondition(conditions)) {
9397
return processRule(conditions, data);
9498
}
9599

tests/and.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ describe('and', () => {
99
it('is', () => {
1010
expect(when(['and', ['is', 'name', 'John Doe'], ['is', 'age', 18]], data)).to.equal(true)
1111
expect(when(['and', ['is', 'name', 'John Doe'], ['is', 'age', 17]], data)).to.equal(false)
12-
})
12+
});
1313

14-
it('isNot', () => {
15-
expect(when(['and', ['isNot', 'name', 'John Doe'], ['isNot', 'age', 18]], data)).to.equal(false)
16-
expect(when(['and', ['isNot', 'name', 'John Doe'], ['isNot', 'age', 17]], data)).to.equal(false)
17-
expect(when(['and', ['isNot', 'name', 'John'], ['isNot', 'age', 17]], data)).to.equal(true)
18-
})
14+
it('a and not b', () => {
15+
expect(when(['and', ['is', 'name', 'John Doe'], ['not', ['is', 'age', 18]]], data)).to.equal(false);
16+
expect(when(['and', ['is', 'name', 'John Doe'], ['not', ['is', 'age', 17]]], data)).to.equal(true);
17+
});
18+
19+
it('not a and not b', () => {
20+
expect(when(['and', ['not', ['is', 'name', 'John Doe']], ['not', ['is', 'age', 18]]], data)).to.equal(false);
21+
expect(when(['and', ['not', ['is', 'name', 'John']], ['not', ['is', 'age', 17]]], data)).to.equal(true);
22+
});
1923
})

tests/isNot.test.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/isNotOfType.test.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/not.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import when from '../src/index';
2+
3+
describe('not', () => {
4+
let data = {
5+
name: 'John Doe',
6+
age: 18
7+
}
8+
9+
it('is', () => {
10+
expect(when(['not', ['is', 'name', 'John Doe']], data)).to.equal(false)
11+
expect(when(['not', ['is', 'age', 17]], data)).to.equal(true)
12+
})
13+
14+
it('(a and b)', () => {
15+
expect(when(['not', ['or', ['is', 'name', 'John Doe'], ['is', 'age', 18]]], data)).to.equal(false)
16+
expect(when(['not', ['or', ['is', 'name', 'John Doe'], ['is', 'age', 17]]], data)).to.equal(false)
17+
expect(when(['not', ['or', ['is', 'name', 'John'], ['is', 'age', 17]]], data)).to.equal(true)
18+
});
19+
20+
it('(a or b)', () => {
21+
expect(when(['not', ['and', ['is', 'name', 'John Doe'], ['is', 'age', 18]]], data)).to.equal(false)
22+
expect(when(['not', ['and', ['is', 'name', 'John Doe'], ['is', 'age', 17]]], data)).to.equal(true)
23+
expect(when(['not', ['and', ['is', 'name', 'John'], ['is', 'age', 17]]], data)).to.equal(true)
24+
});
25+
26+
it('multiple comparison rules throws error', () => {
27+
(function() {
28+
expect(when(['not', ['is', 'name', 'John Doe'], ['is', 'age', 18]], data))
29+
}.should.throw(/"not" can have only one comparison rule, multiple rules given/));
30+
});
31+
})

tests/or.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ describe('or', () => {
99
it('is', () => {
1010
expect(when(['or', ['is', 'name', 'John Doe'], ['is', 'age', 18]], data)).to.equal(true)
1111
expect(when(['or', ['is', 'name', 'John'], ['is', 'age', 17]], data)).to.equal(false)
12-
})
12+
});
1313

14-
it('isNot', () => {
15-
expect(when(['or', ['isNot', 'name', 'John Doe'], ['isNot', 'age', 18]], data)).to.equal(false)
16-
expect(when(['or', ['isNot', 'name', 'John Doe'], ['isNot', 'age', 17]], data)).to.equal(true)
17-
expect(when(['or', ['isNot', 'name', 'John'], ['isNot', 'age', 17]], data)).to.equal(true)
18-
})
14+
it('a or not b', () => {
15+
expect(when(['or', ['is', 'name', 'John Doe'], ['not', ['is', 'age', 18]]], data)).to.equal(true);
16+
expect(when(['or', ['is', 'name', 'John Doe'], ['not', ['is', 'age', 17]]], data)).to.equal(true);
17+
expect(when(['or', ['is', 'name', 'John'], ['not', ['is', 'age', 18]]], data)).to.equal(false);
18+
});
19+
20+
it('not a or not b', () => {
21+
expect(when(['or', ['not', ['is', 'name', 'John Doe']], ['not', ['is', 'age', 18]]], data)).to.equal(false);
22+
expect(when(['or', ['not', ['is', 'name', 'John']], ['not', ['is', 'age', 17]]], data)).to.equal(true);
23+
expect(when(['or', ['not', ['is', 'name', 'John Doe']], ['not', ['is', 'age', 17]]], data)).to.equal(true);
24+
});
1925
})

0 commit comments

Comments
 (0)