-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzipObj.ts
32 lines (25 loc) · 906 Bytes
/
zipObj.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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { Obj, PH } from './utils/types.ts';
import curryN from './utils/curry_n.ts';
// @types
type ZipObj_2 = <T>(values: T[]) => Obj<T>;
type ZipObj_1<T> = (keys: string[]) => Obj<T>;
type ZipObj =
& ((keys: string[]) => ZipObj_2)
& (<T>(keys: PH, values: T[]) => ZipObj_1<T>)
& (<T>(keys: string[], values: T[]) => Obj<T>);
function _zipObj<T>(keys: string[], values: T[]): Obj<T> {
const result: Obj<T> = {};
const len = Math.min(keys.length, values.length);
for (let i = 0; i < len; i++) {
result[keys[i]] = values[i];
}
return result;
}
/**
* Returns a new object out of given list of `keys` and `values`.
* The returned is truncated to the length of the shorter of the two.
*
* Fae.zipObj(['a', 'b', 'c'], [1, 2, 3]) // {a: 1, b: 2, c: 3}
*/
export const zipObj = curryN(2, _zipObj) as ZipObj;