-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrom.ts
33 lines (28 loc) · 978 Bytes
/
from.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
import type { PrototypeStruct } from '../index.js';
type MapFn<K, V> = (n: [K, V]) => [K, V];
interface WeakMapFrom {
from<K extends object, V>(mapEntries: [K, V][], mapFn?: MapFn<K, V>): WeakMap<K, V>;
}
export const from: PrototypeStruct = {
isStatic: true,
label: 'from',
fn: function weakMapFrom<
K extends object,
V
>(mapEntries: [K, V][], mapFn?: MapFn<K, V>): WeakMap<K, V> {
if (!Array.isArray(mapEntries)) {
// tslint:disable-next-line: max-line-length
throw new TypeError(`Invalid WeakMap entries. Each WeakMap entry must contain [key, value] where key must be an object`);
}
try {
const entries = 'function' === typeof(mapFn) ? mapEntries.map(mapFn) : mapEntries;
return new WeakMap(entries);
} catch (_) {
throw new TypeError(`WeakMap key must be an object`);
}
},
};
declare global {
// tslint:disable-next-line: no-empty-interface
interface WeakMapConstructor extends WeakMapFrom {}
}