-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathassociate-array.ts
More file actions
28 lines (23 loc) · 789 Bytes
/
associate-array.ts
File metadata and controls
28 lines (23 loc) · 789 Bytes
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
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
export const associateArray = <T>(
instance: Iterable<T>,
predicate: (
value: T,
index: number,
instance: Record<string | number | symbol, T>,
) => string | number | symbol | undefined,
): Record<string | number | symbol, T> => {
const arrInstance = (instance.constructor === Array)
? <ReadonlyArray<T>> instance
: [...instance];
const result: Record<string | number | symbol, T> = {};
for (let i = 0, len = arrInstance.length; i < len; i++) {
const value = arrInstance[i]!;
const key = predicate(value, i, result);
if (key !== undefined) {
result[key] = value;
}
}
return result;
};
export { associateArray as default };