-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (37 loc) · 1.42 KB
/
index.js
File metadata and controls
47 lines (37 loc) · 1.42 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
const matchWith = (value, dict) => (condition, lambda) => {
if(typeof condition === 'function') {
dict = {...dict, [condition(value)]: matched => lambda(matched)}
}
else if (typeof condition === 'boolean') {
dict = {...dict, [condition]: matched => lambda(matched)}
}
else if (Array.isArray(condition)) {
dict = {...dict, [condition.some(entry => entry === value)]: matched => lambda(matched)}
}
else {
dict = {...dict, [condition === value || !!condition]: matched => lambda(matched)}
}
return match(value, dict)
}
const isLike = (value, comparison) => Object.keys(comparison)
.filter(key => value[key])
.every(key => value[key] === comparison[key])
const like = (value, dict) => (comparison, lambda) => {
dict = {...dict, [isLike(value, comparison)]: matched => lambda(matched)}
return match(value, dict)
}
const hasField = (value, field) => Object.keys(value)
.includes(field)
const has = (value, dict) => (field, lambda) => {
dict = {...dict, [hasField(value, field)]: matched => lambda(matched)}
return match(value, dict)
}
const otherwise = (value, dict) => lambda => dict[true] ? dict[true](value) : lambda(value)
const match = (value, dict={}) => ({
with: matchWith(value, dict),
like: like(value, dict),
otherwise: otherwise(value, dict),
has: has(value, dict),
execute: () => dict[true](value)
})
module.exports = match