-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
596 lines (525 loc) · 24.2 KB
/
index.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import {filter, map} from "@softwareventures/iterable";
import {isNotNull} from "@softwareventures/nullable";
/** The type of a property key of `T`.
*
* If `T` is not specified, the type of a property key of any object.
*
* As of TypeScript 4.9, `Key` is equivalent to `string | number | symbol`. */
export type Key<T extends object = never> = keyof T;
/** The type of a property value of `T`. */
export type PropertyValue<T extends object> = T[keyof T];
/** The type of a key-value pair for properties of `T`. */
export type Entry<T extends object> = [keyof T, T[keyof T]];
/** The string-keyed properties of `T`. */
export type StringKeyedProperties<T> = {
[K in string & keyof T]: T[K];
};
/** The string property names of `T`. */
export type StringKey<T extends object> = Key<StringKeyedProperties<T>>;
/** The string-keyed property values of `T`. */
export type StringKeyedValue<T extends object> = PropertyValue<StringKeyedProperties<T>>;
/** Key-value pairs of the string-keyed properties of `T`. */
export type StringKeyedEntry<T extends object> = Entry<StringKeyedProperties<T>>;
/** The type `T`, but with any callable or newable signatures removed. */
export type NotFunction<T> = {[K in keyof T]: T[K]};
/** Creates a new object with the specified properties and a null prototype. */
export function object<T extends object>(properties: T): NotFunction<T> {
return Object.assign(emptyObject(), properties);
}
/** Creates a new empty object with a null prototype typed as a
* `Partial<T>`. */
export function emptyObject<T extends object>(): Partial<NotFunction<T>> {
return Object.create(null) as Partial<NotFunction<T>>;
}
/** Creates a new record with the specified properties and a null prototype. */
export function record<TKey extends Key, TValue>(
properties?: Record<TKey, TValue>
): Record<TKey, TValue> {
return Object.assign(emptyObject(), properties);
}
/** Creates a shallow copy of the specified object.
*
* The new object will have the same prototype as the specified object.
*
* If the object is callable (is a `Function`), then the new object will also
* be callable and will call the same function.
*
* If the object is newable (is a class), then the new object will also be
* newable and will construct a subclass with no overrides.
*
* To create a copy that has a null prototype and that is not callable or
* newable, use {@link object} instead. */
export function copy<T extends object>(object: T): T {
let copy: object;
if (typeof object === "function") {
copy = function (this: unknown, ...args: unknown[]): unknown {
return (object as (...args: unknown[]) => unknown).apply(this, args);
};
Object.setPrototypeOf(copy, Object.getPrototypeOf(object) as object | null);
} else {
copy = Object.create(Object.getPrototypeOf(object) as object | null) as object;
}
return Object.assign(copy, object);
}
/** Transforms a list of key-value pairs into an object.
*
* If the same key is specified twice or more, then later values overwrite
* earlier values. */
export function objectFromEntries<TKey extends Key, TValue>(
entries: Iterable<readonly [key: TKey, value: TValue]>
): Record<TKey, TValue> {
return Object.assign(emptyObject(), Object.fromEntries(entries)) as Record<TKey, TValue>;
}
/** Returns an array of the object's own ennumerable string-keyed property names. */
// @ts-ignore duplicate identifier: This is the exported declaration, the implementation is below.
export function keys<T extends object>(object: T): Array<StringKey<T>>;
/* @internal This implementation is for internal use only, the exported declaration is above */
// @ts-ignore duplicate identifier: This is the actual implementation, the exported declaration is above.
export const keys: <T extends object>(object: T) => Array<StringKey<T>> = Object.keys;
/** Returns an array of the object's own ennumerable string-keyed property
* values. */
// @ts-ignore duplicate identifier: This is the exported declaration, the implementation is below.
export function values<T extends object>(object: T): Array<StringKeyedValue<T>>;
/* @internal This implementation is for internal use only, the exported declaration is above. */
// @ts-ignore duplicate identifier: This is the actual implementation, the exported declaration is above.
export const values: <T extends object>(object: T) => Array<StringKeyedValue<T>> = Object.values;
/** Returns an array of the object's own ennumerable string-keyed property
* `[key, value]` pairs. */
// @ts-ignore duplicate identifier: This is the exported declaration, the implementation is below.
export function entries<T extends object>(object: T): Array<StringKeyedEntry<T>>;
/* @internal This implementation is for internal use only, the exported declaration is above. */
// @ts-ignore duplicate identifier: This is the actual implementation, the exported declaration is above.
export const entries: <T extends object>(object: T) => Array<StringKeyedEntry<T>> = Object.entries;
/** Tests if the specified record is empty of own ennumerable string-keyed
* properties.
*
* Properties keyed by symbols, inherited properties, and non-ennumerable
* properties are not considered, so an "empty" record may in fact have
* properties of those types. */
export function empty<T>(record: Readonly<Record<string, T>>): boolean {
return keys(record).length === 0;
}
export type Merge<A extends object, B extends object> = {
[K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never;
};
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<A extends object>(a: A): NotFunction<A>;
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<A extends object, B extends object>(a: A, b: B): Merge<A, B>;
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<A extends object, B extends object, C extends object>(
a: A,
b: B,
c: C
): Merge<Merge<A, B>, C>;
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<
A extends object,
B extends object,
C extends object,
D extends object
>(a: A, b: B, c: C, d: D): Merge<Merge<Merge<A, B>, C>, D>;
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<
A extends object,
B extends object,
C extends object,
D extends object,
E extends object
>(a: A, b: B, c: C, d: D, e: E): Merge<Merge<Merge<Merge<A, B>, C>, D>, E>;
/** Creates a new object that contains the properties of all the specified
* objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjects<K extends Key, T>(
...records: Array<Readonly<Record<K, T>>>
): Record<K, T>;
export function mergeObjects<K extends Key, T>(
...records: Array<Readonly<Record<K, T>>>
): Record<K, T> {
const result = record<K, T>();
for (let i = 0; i < records.length; ++i) {
Object.assign(result, records[i]);
}
return result;
}
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<A extends object>(): (a: A) => NotFunction<A>;
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<A extends object, B extends object>(b: B): (a: A) => Merge<A, B>;
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<A extends object, B extends object, C extends object>(
b: B,
c: C
): (a: A) => Merge<Merge<A, B>, C>;
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<
A extends object,
B extends object,
C extends object,
D extends object
>(b: B, c: C, d: D): (a: A) => Merge<Merge<Merge<A, B>, C>, D>;
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<
A extends object,
B extends object,
C extends object,
D extends object,
E extends object
>(b: B, c: C, d: D, e: E): (a: A) => Merge<Merge<Merge<Merge<A, B>, C>, D>, E>;
/** Curried variant of {@link mergeObjects}.
*
* Returns a function that creates an object that contains the properties of
* all the specified objects.
*
* If two or more of the specified objects have properties with the same key,
* then the newly created object will contain the property value from the last
* such specified object. */
export function mergeObjectsFn<K extends Key, T>(
...records: Array<Readonly<Record<K, T>>>
): (record: Readonly<Record<K, T>>) => Record<K, T>;
export function mergeObjectsFn<K extends Key, T>(
...records: Array<Readonly<Record<K, T>>>
): (record: Readonly<Record<K, T>>) => Record<K, T> {
return record => mergeObjects(record, ...records);
}
/** Creates a new object with properties mapped from the string-keyed
* properties of the specified object according to the specified mapping
* function.
*
* Only the string-keyed properties of the input object are considered, but
* the mapping function may produce keys of any suitable type.
*
* If the mapping function returns the same key twice, then later values will
* overwrite earlier ones. */
export function mapObject<TObject extends object, TNewKey extends Key, TNewValue>(
object: Readonly<TObject>,
f: (key: StringKey<TObject>, value: StringKeyedValue<TObject>) => readonly [TNewKey, TNewValue]
): Record<TNewKey, TNewValue> {
return objectFromEntries(map(entries(object), ([key, value]) => f(key, value)));
}
/** Curried variant of {@link mapObject}.
*
* Returns a function that creates a new object with properties mapped from the
* string-keyed properties of the specified object according to the specified
* mapping function.
*
* Only the string-keyed properties of the input object are considered, but
* the mapping function may produce keys of any suitable type.
*
* If the mapping function returns the same key twice, then later values will
* overwrite earlier ones. */
export function mapObjectFn<TObject extends object, TNewKey extends Key, TNewValue>(
f: (key: StringKey<TObject>, value: StringKeyedValue<TObject>) => readonly [TNewKey, TNewValue]
): (object: Readonly<TObject>) => Record<TNewKey, TNewValue> {
return object => mapObject(object, f);
}
/** Creates a new object with string-keyed properties of the specified object
* mapped to new keys according to the specified mapping function.
*
* Only the string-keyed properties of the input object are considered, but the
* mapping function may produce keys of any suitable type.
*
* If the mapping function returns the same key twice, then later values will
* overwrite earlier ones. */
export function mapObjectKeys<TObject extends object, TNewKey extends Key = string>(
object: Readonly<TObject>,
f: (key: StringKey<TObject>) => TNewKey
): Record<TNewKey, StringKeyedValue<TObject>> {
return objectFromEntries(map(entries(object), ([key, value]) => [f(key), value]));
}
/** Curried variant of {@link mapObjectKeys}.
*
* Returns a function that creates a new object with string-keyed properties of
* the specified object mapped to new keys according to the specified mapping
* function.
*
* Only the string-keyed properties of the input object are considered, but the
* mapping function may produce keys of any suitable type.
*
* If the mapping function returns the same key twice, then later values will
* overwrite earlier ones. */
export function mapObjectKeysFn<TObject extends object, TNewKey extends Key = string>(
f: (key: StringKey<TObject>) => TNewKey
): (object: Readonly<TObject>) => Record<TNewKey, StringKeyedValue<TObject>> {
return object => mapObjectKeys(object, f);
}
export type MapObjectValues<TObject extends object, TNewValue> = {
[TKey in StringKey<TObject>]: TNewValue;
};
/** Creates a new object with string-keyed properties of the specified object
* mapped to new values according to the specified mapping function. */
export function mapObjectValues<TObject extends object, TNewValue>(
object: TObject,
f: (value: StringKeyedValue<TObject>, key: StringKey<TObject>) => TNewValue
): MapObjectValues<TObject, TNewValue> {
return objectFromEntries(map(entries(object), ([key, value]) => [key, f(value, key)]));
}
/** Curried variant of {@link mapObjectValues}.
*
* Returns a function that creates a new object with string-keyed properties
* of the specified object mapped to new values according to the specified
* mapping function. */
export function mapObjectValuesFn<TObject extends object, TNewValue>(
f: (value: StringKeyedValue<TObject>, key: StringKey<TObject>) => TNewValue
): (object: TObject) => MapObjectValues<TObject, TNewValue> {
return object => mapObjectValues(object, f);
}
/** Creates a new object that contains the string-keyed properties of the
* specified object, filtered by the specified predicate. */
export function filterObject<T extends object>(
object: Readonly<T>,
predicate: (key: StringKey<T>, value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return objectFromEntries(
filter(entries(object), ([key, value]) => predicate(key, value))
) as Partial<StringKeyedProperties<T>>;
}
/** Curried variant of {@link filterObject}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectFn<T extends object>(
predicate: (key: StringKey<T>, value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => filterObject(object, predicate);
}
export type FilterObjectKeys<T extends object, U extends StringKey<T>> = {
[K in U]: T[K];
};
/** Creates a new object that contains the string-keyed properties of the
* specified object, filtered by the specified predicate. */
export function filterObjectKeys<T extends object, U extends StringKey<T>>(
object: Readonly<T>,
predicate: (key: StringKey<T>) => key is U
): FilterObjectKeys<T, U>;
/** Creates a new object that contains the string-keyed properties of the
* specified object, filtered by the specified predicate. */
export function filterObjectKeys<T extends object>(
object: Readonly<T>,
predicate: (key: StringKey<T>) => boolean
): Partial<StringKeyedProperties<T>>;
export function filterObjectKeys<T extends object>(
object: Readonly<T>,
predicate: (key: StringKey<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return objectFromEntries(filter(entries(object), ([key]) => predicate(key))) as Partial<
StringKeyedProperties<T>
>;
}
/** Curried variant of {@link filterObjectKeys}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectKeysFn<T extends object, U extends StringKey<T>>(
predicate: (key: StringKey<T>) => key is U
): (object: Readonly<T>) => FilterObjectKeys<T, U>;
/** Curried variant of {@link filterObjectKeys}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectKeysFn<T extends object>(
predicate: (key: StringKey<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>>;
/** Curried variant of {@link filterObjectKeys}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectKeysFn<T extends object>(
predicate: (key: StringKey<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => filterObjectKeys(object, predicate);
}
export type FilterObjectValues<T extends object, U extends StringKeyedValue<T>> = {
[K in StringKey<T>]: T[K] extends U ? T[K] : never;
};
export function filterObjectValues<T extends object, U extends StringKeyedValue<T>>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => value is U
): FilterObjectValues<T, U>;
/** Creates a new object that contains the string-keyed properties of the
* specified object, filtered by the specified predicate. */
export function filterObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>>;
export function filterObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return objectFromEntries(filter(entries(object), ([_, value]) => predicate(value))) as Partial<
StringKeyedProperties<T>
>;
}
/** Curried variant of {@link filterObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectValuesFn<T extends object, U extends StringKeyedValue<T>>(
predicate: (value: StringKeyedValue<T>) => value is U
): (object: Readonly<T>) => FilterObjectValues<T, U>;
/** Curried variant of {@link filterObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, filtered by the specified predicate. */
export function filterObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>>;
export function filterObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => filterObjectValues(object, predicate);
}
/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectEntries<T extends object>(
object: Readonly<T>,
predicate: (key: StringKey<T>, value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return filterObject(object, (key, value) => !predicate(key, value));
}
/** Curried variant of {@link excludeObjectEntries}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectEntriesFn<T extends object>(
predicate: (key: StringKey<T>, value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => excludeObjectEntries(object, predicate);
}
/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectKeys<T extends object>(
object: Readonly<T>,
predicate: (key: StringKey<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return filterObjectKeys(object, key => !predicate(key));
}
/** Curried variant of {@link excludeObjectKeys}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectKeysFn<T extends object>(
predicate: (key: StringKey<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => excludeObjectKeys(object, predicate);
}
export type ExcludeObjectValues<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
> = {
[K in StringKey<TObject>]: TObject[K] extends TExclude ? never : TObject[K];
};
/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectValues<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
>(
object: Readonly<TObject>,
predicate: (value: StringKeyedValue<TObject>) => value is TExclude
): ExcludeObjectValues<TObject, TExclude>;
/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>>;
export function excludeObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return filterObjectValues(object, value => !predicate(value));
}
/** Curried variant of {@link excludeObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectValuesFn<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
>(
predicate: (value: StringKeyedValue<TObject>) => value is TExclude
): (object: Readonly<TObject>) => ExcludeObjectValues<TObject, TExclude>;
/** Curried variant of {@link excludeObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>>;
export function excludeObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => excludeObjectValues(object, predicate);
}
export type ExcludeNullProperties<T extends object> = {
[K in StringKey<T>]?: NonNullable<T[K]>;
};
export function excludeNullProperties<T extends object>(
object: Readonly<T>
): ExcludeNullProperties<T> {
return filterObjectValues(object, isNotNull) as ExcludeNullProperties<T>;
}