-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestMap.ts
253 lines (211 loc) · 7.15 KB
/
TestMap.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* eslint-disable no-magic-numbers */
import { expect } from 'chai';
import { NotFoundError } from '../../src/error/NotFoundError';
import {
entriesOf,
getHead,
getHeadOrDefault,
getOrDefault,
makeDict,
makeMap,
mergeMap,
mustGet,
normalizeMap,
pairsToMap,
pushMergeMap,
setOrPush,
} from '../../src/Map';
const DEFAULT_VALUE = 'default';
const mapKey = 'key';
const mapValue = 'value';
const singleItem = new Map([[mapKey, mapValue]]);
const multiItem = new Map([
[mapKey, [mapValue]],
// eslint-disable-next-line no-null/no-null,@typescript-eslint/no-explicit-any
['nilKey', null as any],
// eslint-disable-next-line no-null/no-null
['nilValue', [null]],
]);
describe('map utils', async () => {
describe('make dict', async () => {
it('should return an empty dict for nil values', async () => {
/* eslint-disable-next-line no-null/no-null */
expect(makeDict(null)).to.deep.equal({});
expect(makeDict(undefined)).to.deep.equal({});
});
it('should return an existing dict', async () => {
const input = {};
expect(makeDict(input)).to.equal(input);
});
});
describe('make map', async () => {
it('should convert objects to maps', async () => {
const data = {
bar: '2',
foo: '1',
};
const map = makeMap(data);
expect(Array.from(map.entries())).to.deep.equal(Object.entries(data));
});
});
describe('must get helper', async () => {
it('should get existing keys', async () => {
expect(mustGet(singleItem, mapKey)).to.equal(mapValue);
});
it('should throw on missing keys', async () => {
expect(() => {
mustGet(singleItem, 'nope');
}).to.throw(NotFoundError);
});
});
describe('get head helper', async () => {
it('should get the first item from existing keys', async () => {
expect(getHead(multiItem, mapKey)).to.equal(mapValue);
});
it('should throw on missing keys', async () => {
expect(() => {
getHead(multiItem, 'nope');
}).to.throw(NotFoundError);
});
});
describe('get head or default helper', async () => {
it('should get the first item from existing keys', async () => {
expect(getHeadOrDefault(multiItem, mapKey, 'nope')).to.equal(mapValue);
});
it('should get the default for missing keys', async () => {
expect(getHeadOrDefault(multiItem, 'nope', mapValue)).to.equal(mapValue);
});
it('should return the default value for nil values', async () => {
expect(getHeadOrDefault(multiItem, 'nilValue', mapValue)).to.equal(mapValue);
});
it('should return the default value for nil keys', async () => {
expect(getHeadOrDefault(multiItem, 'nilKey', mapValue)).to.equal(mapValue);
});
});
describe('get or default helper', () => {
it('should get the item for existing keys', () => {
expect(getOrDefault(singleItem, mapKey, DEFAULT_VALUE)).to.equal(mapValue);
});
it('should get the item for missing keys', () => {
expect(getOrDefault(singleItem, 'missing', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE);
});
it('should return the default for nil values', () => {
expect(getOrDefault(multiItem, 'nilKey', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE);
});
xit('should return falsy values for existing keys');
});
describe('pairs to map helper', () => {
it('should convert pairs', () => {
const result = pairsToMap([{
name: 'foo',
value: 3,
}]);
expect(result.get('foo')).to.equal(3);
});
});
describe('entries of helper', () => {
it('should return entries for maps', () => {
const input: Array<[string, number]> = [['foo', 1], ['bar', 3]];
const value = new Map(input);
const result = entriesOf(value);
expect(result.length).to.equal(2);
expect(result).to.deep.equal(input);
});
it('should return entries for objects', () => {
const result = entriesOf({
bar: 3,
foo: 1,
});
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('bar');
});
it('should return empty entries for nil values', () => {
/* eslint-disable-next-line no-null/no-null */
expect(entriesOf(null)).to.deep.equal([]);
expect(entriesOf(undefined)).to.deep.equal([]);
});
});
describe('set or push helper', () => {
xit('should insert new lists');
it('should wrap new items', () => {
const values = new Map<string, Array<string>>([]);
setOrPush(values, mapKey, 'fin');
expect(Array.from(values.keys()).length).to.equal(1);
expect(mustGet(values, mapKey).length).to.equal(1);
});
it('should append to existing items', () => {
const values = new Map(multiItem);
setOrPush(values, mapKey, 'fin');
expect(Array.from(values.keys()).length).to.equal(3);
expect(mustGet(values, mapKey).length).to.equal(2);
});
});
describe('merge map helper', () => {
it('should combine two maps', () => {
const firstValue = new Map([[mapKey, mapValue]]);
const otherValue = new Map([['foo', 'bar']]);
const merged = mergeMap(firstValue, otherValue);
expect(Array.from(merged.keys()).length).to.equal(2);
});
});
describe('push merge map helper', () => {
it('should combine two overlapping maps', () => {
const firstValue = new Map([[mapKey, [1]]]);
const otherValue = new Map([[mapKey, [2, 3]]]);
const merged = pushMergeMap(firstValue, otherValue);
expect(merged).to.have.keys(mapKey);
expect(merged.get(mapKey)).to.deep.equal([1, 2, 3]);
});
});
describe('make map helper', () => {
it('should make an empty map', () => {
expect(makeMap(undefined).size).to.equal(0);
});
it('should copy an existing map', () => {
const copy = makeMap(singleItem);
expect(copy).to.deep.equal(singleItem);
expect(copy).not.to.equal(singleItem);
});
it('should copy a dict', () => {
expect(makeMap({
[mapKey]: mapValue,
})).to.deep.equal(singleItem);
});
});
describe('normalize map helper', () => {
it('should convert values into arrays of strings', () => {
const banVal = [Symbol()];
const initial = new Map<string, unknown>([
['bar', 'bin'],
['ban', banVal],
['toad', {
toString() {
return 'too';
},
}],
]);
const normalized = normalizeMap(initial);
expect(normalized.bar).to.deep.equal(['bin']);
expect(normalized.ban).to.equal(banVal);
expect(normalized.toad).to.deep.equal(['too']);
});
it('should convert number arguments into string values', () => {
const INPUT_VALUE = 123;
const input = new Map([
['foo', INPUT_VALUE],
]);
const normalized = normalizeMap(input);
expect(normalized.foo).to.deep.equal([INPUT_VALUE.toString()]);
});
it('should convert object arguments into string values', () => {
const OUTPUT_VALUE = 'bar';
const input = new Map([
['foo', {
toString: () => OUTPUT_VALUE,
}],
]);
const normalized = normalizeMap(input);
expect(normalized.foo).to.deep.equal([OUTPUT_VALUE]);
});
});
});