-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.ts
39 lines (36 loc) · 978 Bytes
/
object.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
export function* objectKeys<T extends object>(
object: T
): Generator<Extract<keyof T, string>, void, unknown> {
for (const key in object) {
if (object.hasOwnProperty(key)) {
yield key;
}
}
}
export function* objectValues<T extends object>(
object: T
): Generator<Extract<T[Extract<keyof T, string>], string>, void, unknown> {
for (const key in object) {
if (object.hasOwnProperty(key)) {
yield (object as any)[key];
}
}
}
export function* objectEntries<T extends object>(
object: T
): Generator<[Extract<keyof T, string>, T[Extract<keyof T, string>]], void, unknown> {
for (const key in object) {
if (object.hasOwnProperty(key)) {
yield [key, (object as any)[key]];
}
}
}
export function fromEntries<T = any>(
entries: Iterable<readonly [PropertyKey, T]>
): { [k: string]: T } {
const object: Record<PropertyKey, T> = {};
for (const [key, value] of entries) {
object[key] = value;
}
return object;
}