forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assoc.spec.ts
128 lines (87 loc) · 3.87 KB
/
assoc.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {map, assoc as assoc} from '../../src/associative'
import { flow } from '../../src/composition'
/**
* tsfun | update
*
* As opposed to *update*, which views its data as
* Struct, i.e. as Tuple, Interface or nested structure,
* *update_a* works with homogeneous Map-s and Array-s.
*/
describe('update', () => {
const toString = (x: number) => x.toString()
const inc = (x: number) => x + 1
it('Map and Array', () => {
const $1 /*: Map<number>*/ = assoc('b', inc /*update*/, {a: 4, b: 7})
expect($1).toEqual({a: 4, b: 8})
const $2 /*: Map<number>*/ = assoc('b', 8 /*assoc*/, {a: 4, b: 7})
expect($2).toEqual({a: 4, b: 8})
const $3 /*: Map<number>*/ = assoc('b', inc /*update*/)({a: 4, b: 7})
expect($3).toEqual({a: 4, b: 8})
const $4 /*: Map<number>*/ = assoc('b', 8 /*assoc*/)({a: 4, b: 7})
expect($4).toEqual({a: 4, b: 8})
const $5 /*: Array<number>*/ = assoc(1, inc /*update*/, [4, 7])
expect($5).toEqual([4, 8])
const $6 /*: Array<number>*/ = assoc(1, 8 /*assoc*/, [4, 7])
expect($6).toEqual([4, 8])
const $7 /*: Array<number>*/ = assoc(1, inc /*update*/)([4, 7])
expect($7).toEqual([4, 8])
const $8 /*: Array<number>*/ = assoc(1, 8 /*assoc*/)([4, 7])
expect($8).toEqual([4, 8])
})
it('Types changed', () => {
const $1 /*: Map<string|number>*/ = assoc('b', toString /*update*/, {a: '4', b: '7'})
expect($1).toEqual({a: '4', b: '7'})
const $2 /*: Map<string|number>*/ = assoc('b', 'a' /*assoc*/, {a: 4, b: 7})
expect($2).toEqual({a: 4, b: 'a'})
const $3 /*: Map<string|number>*/ = assoc('b', toString /*update*/)({a: 4, b: 7})
expect($3).toEqual({a: 4, b: '7'})
const $4 /*: Map<string|number>*/ = assoc('b', 'a' /*assoc*/)({a: 4, b: 7})
expect($4).toEqual({a: 4, b: 'a'})
const $5 /*: Array<string|number>*/ = assoc(1, toString /*update*/, [4, 7])
expect($5).toEqual([4, '7'])
const $6 /*: Array<string|number>*/ = assoc(1, 'a' /*assoc*/, [4, 7])
expect($6).toEqual([4, 'a'])
const $7 /*: Array<string|number>*/ = assoc(1, toString /*update*/)([4, 7])
expect($7).toEqual([4, '7'])
const $8 /*: Array<string|number>*/ = assoc(1, 'a' /*assoc*/)([4, 7])
expect($8).toEqual([4, 'a'])
const $11 /*: Map<string|number|undefined>*/ = assoc('b', _ => JSON.stringify(_), {a: 7, b: undefined})
})
it('Associative', () => {
// It works for both Array and Map so that one can
// work with them just as Associative in a composition
const $1 /*: Map<number>*/ =
flow(
{a: 4, b: 7},
assoc('b', 8 /*assoc*/))
expect($1).toEqual({a: 4, b: 8})
const $2 /*: Array<number>*/ =
flow(
[4, 7],
assoc(1, 8 /*assoc*/))
expect($2).toEqual([4, 8])
const $3 /*: Map<number>*/ =
flow(
{a: 4, b: 7},
assoc('b', inc /*update*/))
expect($3).toEqual({a: 4, b: 8})
const $4 /*: Array<number>*/ =
flow(
[4, 7],
assoc(1, inc /*update*/))
expect($4).toEqual([4, 8])
})
it('array - ignore undefined indices', () => {
const result = assoc(3, 8)([11, 12])
expect(result[3]).toEqual(8)
expect(result[2]).toBeUndefined()
})
it('pitfall', () => {
const result = map(assoc('a', {}))([{a: 1}, {a: 1}]) as Array<{a: number}>
expect(result[0].a).toBe(result[1].a) // this is possibly not be what one wants
// to circumvent this, we use it like this
const result2 = map(assoc('a', () => ({})))([{a: 1}, {a: 1}]) as Array<{a: any}>
expect(result2[0].a).toEqual({})
expect(result2[0].a).not.toBe(result[1].a)
})
})