Skip to content

Commit 3dcd8bc

Browse files
authored
Merge pull request #5 from easeq/master
Changed order of arguments for comparison rule
2 parents 3b7a006 + 1194cdc commit 3dcd8bc

File tree

15 files changed

+216
-236
lines changed

15 files changed

+216
-236
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ Define your conditionals using an array with logical rule ('and' or 'or') as the
2323
General representation of a logical rule is as follows:
2424

2525
```js
26-
['{your logical rule}', [{your conditional rule 1}], [{your conditional rule 2}], ...]
26+
['{your logical rule}', [{your comparison rule 1}], [{your comparison rule 2}], ...]
2727
```
2828

29-
Conditional rule is an array with 3 elements:
29+
Comparison rule is an array with 3 elements:
3030

31+
- The comparison rule.
3132
- The key of the object whose value needs to be compared with a condition.
3233
- The comparison value.
33-
- The comparison rule.
3434

3535
General representation of a comparison rule is as follows:
3636

3737
```js
38-
['{key}', '{comparison value}', '{comparison rule}']
38+
['{comparison rule}', '{key}', '{comparison value}']
3939
```
4040

4141
## Example
@@ -61,36 +61,36 @@ let data = {
6161
#### To simply check one rule (say) data.a = 1
6262

6363
```js
64-
let rule = ['a', 1, 'is']
64+
let rule = ['is', 'a', 1]
6565
```
6666

6767
or
6868

6969
#### Check whether data.c.e\[0\].f = 1
7070
```js
71-
let rule = ['c.e[0]`.f', 1, 'is']
71+
let rule = ['is', 'c.e[0]`.f', 1]
7272
```
7373

7474
### Rules with multiple comparisons
7575

7676
#### Check whether both data.a = 1 and data.b = 2
7777

7878
```js
79-
let rule = ['and', ['a', 1, 'is'], ['b', 2, 'is']]
79+
let rule = ['and', ['is', 'a', 1], ['is', 'b', 2]]
8080
```
8181

8282
#### Check whether either data.a = 1 or data.b = 2
8383

8484
```js
85-
let rule = ['or', ['a', 1, 'is'], ['b', 2, 'is']]
85+
let rule = ['or', ['is', 'a', 1], ['is', 'b', 2]]
8686
```
8787

8888
### Complex rules
8989

9090
#### Check whether (data.a = 1 and data.b = 2) or (data.b = 2 and data.c.d != 'string')
9191

9292
```js
93-
let rule = ['or', ['and', ['a', 1, 'is'], ['b', 2, 'is']], ['and', ['b', 2, 'is'], ['c.d', 'string', 'isNot']]]
93+
let rule = ['or', ['and', ['is', 'a', 1], ['is', 'b', 2]], ['and', ['is', 'b', 2], ['is', 'c.d', 'string']]]
9494
```
9595

9696
### Evaluating rules

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.1.2",
3+
"version": "0.2.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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ var rules = {
1919
// },
2020
anyOf: function(key, values, data) {
2121
if(!Array.isArray(values)) {
22-
throw Error('"anyOf" condition requires an array as #2 argument');
22+
throw Error('"anyOf" condition requires an array as #3 argument');
2323
}
2424

2525
let dataValue = get(data, key);
2626
return values.includes(dataValue);
2727
},
2828
noneOf: function(key, values, data) {
2929
if(!Array.isArray(values)) {
30-
throw Error('"noneOf" condition requires an array as #2 argument');
30+
throw Error('"noneOf" condition requires an array as #3 argument');
3131
}
3232

3333
let dataValue = get(data, key);
@@ -58,7 +58,7 @@ const isValidCondition = (conditions) => {
5858
return false;
5959
}
6060

61-
const processRule = ([key, value, condition], data) => {
61+
const processRule = ([condition, key, value], data) => {
6262
if(typeof condition !== 'string' || rules[condition] === undefined) {
6363
throw Error('Invalid comparison rule ' + condition + '.');
6464
}

tests/and.test.js

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import when from '../src/index';
32

43
describe('and', () => {
@@ -8,43 +7,13 @@ describe('and', () => {
87
}
98

109
it('is', () => {
11-
expect(when(['and', ['name', 'John Doe', 'is'], ['age', 18, 'is']], data)).to.equal(true)
12-
expect(when(['and', ['name', 'John Doe', 'is'], ['age', 17, 'is']], data)).to.equal(false)
10+
expect(when(['and', ['is', 'name', 'John Doe'], ['is', 'age', 18]], data)).to.equal(true)
11+
expect(when(['and', ['is', 'name', 'John Doe'], ['is', 'age', 17]], data)).to.equal(false)
1312
})
1413

1514
it('isNot', () => {
16-
expect(when(['and', ['name', 'John Doe', 'isNot'], ['age', 18, 'isNot']], data)).to.equal(false)
17-
expect(when(['and', ['name', 'John Doe', 'isNot'], ['age', 17, 'isNot']], data)).to.equal(false)
18-
expect(when(['and', ['name', 'John', 'isNot'], ['age', 17, 'isNot']], data)).to.equal(true)
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)
1918
})
20-
21-
// it('single rule using "or"', () => {
22-
// expect(when(['or', ['name', 'John Doe', 'is']], data)).to.equal(true)
23-
// expect(when(['or', ['name', 'John', 'is']], data)).to.equal(false)
24-
// })
25-
//
26-
// it('deep object key', () => {
27-
// let data = {
28-
// contact: {
29-
// person: {
30-
// name: 'John Doe'
31-
// }
32-
// }
33-
// }
34-
// expect(when(['contact.person.name', 'John Doe', 'is'], data)).to.equal(true)
35-
// expect(when(['contact.person.lastName', 'John Doe', 'is'], data)).to.equal(false)
36-
// })
37-
//
38-
// it('deep object array key', () => {
39-
// let data = {
40-
// contact: {
41-
// person: [{
42-
// name: 'John Doe'
43-
// }]
44-
// }
45-
// }
46-
// expect(when(['contact.person.0.name', 'John Doe', 'is'], data)).to.equal(true)
47-
// expect(when(['contact.person[0].name', 'John Doe', 'is'], data)).to.equal(true)
48-
// expect(when(['contact.person[1].name', 'John Doe', 'is'], data)).to.equal(false)
49-
// })
5019
})

tests/anyOf.test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
21
import when from '../src/index';
32

43
describe('anyOf', () => {
54
let data = { name: 'John Doe' }
65

76
it('single rule', () => {
8-
expect(when(['name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
9-
expect(when(['name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
10-
expect(when(['name', ['Doe', 'John'], 'anyOf'], data)).to.equal(false)
7+
expect(when(['anyOf', 'name', ['John Doe', 'John']], data)).to.equal(true)
8+
expect(when(['anyOf', 'name', ['John Doe', 'John']], data)).to.equal(true)
9+
expect(when(['anyOf', 'name', ['Doe', 'John']], data)).to.equal(false)
1110
})
1211

1312
it('single rule using "and"', () => {
14-
expect(when(['and', ['name', ['John Doe', 'John'], 'anyOf']], data)).to.equal(true)
15-
expect(when(['and', ['name', ['John Doe', 'John'], 'anyOf']], data)).to.equal(true)
16-
expect(when(['and', ['name', ['Doe', 'John'], 'anyOf']], data)).to.equal(false)
13+
expect(when(['and', ['anyOf', 'name', ['John Doe', 'John']]], data)).to.equal(true)
14+
expect(when(['and', ['anyOf', 'name', ['John Doe', 'John']]], data)).to.equal(true)
15+
expect(when(['and', ['anyOf', 'name', ['Doe', 'John']]], data)).to.equal(false)
1716
})
1817

1918
it('single rule using "or"', () => {
20-
expect(when(['or', ['name', ['John Doe', 'John'], 'anyOf']], data)).to.equal(true)
21-
expect(when(['or', ['name', ['John Doe', 'John'], 'anyOf']], data)).to.equal(true)
22-
expect(when(['or', ['name', ['Doe', 'John'], 'anyOf']], data)).to.equal(false)
19+
expect(when(['or', ['anyOf', 'name', ['John Doe', 'John']]], data)).to.equal(true)
20+
expect(when(['or', ['anyOf', 'name', ['John Doe', 'John']]], data)).to.equal(true)
21+
expect(when(['or', ['anyOf', 'name', ['Doe', 'John']]], data)).to.equal(false)
2322
})
2423

2524
it('deep object key', () => {
@@ -31,9 +30,9 @@ describe('anyOf', () => {
3130
}
3231
}
3332

34-
expect(when(['contact.person.name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
35-
expect(when(['contact.person.name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
36-
expect(when(['contact.person.name', ['Doe', 'John'], 'anyOf'], data)).to.equal(false)
33+
expect(when(['anyOf', 'contact.person.name', ['John Doe', 'John']], data)).to.equal(true)
34+
expect(when(['anyOf', 'contact.person.name', ['John Doe', 'John']], data)).to.equal(true)
35+
expect(when(['anyOf', 'contact.person.name', ['Doe', 'John']], data)).to.equal(false)
3736
})
3837

3938
it('deep object array key', () => {
@@ -45,15 +44,15 @@ describe('anyOf', () => {
4544
}
4645
}
4746

48-
expect(when(['contact.person.0.name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
49-
expect(when(['contact.person[0].name', ['John Doe', 'John'], 'anyOf'], data)).to.equal(true)
50-
expect(when(['contact.person[0].name', ['Doe', 'John'], 'anyOf'], data)).to.equal(false)
51-
expect(when(['contact.person[1].name', ['Doe', 'John', 'John Doe'], 'anyOf'], data)).to.equal(false)
47+
expect(when(['anyOf', 'contact.person.0.name', ['John Doe', 'John']], data)).to.equal(true)
48+
expect(when(['anyOf', 'contact.person[0].name', ['John Doe', 'John']], data)).to.equal(true)
49+
expect(when(['anyOf', 'contact.person[0].name', ['Doe', 'John']], data)).to.equal(false)
50+
expect(when(['anyOf', 'contact.person[1].name', ['Doe', 'John', 'John Doe']], data)).to.equal(false)
5251
})
5352

5453
it('throws error when values is not of type array', () => {
5554
(function() {
56-
when(['name', 'John', 'anyOf'], data)
57-
}.should.throw(/"anyOf" condition requires an array as #2 argument/));
55+
when(['anyOf', 'name', 'John'], data)
56+
}.should.throw(/"anyOf" condition requires an array as #3 argument/));
5857
})
5958
})

tests/gt.test.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
21
import when from '../src/index';
32

43
describe('gt', () => {
54
let data = { age: 18 }
65

76
it('single rule', () => {
8-
expect(when(['age', 19, 'gt'], data)).to.equal(false)
9-
expect(when(['age', '19', 'gt'], data)).to.equal(false)
10-
expect(when(['age', 18, 'gt'], data)).to.equal(false)
11-
expect(when(['age', 16, 'gt'], data)).to.equal(true)
7+
expect(when(['gt', 'age', 19], data)).to.equal(false)
8+
expect(when(['gt', 'age', '19'], data)).to.equal(false)
9+
expect(when(['gt', 'age', 18], data)).to.equal(false)
10+
expect(when(['gt', 'age', 16], data)).to.equal(true)
1211
})
1312

1413
it('single rule using "and"', () => {
15-
expect(when(['and', ['age', 19, 'gt']], data)).to.equal(false)
16-
expect(when(['and', ['age', '19', 'gt']], data)).to.equal(false)
17-
expect(when(['and', ['age', 18, 'gt']], data)).to.equal(false)
18-
expect(when(['and', ['age', 16, 'gt']], data)).to.equal(true)
14+
expect(when(['and', ['gt', 'age', 19]], data)).to.equal(false)
15+
expect(when(['and', ['gt', 'age', '19']], data)).to.equal(false)
16+
expect(when(['and', ['gt', 'age', 18]], data)).to.equal(false)
17+
expect(when(['and', ['gt', 'age', 16]], data)).to.equal(true)
1918
})
2019

2120
it('single rule using "or"', () => {
22-
expect(when(['or', ['age', 19, 'gt']], data)).to.equal(false)
23-
expect(when(['or', ['age', '19', 'gt']], data)).to.equal(false)
24-
expect(when(['or', ['age', 18, 'gt']], data)).to.equal(false)
25-
expect(when(['or', ['age', 16, 'gt']], data)).to.equal(true)
21+
expect(when(['or', ['gt', 'age', 19]], data)).to.equal(false)
22+
expect(when(['or', ['gt', 'age', '19']], data)).to.equal(false)
23+
expect(when(['or', ['gt', 'age', 18]], data)).to.equal(false)
24+
expect(when(['or', ['gt', 'age', 16]], data)).to.equal(true)
2625
})
2726

2827
it('deep object key', () => {
@@ -35,10 +34,10 @@ describe('gt', () => {
3534
}
3635
}
3736

38-
expect(when(['contact.person.age', 19, 'gt'], data)).to.equal(false)
39-
expect(when(['contact.person.age', '19', 'gt'], data)).to.equal(false)
40-
expect(when(['contact.person.age', 18, 'gt'], data)).to.equal(false)
41-
expect(when(['contact.person.age', 16, 'gt'], data)).to.equal(true)
37+
expect(when(['gt', 'contact.person.age', 19], data)).to.equal(false)
38+
expect(when(['gt', 'contact.person.age', '19'], data)).to.equal(false)
39+
expect(when(['gt', 'contact.person.age', 18], data)).to.equal(false)
40+
expect(when(['gt', 'contact.person.age', 16], data)).to.equal(true)
4241
})
4342

4443
it('deep object array key', () => {
@@ -51,20 +50,20 @@ describe('gt', () => {
5150
}
5251
}
5352

54-
expect(when(['contact.person.0.age', 19, 'gt'], data)).to.equal(false)
55-
expect(when(['contact.person.0.age', '19', 'gt'], data)).to.equal(false)
56-
expect(when(['contact.person.0.age', 18, 'gt'], data)).to.equal(false)
57-
expect(when(['contact.person.0.age', 16, 'gt'], data)).to.equal(true)
53+
expect(when(['gt', 'contact.person.0.age', 19], data)).to.equal(false)
54+
expect(when(['gt', 'contact.person.0.age', '19'], data)).to.equal(false)
55+
expect(when(['gt', 'contact.person.0.age', 18], data)).to.equal(false)
56+
expect(when(['gt', 'contact.person.0.age', 16], data)).to.equal(true)
5857

59-
expect(when(['contact.person[0].age', 19, 'gt'], data)).to.equal(false)
60-
expect(when(['contact.person[0].age', '19', 'gt'], data)).to.equal(false)
61-
expect(when(['contact.person[0].age', 18, 'gt'], data)).to.equal(false)
62-
expect(when(['contact.person[0].age', 16, 'gt'], data)).to.equal(true)
58+
expect(when(['gt', 'contact.person[0].age', 19], data)).to.equal(false)
59+
expect(when(['gt', 'contact.person[0].age', '19'], data)).to.equal(false)
60+
expect(when(['gt', 'contact.person[0].age', 18], data)).to.equal(false)
61+
expect(when(['gt', 'contact.person[0].age', 16], data)).to.equal(true)
6362

64-
expect(when(['contact.person[1].age', 19, 'gt'], data)).to.equal(false)
65-
expect(when(['contact.person[1].age', '19', 'gt'], data)).to.equal(false)
66-
expect(when(['contact.person[1].age', 18, 'gt'], data)).to.equal(false)
67-
expect(when(['contact.person[1].age', 16, 'gt'], data)).to.equal(false)
63+
expect(when(['gt', 'contact.person[1].age', 19], data)).to.equal(false)
64+
expect(when(['gt', 'contact.person[1].age', '19'], data)).to.equal(false)
65+
expect(when(['gt', 'contact.person[1].age', 18], data)).to.equal(false)
66+
expect(when(['gt', 'contact.person[1].age', 16], data)).to.equal(false)
6867

6968
})
7069
})

0 commit comments

Comments
 (0)