-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
95 lines (79 loc) Β· 2.46 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
console.log('\x1Bc'/* clear */)
type Predicate = (values: string[]) => boolean
function equals (filters: string[]): Predicate {
return function (values) {
return filters.sort().toString() === values.sort().toString()
}
}
function only (filters: string[]): Predicate {
return function (values) {
const leftover = values.toString().replace(new RegExp(filters.join('|')), '')
return leftover === ''
}
}
function all (filters: string[]): Predicate {
return function (values) {
return values.sort().toString().includes(filters.sort().toString())
}
}
function any (filters: string[]): Predicate {
return function (values) {
return new RegExp(filters.join('|')).test(values.join(' '))
}
}
function not (predicate: Predicate): Predicate {
return function (values) {
return !predicate(values)
}
}
function or (...predicates: Predicate[]): Predicate {
return function (values) {
return Boolean(predicates.find(p => p(values)))
}
}
function contains (...predicates: Predicate[]) {
return function (values: string[]) {
return predicates.every((p) => p(values))
}
}
const isDallasAndMiami = contains(
equals(['Dallas', 'Miami']),
)
const containsOnlyFloridaOrTexas = contains(
or(
only(['Miami', 'Orlando']),
only(['Austin', 'Dallas']),
),
)
const containsMiamiAndOrlandoAndNotLa = contains(
all(['Miami', 'Orlando']),
not(all(['LA'])),
)
const containsAustinOrDallas = contains(
any(['Austin', 'Dallas']),
)
const containsDallasOrMiamiAndNotLaOrNYC = contains(
any(['Dallas', 'Miami']),
not(any(['LA', 'NYC'])),
)
const tests = [
() => true === isDallasAndMiami(['Dallas', 'Miami']),
() => false === isDallasAndMiami(['Dallas', 'Miami', 'NYC']),
() => true === containsOnlyFloridaOrTexas(['Miami']),
() => false === containsOnlyFloridaOrTexas(['Miami', 'LA']),
() => true === containsMiamiAndOrlandoAndNotLa(['Miami', 'Orlando']),
() => false === containsMiamiAndOrlandoAndNotLa(['Miami', 'Orlando', 'LA']),
() => true === containsAustinOrDallas(['Austin', 'LA']),
() => false === containsAustinOrDallas(['LA', 'NYC']),
() => true === containsDallasOrMiamiAndNotLaOrNYC(['Dallas']),
() => false === containsDallasOrMiamiAndNotLaOrNYC(['Dallas', 'NYC']),
]
function run (test: () => boolean) {
try { return test() }
catch { return false }
}
for (const test of tests) {
const result = run(test) ? 'β
' : 'β'
const content = test.toString().match(/(t|f).+/)?.[0]
console.log(`${result} ${content}`)
}