Skip to content

Commit 1464f7b

Browse files
authored
Merge pull request #7 from easeq/master
Added new rules "allOf" and "isOfType"
2 parents 602a86a + 0e1b3a8 commit 1464f7b

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Following are the available comparison rules:
124124
- ```is``` - returns true if the object value strictly matches the comparison value.
125125
- ```isNot``` - returns true if the object value does not match the comparison value.
126126
- ```anyOf``` - returns true if at least one of the comparison values matches the object key value. The comparison value needs to be an array.
127+
- ```allOf``` - returns true if all the object key value matches the comparison values. The comparison value needs to be an array.
127128
- ```noneOf``` - returns true if none of the comparison values match the object key value. The comparison value needs to be an array.
128129
- ```gt``` - returns true if the object key value is greater than the comparison value
129130
- ```gte``` - returns true if the object key value is greater than or equal to the comparison value

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.2.0",
3+
"version": "0.3.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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ var rules = {
99
isNot: function(key, value, data) {
1010
return get(data, key) != value;
1111
},
12-
// allOf: function(key, value, data) {
13-
// if(!Array.isArray(value)) {
14-
// throw Error('"allOf" condition requires an array as #2 argument');
15-
// }
16-
//
17-
// let dataValue = get(data, key);
18-
// return value.every(currentValue => currentValue === dataValue)
19-
// },
12+
isOfType: function(key, value, data) {
13+
return typeof get(data, key) === value;
14+
},
15+
allOf: function(key, values, data) {
16+
if(!Array.isArray(values)) {
17+
throw Error('"allOf" condition requires an array as #3 argument');
18+
}
19+
20+
var dataValues = get(data, key);
21+
return values.every((currentValue) => dataValues.includes(currentValue))
22+
},
2023
anyOf: function(key, values, data) {
2124
if(!Array.isArray(values)) {
2225
throw Error('"anyOf" condition requires an array as #3 argument');

tests/allOf.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import when from '../src/index';
2+
3+
describe('allOf', () => {
4+
let data = {
5+
brand: ['Apple', 'Samsung', 'Nokia']
6+
}
7+
8+
it('single rule', () => {
9+
expect(when(['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true)
10+
expect(when(['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true)
11+
expect(when(['allOf', 'brand', ['Nokia', 'Samsung']], data)).to.equal(true)
12+
expect(when(['allOf', 'brand', ['Nokia', 'Another']], data)).to.equal(false)
13+
expect(when(['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false)
14+
})
15+
16+
it('single rule using "and"', () => {
17+
expect(when(['and', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']]], data)).to.equal(true)
18+
expect(when(['and', ['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']]], data)).to.equal(true)
19+
expect(when(['and', ['allOf', 'brand', ['Nokia', 'Samsung']]], data)).to.equal(true)
20+
expect(when(['and', ['allOf', 'brand', ['Nokia', 'Another']]], data)).to.equal(false)
21+
expect(when(['and', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']]], data)).to.equal(false)
22+
})
23+
24+
it('single rule using "or"', () => {
25+
expect(when(['or', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']]], data)).to.equal(true)
26+
expect(when(['or', ['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']]], data)).to.equal(true)
27+
expect(when(['or', ['allOf', 'brand', ['Nokia', 'Samsung']]], data)).to.equal(true)
28+
expect(when(['or', ['allOf', 'brand', ['Nokia', 'Another']]], data)).to.equal(false)
29+
expect(when(['or', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']]], data)).to.equal(false)
30+
})
31+
32+
it('deep object key', () => {
33+
let data = {
34+
contact: {
35+
person: {
36+
brand: ['Apple', 'Samsung', 'Nokia']
37+
}
38+
}
39+
}
40+
41+
expect(when(['allOf', 'contact.person.brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true)
42+
expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true)
43+
expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Samsung']], data)).to.equal(true)
44+
expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Another']], data)).to.equal(false)
45+
expect(when(['allOf', 'contact.person.brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false)
46+
})
47+
48+
it('deep object array key', () => {
49+
let data = {
50+
contact: {
51+
person: [{
52+
brand: ['Apple', 'Samsung', 'Nokia']
53+
}]
54+
}
55+
}
56+
57+
expect(when(['allOf', 'contact.person.0.brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true)
58+
expect(when(['allOf', 'contact.person[0].brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true)
59+
expect(when(['allOf', 'contact.person[0].brand', ['Nokia', 'Samsung']], data)).to.equal(true)
60+
expect(when(['allOf', 'contact.person.0.brand', ['Nokia', 'Another']], data)).to.equal(false)
61+
expect(when(['allOf', 'contact.person[0].brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false)
62+
})
63+
64+
it('throws error when values is not of type array', () => {
65+
(function() {
66+
when(['allOf', 'name', 'Apple'], data)
67+
}.should.throw(/"allOf" condition requires an array as #3 argument/));
68+
})
69+
})

tests/isOfType.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import when from '../src/index';
2+
3+
describe('isOfType', () => {
4+
let data = { name: 'John Doe' }
5+
6+
it('single rule', () => {
7+
expect(when(['isOfType', 'firstname', 'undefined'], data)).to.equal(true)
8+
expect(when(['isOfType', 'name', 'undefined'], data)).to.equal(false)
9+
})
10+
11+
it('single rule using "and"', () => {
12+
expect(when(['and', ['isOfType', 'firstname', 'undefined']], data)).to.equal(true)
13+
expect(when(['and', ['isOfType', 'name', 'John']], data)).to.equal(false)
14+
})
15+
16+
it('single rule using "or"', () => {
17+
expect(when(['or', ['isOfType', 'firstname', 'undefined']], data)).to.equal(true)
18+
expect(when(['or', ['isOfType', 'name', 'John']], data)).to.equal(false)
19+
})
20+
21+
it('deep object key', () => {
22+
let data = {
23+
contact: {
24+
person: {
25+
name: 'John Doe'
26+
}
27+
}
28+
}
29+
expect(when(['isOfType', 'contact.person.name', 'John Doe'], data)).to.equal(false)
30+
expect(when(['isOfType', 'contact.person.lastName', 'undefined'], data)).to.equal(true)
31+
})
32+
33+
it('deep object array key', () => {
34+
let data = {
35+
contact: {
36+
person: [{
37+
name: 'John Doe'
38+
}]
39+
}
40+
}
41+
expect(when(['isOfType', 'contact.person.0.name', 'John Doe'], data)).to.equal(false)
42+
expect(when(['isOfType', 'contact.person[0].name', 'John Doe'], data)).to.equal(false)
43+
expect(when(['isOfType', 'contact.person[1].name', 'undefined'], data)).to.equal(true)
44+
})
45+
})

0 commit comments

Comments
 (0)