-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher.js
More file actions
53 lines (41 loc) · 1.01 KB
/
matcher.js
File metadata and controls
53 lines (41 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function isBlank(value) {
return value === undefined || value === "";
}
function equalsNumber(actual, expected) {
return actual === Number(expected);
}
function includes(actual, expected) {
return actual.includes(expected);
}
function containsOneOf(array, ...oneOf) {
const expected = oneOf.flat();
return array.some((value) => expected.includes(value));
}
class Matcher {
constructor() {
this.matched = true;
}
equalsNumber(actual, expected) {
return this.match(actual, expected, equalsNumber);
}
includes(actual, expected) {
return this.match(actual, expected, includes);
}
containsOneOf(actual, expected) {
return this.match(actual, expected, containsOneOf);
}
match(actual, expected, callback) {
if (this.matched && !isBlank(expected)) {
if (isBlank(actual) || !callback(actual, expected)) {
this.matched = false;
}
}
return this;
}
ifMatched(callback) {
if (this.matched) {
callback();
}
}
}
module.exports = Matcher;