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

/**
 * Returns a tuple of two records with the first one containing all entries of
 * the given record that match the given predicate and the second one containing
 * all that do not.
 *
 * @template T The type of the values in the record.
 *
 * @param record The record to partition.
 * @param predicate The predicate function to determine which entries go where.
 *
 * @returns A tuple containing two records, the first one containing all entries
 * that match the predicate and the second one containing all that do not.
 *
 * @example Basic usage
 * ```ts
 * import { partitionEntries } from "@std/collections/partition-entries";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const menu = {
 *   Salad: 11,
 *   Soup: 8,
 *   Pasta: 13,
 * };
 * const myOptions = partitionEntries(
 *   menu,
 *   ([item, price]) => item !== "Pasta" && price < 10,
 * );
 *
 * assertEquals(
 *   myOptions,
 *   [
 *     { Soup: 8 },
 *     { Salad: 11, Pasta: 13 },
 *   ],
 * );
 * ```
 */
export function partitionEntries<T>(
  record: Readonly<Record<string, T>>,
  predicate: (entry: [string, T]) => boolean,
): [match: Record<string, T>, rest: Record<string, T>] {
  const match: Record<string, T> = {};
  const rest: Record<string, T> = {};
  const entries = Object.entries(record);

  for (const [key, value] of entries) {
    if (predicate([key, value])) {
      match[key] = value;
    } else {
      rest[key] = value;
    }
  }

  return [match, rest];
}

// denoCacheMetadata={"headers":{"etag":"\"27d0e7699432ce89e27f96489b440155\"","set-cookie":"__cf_bm=8Ar3YoLa_YGBB9ba18KLbaN.0eHtcrBjMs1rjvqG0vk-1785241874.9623163-1.0.1.1-qyOwNBrO3Dp0zK7oVlIr04lqFy9wA_pif72H89fZ7lcw7ZeRPgi77T89PPBA7p1DrHfr5Lk4Dsnc_DHAyezrqcsl5S.KUjycGDQvOOmHui8CcKuMldaMbrbE0bgHxvc1; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","access-control-expose-headers":"*","access-control-allow-origin":"*","x-robots-tag":"noindex","cache-control":"public, max-age=31536000, immutable","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"1590","x-jsr-backend":"modules","cf-ray":"a223e6564f2b7db6-LAX","content-type":"text/typescript","vary":"Origin","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=rIDkmwz1H4Bf5zvUbjtmX9SuxLGxQ4yufshYaustv1asEU8gvncLHbf%2BqnNmOIuHND8ys6XCoTdT%2BaerQO7R79UDJFrJNFHyEKzHNSNAH%2BqEy9LR9vsegDY%3D\"}]}","alt-svc":"h3=\":443\"; ma=86400","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';","x-content-type-options":"nosniff","server":"cloudflare","cross-origin-resource-policy":"cross-origin"},"url":"https://jsr.io/@std/collections/0.224.2/partition_entries.ts","time":1785241875}