/
test.ts
81 lines (63 loc) · 1.82 KB
/
test.ts
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
import { isLeft, left, right, isRight } from "@sitebender/fp/lib/either"
import { some, none, Option } from "@sitebender/fp/lib/option"
type Unique = <T>(arr: Array<T>) => Array<T>
const unique: Unique = arr =>
arr.filter((value, index, self) => self.indexOf(value) === index)
const and = op => n => {
const ops = op.operands.map(operand =>
typeof operand === "object" ? composeOperations(operand)(n) : operand,
)
const lefts = ops.filter(r => isLeft(r))
if (lefts.length) {
return left(unique(lefts.reduce((a, l) => a.concat(l.left), [])))
}
return ops[0]
}
const or = op => n => {
const ops = op.operands.map(operand =>
typeof operand === "object" ? composeOperations(operand)(n) : operand,
)
const lefts = ops.filter(r => isLeft(r))
if (lefts.length === ops.length) {
return left(unique(lefts.reduce((a, l) => a.concat(l.left), [])))
}
return ops.find(r => !isLeft(r))
}
const lessThan = op => n => {
const { operand, test } = op
const o =
typeof operand === "object"
? operand.operation === "injectFromArgument"
? n
: composeOperations(operand)(n)
: operand
const t = typeof test === "number" ? test : composeOperations(test)(n)
const out = o < t ? right(some(o)) : left(["well, shit"])
return out
}
const moreThan = op => n => {
const { operand, test } = op
const o =
typeof operand === "object"
? operand.operation === "injectFromArgument"
? n
: composeOperations(operand)(n)
: operand
const t = typeof test === "number" ? test : composeOperations(test)(n)
const out = o > t ? right(some(o)) : left(["well, golly"])
return out
}
const composeOperations = op => {
switch (op.operation) {
case "and":
return and(op)
case "or":
return or(op)
case "lessThan":
return lessThan(op)
case "moreThan":
return moreThan(op)
default:
return () => left(["Ouch"])
}
}