-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.ts
302 lines (287 loc) · 10.1 KB
/
maps.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import * as Iterables from './iterables'
/**
* Creates a map from the source iterable object.
* NOTE: Items with duplicate keys will be ignored.
* @param source An iterable objext of tuples to convert to a map.
*/
export function ofIterable<Key, T>(source: Iterable<[Key, T]>): Map<Key, T> {
return new Map(source)
}
/**
* Creates a map from the source array object.
* NOTE: Items with duplicate keys will be ignored.
* @param source An array of tuples to convert to a map.
*/
export function ofArray<Key, T>(source: ReadonlyArray<[Key, T]>): Map<Key, T> {
return new Map(source)
}
/**
* Creates a map from the source set object.
* @param source A set objext to convert to a map.
*/
export function ofSet<T>(source: ReadonlySet<T>): Map<T, T> {
return new Map(source.entries())
}
/**
* Returns the source, but with the type restricted to being only iterable of the map's entries.
* @param source A map objext to return as an iterable.
*/
export function asIterable<Key, T>(source: ReadonlyMap<Key, T>): Iterable<[Key, T]> {
return source
}
/**
* Creates a new map whose values are the results of applying the specified mapping to each of the values of the source map.
* @param source The input collection.
* @param mapping A function to transform entries from the input collection into new values.
*/
export function map<Key, T, U>(
source: ReadonlyMap<Key, T>,
mapping: (key: Key, value: T) => U
): Map<Key, U>
/**
* Creates a new map whose values are the results of applying the specified mapping to each of the values of the source map.
* @param mapping A function to transform entries from the input collection into new values.
*/
export function map<Key, T, U>(
mapping: (key: Key, value: T) => U
): (source: ReadonlyMap<Key, T>) => Map<Key, U>
export function map<Key, T, U>(a: any, b?: any): any {
const partial = typeof a === 'function'
const mapping: (key: Key, value: T) => U = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
const target = new Map<Key, U>()
for (const pair of source.entries()) {
const key = pair[0]
const mapped = mapping(key, pair[1])
target.set(key, mapped)
}
return target
}
return partial ? exec : exec(a)
}
/**
* Returns a new map containing only the elements of the map for which the given predicate returns true.
* @param source The input collection.
* @param predicate A function to test whether each item in the input map should be included in the output map.
*/
export function filter<Key, T>(
source: ReadonlyMap<Key, T>,
predicate: (key: Key, value: T) => boolean
): Map<Key, T>
/**
* Returns a new map containing only the elements of the map for which the given predicate returns true.
* @param predicate A function to test whether each item in the input map should be included in the output map.
*/
export function filter<Key, T>(
predicate: (key: Key, value: T) => boolean
): (source: ReadonlyMap<Key, T>) => Map<Key, T>
export function filter<Key, T>(a: any, b?: any): any {
const partial = typeof a === 'function'
const predicate: (key: Key, value: T) => boolean = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
return new Map<Key, T>(
Iterables.filter(source.entries(), (entry) => predicate(entry[0], entry[1]))
)
}
return partial ? exec : exec(a)
}
/**
* Applies the given function to each entry of the map and returns a new map comprised of the results for each element where the function returns a value.
* @param source The input collection.
* @param chooser A function to transform entries from the input map to a new value to be included, or undefined to be excluded.
*/
export function choose<Key, T, U>(
source: ReadonlyMap<Key, T>,
chooser: (key: Key, value: T) => U | undefined
): Map<Key, U>
/**
* Applies the given function to each entry of the map and returns a new map comprised of the results for each element where the function returns a value.
* @param chooser A function to transform entries from the input map to a new value to be included, or undefined to be excluded.
*/
export function choose<Key, T, U>(
chooser: (key: Key, value: T) => U | undefined
): (source: ReadonlyMap<Key, T>) => Map<Key, U>
export function choose<Key, T, U>(a: any, b?: any): any {
const partial = typeof a === 'function'
const chooser: (key: Key, value: T) => U | undefined = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
const target = new Map<Key, U>()
for (const pair of source.entries()) {
const key = pair[0]
const mapped = chooser(key, pair[1])
if (mapped !== undefined) {
target.set(key, mapped)
}
}
return target
}
return partial ? exec : exec(a)
}
/**
* Wraps the two given maps as a single concatenated map.
* @param first The first map.
* @param second The second map.
*/
export function append<Key, T>(first: ReadonlyMap<Key, T>, second: ReadonlyMap<Key, T>): Map<Key, T>
/**
* Wraps the two given maps as a single concatenated map.
* @param second The second map.
* @param first The first map.
*/
export function append<Key, T>(
second: ReadonlyMap<Key, T>
): (first: ReadonlyMap<Key, T>) => Map<Key, T>
export function append<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const second: Map<Key, T> = partial ? a : b
function exec(first: Map<Key, T>): Map<Key, T> {
return new Map<Key, T>(Iterables.append(first, second))
}
return partial ? exec : exec(a)
}
/**
* Combines the given collection-of-maps as a single concatenated map.
* NOTE: Duplicate items will be ignored.
* @param sources The input collection.
*/
export function concat<Key, T>(sources: Iterable<ReadonlyMap<Key, T>>): Map<Key, T> {
const target = new Map<Key, T>()
for (const source of sources) {
for (const item of source) {
target.set(item[0], item[1])
}
}
return target
}
/**
* Returns the value for the given key.
* @param source The input collection.
* @param key The key to lookup in the map.
* @throws If the key does not exist in the source collection.
*/
export function get<Key, T>(source: ReadonlyMap<Key, T>, key: Key): T
/**
* Returns the value for the given key.
* @param key The key to lookup in the map.
* @param source The input collection.
* @throws If the key does not exist in the source collection.
*/
export function get<Key, T>(key: Key): (source: ReadonlyMap<Key, T>) => T
export function get<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const key: Key = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
if (!source.has(key)) {
throw new Error('Specified key not found')
}
return source.get(key) as T
}
return partial ? exec : exec(a)
}
/**
* Returns the value for the given key, or undefined if not found.
* @param source The input collection.
* @param key The key to lookup in the map.
*/
export function find<Key, T>(source: ReadonlyMap<Key, T>, key: Key): T | undefined
/**
* Returns the value for the given key, or undefined if not found.
* @param key The key to lookup in the map.
* @param source The input collection.
*/
export function find<Key, T>(key: Key): (source: ReadonlyMap<Key, T>) => T | undefined
export function find<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const key: Key = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
return source.get(key)
}
return partial ? exec : exec(a)
}
/**
* Tests if any element of the map satisfies the given predicate.
* @param source The input collection.
* @param predicate A function to test each item of the input collection.
*/
export function exists<Key, T>(
source: ReadonlyMap<Key, T>,
predicate: (key: Key, value: T) => boolean
): boolean
/**
* Tests if any element of the map satisfies the given predicate.
* @param predicate A function to test each item of the input collection.
* @param source The input collection.
*/
export function exists<Key, T>(
predicate: (key: Key, value: T) => boolean
): (source: ReadonlyMap<Key, T>) => boolean
export function exists<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const predicate: (key: Key, value: T) => boolean = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
for (const item of source) {
if (predicate(item[0], item[1])) {
return true
}
}
return false
}
return partial ? exec : exec(a)
}
/**
* Tests if every element of the map satisfies the given predicate.
* @param source The input collection.
* @param predicate A function to test each item of the input collection.
*/
export function every<Key, T>(
source: ReadonlyMap<Key, T>,
predicate: (key: Key, value: T) => boolean
): boolean
/**
* Tests if every element of the map satisfies the given predicate.
* @param predicate A function to test each item of the input collection.
* @param source The input collection.
*/
export function every<Key, T>(
predicate: (key: Key, value: T) => boolean
): (source: ReadonlyMap<Key, T>) => boolean
export function every<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const predicate: (key: Key, value: T) => boolean = partial ? a : b
function exec(source: ReadonlyMap<Key, T>) {
for (const item of source) {
if (!predicate(item[0], item[1])) {
return false
}
}
return true
}
return partial ? exec : exec(a)
}
/**
* Evaluates to true if the given key is in the source map.
* @param source The input collection.
* @param key The key to look for.
*/
export function containsKey<Key, T>(source: ReadonlyMap<Key, T>, key: Key): boolean
/**
* Evaluates to true if the given key is in the source map.
* @param key The key to look for.
* @param source The input collection.
*/
export function containsKey<Key, T>(key: Key): (source: ReadonlyMap<Key, T>) => boolean
export function containsKey<Key, T>(a: any, b?: any): any {
const partial = b === undefined
const key: Key = partial ? a : b
function exec(source: ReadonlyMap<Key, T>): boolean {
return source.has(key)
}
return partial ? exec : exec(a)
}
/**
* Returns the number of items in the collection.
* @param source The input collection.
*/
export function count<Key, T>(source: ReadonlyMap<Key, T>): number {
return source.size
}