forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_each.spec.ts
106 lines (74 loc) · 2.36 KB
/
for_each.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {expectNever, expectType} from 'ts-expect'
import {Expect, Map} from '../../src/type'
import {forEach} from '../../src/associative'
/**
* tsfun | forEach
*/
describe('forEach', () => {
it('forEach - Array', () => {
let acc = 1
const items = forEach([2, 4, 3], item => (acc += item))
expect(items).toEqual([2, 4, 3])
expect(acc).toEqual(10)
})
it('forEach with i', () => {
let acc = 1
forEach([2, 4, 3], (_item, i) => {
acc += i
})
expect(acc).toEqual(4)
})
it('forEach - Map', () => {
let acc = 1
const items = forEach({a: 2, b: 4, c: 3}, item => (acc += item))
expect(items).toEqual({a: 2, b: 4, c: 3})
expect(acc).toEqual(10)
})
it('forEach - with k', () => {
let acc = 1
const items = forEach({a: 2, b: 4, c: 3}, item => {
acc += item
})
expect(items).toEqual({a: 2, b: 4, c: 3})
expect(acc).toEqual(10)
})
it('forEach - Map - curried', () => {
let acc = 1
const items = forEach((item: number) => {
acc += item
})([2, 4, 3])
expect(items).toEqual([2, 4, 3])
expect(acc).toEqual(10)
})
it('forEach with i - curried', () => {
let acc = 1
forEach((item, i: number) => (acc += i))([2, 4, 3])
expect(acc).toEqual(4)
})
it('forEach - Map - curried', () => {
let acc = 1
const items = forEach((item: number) => {
acc += item
})({a: 2, b: 4, c: 3})
expect(items).toEqual({a: 2, b: 4, c: 3})
expect(acc).toEqual(10)
})
it('forEach - with k - curried', () => {
let acc = 1
const items = forEach((item: number) => {
acc += item
})({a: 2, b: 4, c: 3})
expect(items).toEqual({a: 2, b: 4, c: 3})
expect(acc).toEqual(10)
})
it('typing', () => {
const $1 = forEach((_: number) => _ * 2)([1,2,3])
expectType<Array<number>>($1)
const $2 = forEach((_: number) => {})({a: 1, b: 2})
expectType<Map<number>>($2)
const $3: Expect<Map<number>,typeof $2> = true
const $36 = forEach((x: string) => true)([1, 2])
const $37: void[] = $36
})
// TODO test keys passed to f, curried as well as uncurried, with maps and arrays
})