Skip to content

Commit 3e61e1d

Browse files
committed
Merge branch 'feature/match-any-and-all-operators' into 'master'
Feature/match any and all operators See merge request auto-cloud/cloudgraph/sdk!59
2 parents 99905d0 + d981603 commit 3e61e1d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/rules-engine/operators/regex.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,28 @@
22
export default {
33
match: (a, b) => new RegExp(b).test(a),
44
mismatch: (a, b) => !new RegExp(b).test(a),
5+
matchAny: (a, b) => {
6+
if (Array.isArray(a)) {
7+
const result = a
8+
.map(value =>
9+
typeof value === 'string' ? new RegExp(b).test(value) : undefined
10+
)
11+
.some(v => v)
12+
return result
13+
}
14+
15+
return false
16+
},
17+
matchAll: (a, b) => {
18+
if (Array.isArray(a)) {
19+
const result = a
20+
.map(value =>
21+
typeof value === 'string' ? new RegExp(b).test(value) : undefined
22+
)
23+
.every(v => v)
24+
return result
25+
}
26+
27+
return false
28+
},
529
}

tests/operators/array.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,28 @@ describe('Array Operators', () => {
2828
expect(ArrayOperators.notIn('rule', [])).toBeTruthy()
2929
})
3030
})
31+
32+
describe('Contains Operator', () => {
33+
test('Should pass given a filled array with the searched element', () => {
34+
expect(ArrayOperators.contains(['*', 'rule'], '*')).toBeTruthy()
35+
})
36+
37+
test('Should fail given a filled array without the searched element', () => {
38+
expect(ArrayOperators.contains(['*', 'rule'], 'x')).toBeFalsy()
39+
})
40+
41+
test('Should fail given an empty array', () => {
42+
expect(ArrayOperators.contains([], 'x')).toBeFalsy()
43+
})
44+
})
45+
46+
describe('DoesNotContain Operator', () => {
47+
test('Should fail given a filled array with the searched element', () => {
48+
expect(ArrayOperators.doesNotContain(['rule'], 'rule')).toBeFalsy()
49+
})
50+
51+
test('Should pass given a filled array without the searched element', () => {
52+
expect(ArrayOperators.doesNotContain(['*'], 'rule')).toBeTruthy()
53+
})
54+
})
3155
})

tests/operators/regex.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,50 @@ describe('Regex Operators', () => {
3333
).toBeFalsy()
3434
})
3535
})
36+
37+
describe('matchAny Operator', () => {
38+
test('Should fail given an invalid array', () => {
39+
expect(
40+
RegexOperators.matchAny('john.doe', /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
41+
).toBeFalsy()
42+
})
43+
44+
test('Should fail given an array with invalid emails', () => {
45+
expect(
46+
RegexOperators.matchAny(['john.doe'], /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
47+
).toBeFalsy()
48+
})
49+
50+
test('Should pass given at least one valid email', () => {
51+
expect(
52+
RegexOperators.matchAny(
53+
['john.doe', 'matt.dAvella@autocloud.dev'],
54+
/[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/
55+
)
56+
).toBeTruthy()
57+
})
58+
})
59+
60+
describe('matchAll Operator', () => {
61+
test('Should fail given an invalid array', () => {
62+
expect(
63+
RegexOperators.matchAll('john.doe', /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
64+
).toBeFalsy()
65+
})
66+
67+
test('Should fail given an array with invalid emails', () => {
68+
expect(
69+
RegexOperators.matchAll(['john.doe'], /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
70+
).toBeFalsy()
71+
})
72+
73+
test('Should fail given an array of valid emails', () => {
74+
expect(
75+
RegexOperators.matchAll(
76+
['john.doe@autocloud.dev', 'matt.dAvella@autocloud.dev'],
77+
/[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/
78+
)
79+
).toBeTruthy()
80+
})
81+
})
3682
})

0 commit comments

Comments
 (0)