deno.land / std@0.224.0 / collections / zip.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { minOf } from "./min_of.ts";
/** * Builds N-tuples of elements from the given N arrays with matching indices, * stopping when the smallest array's end is reached. * * @template T the type of the tuples produced by this function. * @example * ```ts * import { zip } from "https://deno.land/std@$STD_VERSION/collections/zip.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.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"], * ], * ); * ``` */export function zip<T extends unknown[]>( ...arrays: { [K in keyof T]: ReadonlyArray<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;}
Version Info