-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTransducer.test.js
154 lines (140 loc) · 4.42 KB
/
Transducer.test.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
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
require('./global')
const Transducer = require('./Transducer')
const assert = require('assert')
describe('Transducer', () => {
const ade = assert.deepEqual
const ase = assert.strictEqual
const aok = assert.ok
const concat = (y, xi) => y.concat(xi)
const add = (y, xi) => y + xi
const isOdd = number => number % 2 == 1
it('reduce with sync transduced reducers', async () => {
const squareOdds = compose([
Transducer.filter(isOdd),
Transducer.map(x => x ** 2),
])
ade(
reduce(squareOdds(concat), [])([1, 2, 3, 4, 5]),
[1, 9, 25],
)
ade(
reduce(squareOdds((y, xi) => y.add(xi)), new Set())([1, 2, 3, 4, 5]),
new Set([1, 9, 25]),
)
const appendAlphas = compose([
Transducer.map(x => `${x}a`),
Transducer.map(x => `${x}b`),
Transducer.map(x => `${x}c`),
])
ase(
reduce(appendAlphas(add), '')('123'),
'1abc2abc3abc',
)
ade(
reduce(appendAlphas(concat), [])('123'),
['1abc', '2abc', '3abc'],
)
})
it('reduce with an async transduced reducer', async () => {
const hosWithHey = compose([
Transducer.filter(async x => x === 'ho'),
Transducer.map(x => Promise.resolve(`${x}hey`)),
])
const hihos = { a: 'hi', b: 'ho', c: 'hi', d: 'ho', e: 'hi', f: 'ho' }
aok(reduce(hosWithHey(add), '')(hihos) instanceof Promise),
aok(reduce(hosWithHey(concat), [])(hihos) instanceof Promise),
ase(
await reduce(hosWithHey(add), '')(hihos),
'hoheyhoheyhohey',
)
ade(
await reduce(hosWithHey(concat), [])(hihos),
['hohey', 'hohey', 'hohey'],
)
})
it('transform with error handling on a transducer composition', async () => {
const caughtErrors = []
const numbers = [1, 2, 3, 4, 5]
const result = await transform(numbers, Transducer.tryCatch(compose([
Transducer.filter(number => number % 2 == 1),
Transducer.map(number => number ** 2),
Transducer.map(number => {
throw new Error(number)
}),
Transducer.map(number => number * 10), // should never reach here
]), (error, item) => {
caughtErrors.push(error)
return item
}), [])
assert.equal(caughtErrors.length, 3)
assert.deepEqual(result, [1, 3, 5])
assert.deepEqual(caughtErrors.map(get('message')), ['1', '9', '25'])
})
it('transform with error handling on a transducer in a composition', async () => {
const caughtErrors = []
const numbers = [1, 2, 3, 4, 5]
const result = await transform(numbers, compose([
Transducer.filter(number => number % 2 == 1),
Transducer.map(number => number ** 2),
Transducer.tryCatch(
Transducer.map(number => {
throw new Error(number)
}),
(error, item) => {
caughtErrors.push(error)
return item
},
),
Transducer.map(number => number * 10), // should reach here with error handler
]), [])
assert.equal(caughtErrors.length, 3)
assert.deepEqual(result, [10, 90, 250])
assert.deepEqual(caughtErrors.map(get('message')), ['1', '9', '25'])
})
it('flatMapping transducer', async () => {
const createOddMultiplesSync = compose([
Transducer.filter(isOdd),
Transducer.flatMap(number => [number * 2, number * 3]),
])
ase(
reduce([1, 2, 3], createOddMultiplesSync(add), 0),
2 + 3 + 6 + 9,
)
ade(
reduce([1, 2, 3], createOddMultiplesSync(concat), []),
[2, 3, 6, 9],
)
const createOddMultiplesAsync = compose([
Transducer.filter(isOdd),
Transducer.flatMap(async number => [number * 2, number * 3]),
])
const p1 = reduce([1, 2, 3], createOddMultiplesAsync(add), 0)
aok(p1 instanceof Promise)
const p2 = reduce([1, 2, 3], createOddMultiplesAsync(concat), [])
aok(p2 instanceof Promise)
await p1.then(result => {
ase(result, 2 + 3 + 6 + 9)
})
await p2.then(result => {
ade(result, [2, 3, 6, 9])
})
})
it('forEach transducer', async () => {
let sum1 = 0
reduce([1, 2, 3, 4, 5], Transducer.forEach(number => {
sum1 += number
})(() => {}), null)
assert.equal(sum1, 15)
let sum2 = 0
await reduce([1, 2, 3, 4, 5], Transducer.forEach(async number => {
sum2 += number
})(() => {}), null)
assert.equal(sum2, 15)
})
it('passthrough transducer', async () => {
assert.deepEqual(
transform({ a: 1, b: 2, c: 3 }, Transducer.passthrough, []),
[1, 2, 3],
)
})
})