/
zip.ts
44 lines (38 loc) · 1.03 KB
/
zip.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
40
41
42
43
44
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* Builds N-tuples of elements from the given N arrays with matching indices,
* stopping when the smallest array's end is reached.
*
* @example
* ```ts
* import { zip } from "https://deno.land/std@$STD_VERSION/collections/zip.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* const numbers = [1, 2, 3, 4];
* const letters = ["a", "b", "c", "d"];
* const pairs = zip(numbers, letters);
*
* assertEquals(
* pairs,
* [
* [1, "a"],
* [2, "b"],
* [3, "c"],
* [4, "d"],
* ],
* );
* ```
*/
import { minOf } from "./min_of.ts";
export function zip<T extends unknown[]>(
...arrays: { [K in keyof T]: T[K][] }
): T[] {
const minLength = minOf(arrays, (it) => it.length) ?? 0;
const ret: T[] = new Array(minLength);
for (let i = 0; i < minLength; i += 1) {
const arr = arrays.map((it) => it[i]);
ret[i] = arr as T;
}
return ret;
}