-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.test.ts
182 lines (161 loc) · 4.65 KB
/
object.test.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as object from './object.ts'
import { describe, it } from 'node:test'
import * as assert from 'assert'
import { Binary } from './data.ts'
import { gt } from './maths.ts'
const str = (x: any): string => x + ''
const concat: Binary<string, string, string> = a => b => a + b
describe('object', () => {
describe('entries', () => {
it('should turn objects into arrays of tuples', () => {
assert.deepEqual(
object.entries({ a: 1, b: 2}),
[['a', 1], ['b', 2]]
)
})
it('should turn empty objects into empty arrays', () => {
assert.deepEqual(
object.entries({}),
[]
)
})
})
describe('fromEntries', () => {
it('should turn arrays of tuples into objects', () => {
assert.deepEqual(
object.fromEntries([['a', 1], ['b', 2]]),
{ a: 1, b: 2 }
)
})
it('should turn empty arrays into empty objects', () => {
assert.deepEqual(
object.fromEntries([]),
{}
)
})
})
describe('values', () => {
it('should return the values of an object as an array', () => {
assert.deepEqual(
object.values({a: 1, b: 2}),
[1,2]
)
})
it('should return an empty array for empty objects', () => {
assert.deepEqual(
object.values({}),
[]
)
})
})
describe('map', () => {
it('should map objects of As into objects of Bs', () => {
assert.deepEqual(
object.map(str)({a: 1, b: 2}),
{a: '1', b: '2'}
)
})
it('should do nothing to empty objects', () => {
assert.deepEqual(
object.map(str)({}),
{}
)
})
})
describe('filter', () => {
it('should remove keys and values', () => {
assert.deepEqual(
object.filter(gt(0))({ a: 1, b: -1 }),
{a: 1}
)
})
it('should do nothing on empty objects', () => {
assert.deepEqual(
object.filter(gt(0))({}),
{}
)
})
})
describe('filterWithKeys', () => {
it('should remove keys and values', () => {
assert.deepEqual(
object.filterWithKeys(k => v => k[0] !== '!')({a: 1, '!a': 2}),
{a:1}
)
})
})
describe('foldr', () => {
it('should combine on the right', () => {
assert.equal(
object.foldr(concat, '')({a: 'a', b: 'b'}),
'ba'
)
})
it('should return the default value on empty objects', () => {
assert.equal(
object.foldr(concat, '')({}),
''
)
})
})
describe('foldrWithKeys', () => {
it('should combine on the right with keys', () => {
assert.equal(
object.foldrWithKeys(k => v => a => a + k + v, '')({
'test': 1,
'best': 2,
}),
'test1best2'
)
})
})
describe('foldl', () => {
it('should combine on the left', () => {
assert.equal(
object.foldl(concat, '')({a: 'a', b: 'b'}),
'ab'
)
})
it('should return the default value on empty objects', () => {
assert.equal(
object.foldl(concat, '')({}),
''
)
})
})
describe('foldrWithKeys', () => {
it('should combine on the left with keys', () => {
assert.equal(
object.foldlWithKeys(a => k=> v => a + k + v, '')({
'test': 1,
'best': 2,
}),
'test1best2'
)
})
})
describe('into', () => {
it('should update a single object key/value', () => {
assert.deepEqual(
object.into({} as any)('test')(1),
{
test: 1,
}
)
})
})
describe('len', () => {
it('should return an object\'s length', () => {
assert.equal(
object.len({ a: 1 }),
1
)
})
it('should consider objects to have zero length', () => {
assert.equal(
object.len({}),
0
)
})
})
})