-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.ts
27 lines (25 loc) · 876 Bytes
/
objects.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
import hasOwnProperty from 'immuton/hasOwnProperty';
import type { Key } from 'immuton/types';
/**
* Iterates through each own enumerable property of the given
* object and calls the given callback for each of them.
* @param obj Object to iterate
* @param iterator Function to be called for each key
*/
export function forEachKey<T>(obj: T, iterator: (key: Key<T>, value: T[Key<T>]) => void): void {
// eslint-disable-next-line no-restricted-syntax
for (const key in obj) {
if (hasOwnProperty(obj, key) && typeof key === 'string') {
iterator(key, obj[key]);
}
}
}
/**
* Returns the string keys of the given object as an array.
* @param obj Object whose keys are returned
*/
export function keys<T>(obj: T): Key<T>[] {
const keyArray: Key<T>[] = [];
forEachKey(obj, (key) => keyArray.push(key));
return keyArray;
}