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

/**
 * Returns a new record with all entries of the given record except the ones that
 * have a key that does not match the given predicate.
 *
 * @template T The type of the values in the input record.
 *
 * @param record The record to filter keys from.
 * @param predicate The function to test each key for a condition.
 *
 * @returns A new record with all entries that have a key that matches the given
 * predicate.
 *
 * @example Basic usage
 * ```ts
 * import { filterKeys } from "@std/collections/filter-keys";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const menu = {
 *   Salad: 11,
 *   Soup: 8,
 *   Pasta: 13,
 * };
 *
 * const menuWithoutSalad = filterKeys(menu, (item) => item !== "Salad");
 *
 * assertEquals(
 *   menuWithoutSalad,
 *   {
 *     Soup: 8,
 *     Pasta: 13,
 *   },
 * );
 * ```
 */
export function filterKeys<T>(
  record: Readonly<Record<string, T>>,
  predicate: (key: string) => boolean,
): Record<string, T> {
  const result: Record<string, T> = {};

  for (const [key, value] of Object.entries(record)) {
    if (predicate(key)) {
      result[key] = value;
    }
  }

  return result;
}

// denoCacheMetadata={"headers":{"x-jsr-backend":"modules","x-robots-tag":"noindex","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","vary":"Origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e6564ef57db6-LAX","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","server":"cloudflare","access-control-allow-origin":"*","etag":"\"a3bdadeca9080ec8fcf8195ea7a28421\"","access-control-expose-headers":"*","content-type":"text/typescript","set-cookie":"__cf_bm=FmDhGtIXvbguCBe.0GIYVPu18D5uWG8Eg8a9VcG13jk-1785241874.9528651-1.0.1.1-JuNwRJbm0xUmbxHyowv1IKJorGFkLFhj1hvEaYrcnApDjUtk4rLDP.eaem3vHzeEivgjRcqY4gnOuc6EAMMJn_1WsljBH8noM8vMXtVlUlryozpsfXCL5dZGL6M1zhUw; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","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=lm9N3FcHBxWFezElvhvIGupJEkgNwZXQcbDwjpoLpqHX0i%2FLeDrZUbxbfvajgLKpWc1WxsDyjwhPV1%2FD0rhgdSngFW2EBDdpCCqwyjrory%2BTrQK%2Bz3fjHHQ%3D\"}]}","content-length":"1263","cache-control":"public, max-age=31536000, immutable"},"url":"https://jsr.io/@std/collections/0.224.2/filter_keys.ts","time":1785241875}