-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
220 lines (199 loc) · 8.04 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import assert from 'node:assert'
import test from 'node:test'
import { cfp, sfp } from './index.js'
test('cfp initialized without args', async () => {
await assert.rejects(async () => (cfp as any)(), new Error('cfp initialized without functions'))
})
test('cfp basic', async () => {
type Context = { numbers: number[] }
const positiveNumbers = ({ numbers }: Context) => numbers.filter(n => n > 0)
const numbersPrefix = () => 'Here is numbers:'
const positiveNumbersAsString = cfp(positiveNumbers, numbersPrefix, (ns, prefix) => `${prefix} ${ns.toString()}`)
assert.strictEqual(positiveNumbersAsString({ numbers: [-1, -5, 7, 0, 4] }), 'Here is numbers: 7,4')
})
test('cfp raw', async () => {
const positiveNumbers = ({ numbers }: { numbers: number[] }) => numbers.filter(n => n > 0)
const numbersPrefix = () => 'Here is numbers:'
const positiveNumbersAsString = cfp(positiveNumbers, numbersPrefix, (ns, prefix) => `${prefix} ${ns.toString()}`)
assert.strictEqual(positiveNumbersAsString.raw([7, 4], 'Here is numbers:'), 'Here is numbers: 7,4')
})
test('cfp cache', async () => {
let called = 0
const positiveNumbers = ({ numbers }: { numbers: number[] }) => (called++, numbers.filter(n => n > 0))
const positiveNumbersLength = cfp(positiveNumbers, ns => ns.length)
const positiveNumbersAsString = cfp(
positiveNumbers,
positiveNumbersLength,
(ns, l) => `${ns.toString()}; length - ${l}`
)
assert.strictEqual(positiveNumbersAsString({ numbers: [-1, -5, 7, 0, 4] }), '7,4; length - 2')
assert.strictEqual(called, 1)
})
test('cfp di', async () => {
const fetchUserFromDB = async (): Promise<{ name: string }> => {
throw new Error('should not call')
}
const fetchUser = ({ fetchUser }: { fetchUser?: typeof fetchUserFromDB }) => fetchUser?.() ?? fetchUserFromDB()
const helloWorldUser = cfp(fetchUser, user => user.then(({ name }) => `Hello world, ${name}!`))
assert.strictEqual(await helloWorldUser({ fetchUser: async () => ({ name: 'Vasya' }) }), 'Hello world, Vasya!')
})
test('cfp generics 1', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn = cfp(fn1, ({ a }) => a + 1)
assert.strictEqual(fn({ ctx: 1 }), 2)
})
test('cfp generics 2', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn = cfp(fn1, fn2, ({ a }, { b }) => a + b + 1)
assert.strictEqual(fn({ ctx: 1 }), 3)
})
test('cfp generics 3', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn = cfp(fn1, fn2, fn3, ({ a }, { b }, { c }) => a + b + c + 1)
assert.strictEqual(fn({ ctx: 1 }), 4)
})
test('cfp generics 4', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn = cfp(fn1, fn2, fn3, fn4, ({ a }, { b }, { c }, { d }) => a + b + c + d + 1)
assert.strictEqual(fn({ ctx: 1 }), 5)
})
test('cfp generics 5', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn = cfp(fn1, fn2, fn3, fn4, fn5, ({ a }, { b }, { c }, { d }, { e }) => a + b + c + d + e + 1)
assert.strictEqual(fn({ ctx: 1 }), 6)
})
test('cfp generics 6', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn6 = ({ ctx }: { ctx: number }) => ({ f: ctx })
const fn = cfp(fn1, fn2, fn3, fn4, fn5, fn6, ({ a }, { b }, { c }, { d }, { e }, { f }) => a + b + c + d + e + f + 1)
assert.strictEqual(fn({ ctx: 1 }), 7)
})
test('cfp generics 7', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn6 = ({ ctx }: { ctx: number }) => ({ f: ctx })
const fn7 = ({ ctx }: { ctx: number }) => ({ g: ctx })
const fn = cfp(
fn1,
fn2,
fn3,
fn4,
fn5,
fn6,
fn7,
({ a }, { b }, { c }, { d }, { e }, { f }, { g }) => a + b + c + d + e + f + g + 1
)
assert.strictEqual(fn({ ctx: 1 }), 8)
})
test('cfp generics 8', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn6 = ({ ctx }: { ctx: number }) => ({ f: ctx })
const fn7 = ({ ctx }: { ctx: number }) => ({ g: ctx })
const fn8 = ({ ctx }: { ctx: number }) => ({ h: ctx })
const fn = cfp(
fn1,
fn2,
fn3,
fn4,
fn5,
fn6,
fn7,
fn8,
({ a }, { b }, { c }, { d }, { e }, { f }, { g }, { h }) => a + b + c + d + e + f + g + h + 1
)
assert.strictEqual(fn({ ctx: 1 }), 9)
})
test('cfp generics 9', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn6 = ({ ctx }: { ctx: number }) => ({ f: ctx })
const fn7 = ({ ctx }: { ctx: number }) => ({ g: ctx })
const fn8 = ({ ctx }: { ctx: number }) => ({ h: ctx })
const fn9 = ({ ctx }: { ctx: number }) => ({ i: ctx })
const fn = cfp(
fn1,
fn2,
fn3,
fn4,
fn5,
fn6,
fn7,
fn8,
fn9,
({ a }, { b }, { c }, { d }, { e }, { f }, { g }, { h }, { i }) => a + b + c + d + e + f + g + h + i + 1
)
assert.strictEqual(fn({ ctx: 1 }), 10)
})
test('cfp generics 10', () => {
const fn1 = ({ ctx }: { ctx: number }) => ({ a: ctx })
const fn2 = ({ ctx }: { ctx: number }) => ({ b: ctx })
const fn3 = ({ ctx }: { ctx: number }) => ({ c: ctx })
const fn4 = ({ ctx }: { ctx: number }) => ({ d: ctx })
const fn5 = ({ ctx }: { ctx: number }) => ({ e: ctx })
const fn6 = ({ ctx }: { ctx: number }) => ({ f: ctx })
const fn7 = ({ ctx }: { ctx: number }) => ({ g: ctx })
const fn8 = ({ ctx }: { ctx: number }) => ({ h: ctx })
const fn9 = ({ ctx }: { ctx: number }) => ({ i: ctx })
const fn10 = ({ ctx }: { ctx: number }) => ({ j: ctx })
const fn = cfp(
fn1,
fn2,
fn3,
fn4,
fn5,
fn6,
fn7,
fn8,
fn9,
fn10,
({ a }, { b }, { c }, { d }, { e }, { f }, { g }, { h }, { i }, { j }) => a + b + c + d + e + f + g + h + i + j + 1
)
assert.strictEqual(fn({ ctx: 1 }), 11)
})
test('cfp context is primitive', async () => {
const incNumber = (number: number) => number + 1
const stringPostfix = () => 'postfix'
const positiveNumbersAsString = cfp(incNumber, stringPostfix, (ns, postfix) => `${ns.toString()} ${postfix}`)
const resp = positiveNumbersAsString(1)
assert.equal(resp, '2 postfix')
})
test('sfp basic', () => {
const numbers = sfp((ns: number[], n: number) => [...ns, n], [])
numbers(1)
numbers(2)
numbers(3)
assert.strictEqual(numbers().length, 3)
assert.deepStrictEqual(numbers(), [1, 2, 3])
})
test('sfp with cfp', () => {
const numbers = ({ incNumber }: { incNumber: number }) => sfp((ns: number[], n: number) => [...ns, n + incNumber], [])
const addNumber1 = cfp(numbers, ns => ns(1))
const addNumber2 = cfp(numbers, ns => ns(2))
const addNumber3 = cfp(numbers, ns => ns(3))
const numbersToString = cfp(numbers, addNumber1, addNumber2, addNumber3, ns => ns().toString())
assert.strictEqual(numbersToString({ incNumber: 1 }), '2,3,4')
})