/
decorate.js
93 lines (82 loc) · 1.68 KB
/
decorate.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import
{ assertValidTag
, getTags
, tagName
, fold
, mapAll
, chainAll
, otherwise
, Either
}
from './core.js'
export function trait(T) {
if (T.traits['sum-type/decorated']) {
return T
} else {
// Do not expose this, they need to define their own _
const _ = otherwise(getTags(T))
const mapT = mapAll(T)
const chainT = chainAll(T)
const foldT = fold(T)
getTags(T).forEach(k => {
const options = [
[k, k, k]
,[k, '_' + k, '_' + k + '_']
,]
options.forEach(([tag, fnName, wrapFnName]) => {
T['is' + fnName] =
T['is' + fnName]
|| (M => {
assertValidTag(T, M)
return tagName(M) === tag
})
T['map' + fnName] =
T['map' + fnName]
|| (f =>
mapT({
..._(x => x)
,[tag]: f,
}))
T['get' + wrapFnName + 'Or'] =
T['get' + wrapFnName + 'Or']
|| (otherwise =>
foldT({
..._(() => otherwise)
,[tag]: x => x,
}))
T['get' + wrapFnName + 'With'] =
T['get' + wrapFnName + 'With']
|| ((otherwise, f) =>
foldT({
..._(() => otherwise)
,[tag]: x => f(x),
}))
T['chain' + fnName] =
T['chain' + fnName]
|| (f => x =>
chainT({
..._(() => x)
,[tag]: f,
})(x))
T['assert' + fnName] =
T['assert' + fnName]
|| foldT({
..._(Either.N)
,[tag]: Either.Y,
})
T[tag.toLowerCase() + 's'] =
T[tag + 's']
|| (xs =>
xs.reduce(
(p, n) => p.concat(T['is' + tag](n) ? [n.value] : []),
[],
))
})
})
T.fold = T.fold || foldT
T.mapAll = T.mapAll || mapT
T.chainAll = T.chainAll || chainT
T.traits['sum-type/decorated'] = true
return T
}
}