// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * Builds two separate arrays from the given array of 2-tuples, with the first
 * returned array holding all first tuple elements and the second one holding
 * all the second elements.
 *
 * @template T The type of the first tuple elements.
 * @template U The type of the second tuple elements.
 *
 * @param pairs The array of 2-tuples to unzip.
 *
 * @returns A tuple containing two arrays, the first one holding all first tuple
 * elements and the second one holding all second elements.
 *
 * @example Basic usage
 * ```ts
 * import { unzip } from "@std/collections/unzip";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const parents = [
 *   ["Maria", "Jeff"],
 *   ["Anna", "Kim"],
 *   ["John", "Leroy"],
 * ] as [string, string][];
 *
 * const [moms, dads] = unzip(parents);
 *
 * assertEquals(moms, ["Maria", "Anna", "John"]);
 * assertEquals(dads, ["Jeff", "Kim", "Leroy"]);
 * ```
 */
export function unzip<T, U>(pairs: readonly [T, U][]): [T[], U[]] {
  const { length } = pairs;
  const result: [T[], U[]] = [
    Array<T>(length),
    Array<U>(length),
  ];

  pairs.forEach(([first, second], index) => {
    result[0][index] = first;
    result[1][index] = second;
  });

  return result;
}

// denoCacheMetadata={"headers":{"nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","server":"cloudflare","x-jsr-backend":"modules","content-type":"text/typescript","content-length":"1344","x-robots-tag":"noindex","set-cookie":"__cf_bm=ssIZmBLdnajPsjTE6faf1cajpj3ArO2nNAo01_sSZw8-1785241874.9674184-1.0.1.1-BfirBdGh7ygWJ_b.9ele9kzJp3f7vcx_ykZ2L2mdrOC0He2bMDfN5g3VwMDysiUNO5x0G3JzEzQwgZZBtODGvsGZj8csPCXx9OypivEgpP5DlX.YzMNOq9fwesC2cngC; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cf-ray":"a223e6564f497db6-LAX","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"519e8c7c0ce1951eb9bb222f46d7b942\"","cache-control":"public, max-age=31536000, immutable","cross-origin-resource-policy":"cross-origin","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=5yITOgZL%2ByLh3H%2B1zDdPYNBbBypJ%2F7FVdS7AM3oC9s7XIMFVxEbAgMPVuLIL55ReNWNdkTfhmpqCKqea0JPL%2FsOUtocELxlfcu8FtaVsOrDz3xTYqrXaVZs%3D\"}]}","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","x-content-type-options":"nosniff","vary":"Origin","access-control-allow-origin":"*"},"url":"https://jsr.io/@std/collections/0.224.2/unzip.ts","time":1785241875}