-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
23 lines (22 loc) · 873 Bytes
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import propertyless from './propertyless.js';
/**
* Creates an object by mapping each item in the given array to pairs of keys and values.
* The given iterator function is called for each item in the array and it should return
* the key-value pair as a two-item array. If it returns undefined, then the item will
* be omitted from the result object.
*/
export default function build<T, V, K extends string>(
source: T[],
iterator: (item: T, index: number) => [K, V] | void | undefined | null,
): { [P in K]: V } {
let result: { [P in K]: V } | undefined;
source.forEach((item, index) => {
const pair = iterator(item, index);
if (pair != null) {
result = result || ({} as { [P in K]: V });
// eslint-disable-next-line prefer-destructuring
result[pair[0]] = pair[1];
}
});
return result || (propertyless as { [P in K]: V });
}