forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.spec.ts
85 lines (52 loc) · 2.04 KB
/
reduce.spec.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
import {Map} from '../../src/type'
import { map, reduce, stop } from '../../src/associative'
/**
* tsfun | reduce
*/
describe('reduce', () => {
it('Array', () =>
expect(
reduce((b: number, a: number, i: number) => b + a + i, 0)([1, 5, 6]))
.toBe(15))
it('Map', () =>
expect(
reduce((b: number, a: number, _k: string) => b + a, 0)({a: 1, b: 5, c: 6}))
.toBe(12))
it('Array - single argument list', () =>
expect(
reduce([1, 5, 6], (b: number, a: number) => b + a, 0))
.toBe(12))
it('Map - single argument list', () =>
expect(
reduce({a: 1, b: 5, c: 6}, (b: number, a: number) => b + a, 0))
.toBe(12))
it('Stop', () =>
expect(
reduce((b: number, a: number) => {
if (b > 1) stop(10)
return a + b
}, 0)({a: 1, b: 5, c: 6}))
.toBe(10))
it('in combination with map', () => {
const f = (b: Map<true>, a: string) => (b[a] = true, b)
const $ = map([['a'], ['b']],
reduce(f, {} /* this here */))
// This is certainly not what we want
expect($).toEqual([{a: true, b: true}, {a: true, b: true}])
expect($[0]).toBe($[1])
// The reason is, that the empty array literal passed to reduce is one and the same instance
// Avoid this by using it like this
const $1 = map([['a'], ['b']], reduce(f, () => ({} as Map<true>) /* this here */))
expect($1).toEqual([{a: true}, {b: true}])
expect($1[0]).not.toBe($1[1])
// or by doing this
const $2 = map([['a'], ['b']], _ => reduce(_, f, {}))
expect($2).toEqual([{a: true}, {b: true}])
expect($2[0]).not.toBe($2[1])
// which obviously could be done for arrays like this
const $3 = map([['a'], ['b']], _ => _.reduce(f, {}))
expect($3).toEqual([{a: true}, {b: true}])
expect($3[0]).not.toBe($3[1])
// but which does not work for Map, obviously
})
})