-
Notifications
You must be signed in to change notification settings - Fork 86
/
composeWith.spec.js
51 lines (44 loc) · 1.37 KB
/
composeWith.spec.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
import { always, identity, inc, isNil, map, modulo, multiply } from 'rambdax'
import { composeWith as composeWithRamda, concat, flip, ifElse } from 'ramda'
import { composeWith } from './composeWith.js'
test('performs right-to-left function composition with function applying', () => {
const f = composeWith((f, res) => f(res))([ map, multiply, parseInt ])
expect(f).toHaveLength(2)
expect(f('10')([ 1, 2, 3 ])).toEqual([ 10, 20, 30 ])
expect(f('10', 2)([ 1, 2, 3 ])).toEqual([ 2, 4, 6 ])
})
test('performs right-to-left function while not nil result', () => {
const isOdd = flip(modulo)(2)
const composeWhenNotNil = composeWithRamda((f, res) =>
isNil(res) ? null : f(res))
const f = composeWhenNotNil([
inc,
ifElse(
isOdd, identity, always(null)
),
parseInt,
])
expect(f).toHaveLength(2)
expect(f('1')).toBe(2)
expect(f('2')).toBeNull()
})
test('performs right-to-left function using promise chaining', () => {
const then = function (f, p){
return p.then(f)
}
const composeP = composeWithRamda(then)
const toListPromise = function (a){
return new Promise(res => {
res([ a ])
})
}
const doubleListPromise = function (a){
return new Promise(res => {
res(concat(a, a))
})
}
const f = composeP([ doubleListPromise, toListPromise ])
return f(1).then(res => {
expect(res).toEqual([ 1, 1 ])
})
})