// 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 value that does not match the given predicate.
 *
 * @template T The type of the values in the input record.
 *
 * @param record The record to filter values from.
 * @param predicate The function to test each value for a condition.
 *
 * @returns A new record with all entries that have a value that matches the
 * given predicate.
 *
 * @example Basic usage
 * ```ts
 * import { filterValues } from "@std/collections/filter-values";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const people = {
 *   Arnold: 37,
 *   Sarah: 7,
 *   Kim: 23,
 * };
 * const adults = filterValues(people, (person) => person >= 18);
 *
 * assertEquals(
 *   adults,
 *   {
 *     Arnold: 37,
 *     Kim: 23,
 *   },
 * );
 * ```
 */
export function filterValues<T>(
  record: Readonly<Record<string, T>>,
  predicate: (value: T) => boolean,
): Record<string, T> {
  const result: Record<string, T> = {};
  const entries = Object.entries(record);

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

  return result;
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","x-jsr-backend":"modules","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"1285","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","etag":"\"cf17ca6e0a9e3fd7a6349103b8c0f6e2\"","set-cookie":"__cf_bm=oVyfJrkzuWubgOrsxkk3Dew3KWbg7EVKSARUv.4JHF8-1785241874.953399-1.0.1.1-45BLvdpf_PSAvyfOAjRPZ27X3_HHS3_cArC3PPRWlm42wuXDtUJIUBbXu6X4qFIuX4Q2ifNM.cUmQxZOWvlLnH2na_NCyl9DcaMWyZ9X5UbsqtU_avqwr0ekJgOG0WMl; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cache-control":"public, max-age=31536000, immutable","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=cztrArE6pyKAmkcpN8D3eNUI3tdZ2j8wxWVtsXQPmNU022VYyCkyxXUDTcvOqCFDRJC8d3O9CSBiDKSPb0fOiXEeKldEskU6URqxCQEVITPQBbmaVI4xC%2BM%3D\"}]}","cf-ray":"a223e6564ef87db6-LAX","vary":"Origin","cross-origin-resource-policy":"cross-origin","access-control-allow-origin":"*","x-content-type-options":"nosniff","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","server":"cloudflare","content-type":"text/typescript"},"url":"https://jsr.io/@std/collections/0.224.2/filter_values.ts","time":1785241875}