Skip to content

Commit d2ec169

Browse files
authored
Merge pull request #13 from easeq/master
0.6.0
2 parents afee9bf + 3b40023 commit d2ec169

File tree

5 files changed

+36
-50
lines changed

5 files changed

+36
-50
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,15 @@ when(rule, data) // => returns true or false
104104

105105
### Rules using callback
106106

107-
You can also pass a callback function to check the data.
107+
You can also pass a callback function to check the data. Which will be resolved to a promise.
108+
It can either be a plain function or a promise
108109

109110
```js
110111
when(function(data) {
111112
return data.a !== data.b
112-
}, data)
113+
}, data).then((result) => {
114+
//...handle your result
115+
})
113116
```
114117

115118
## Logical rules

package-lock.json

Lines changed: 14 additions & 37 deletions
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.5.1",
3+
"version": "0.6.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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ const processCondition = (condition, data) => (
8080
logicalRules[condition.toLowerCase()](data)
8181
);
8282

83-
const when = (conditions, data) => {
84-
if (typeof conditions === 'function') {
85-
return conditions(data);
86-
}
87-
83+
const validate = (conditions, data) => {
8884
if (!isValidCondition(conditions)) {
8985
return processRule(conditions, data);
9086
}
@@ -102,4 +98,12 @@ const when = (conditions, data) => {
10298
return processCondition(logicalRule, result)
10399
}
104100

101+
const when = (conditions, data) => {
102+
if (typeof conditions === 'function') {
103+
return Promise.resolve(conditions(data));
104+
}
105+
106+
return validate(conditions, data);
107+
}
108+
105109
export default when;

tests/when.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ describe('when', () => {
77
country: 'Germany'
88
}
99

10-
it('callback', () => {
11-
expect(when(function(data) {
10+
it('callback', async () => {
11+
const valid = await when(function(data) {
1212
return data.name == 'John Doe' && data.age >= 18
13-
}, data)).to.equal(true)
13+
}, data);
14+
expect(valid).to.equal(true);
1415

15-
expect(when(function(data) {
16+
const inValid = await when(function(data) {
1617
return data.name == 'John' && data.age >= 18
17-
}, data)).to.equal(false)
18+
}, data);
19+
expect(inValid).to.equal(false);
1820
})
1921

2022
it('mixed deep conditions', () => {

0 commit comments

Comments
 (0)