// 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 do not match the given predicate.
 *
 * @template T The type of the values in the input record.
 *
 * @param record The record to filter entries from.
 * @param predicate The function to test each entry for a condition.
 *
 * @returns A new record with all entries that match the given predicate.
 *
 * @example Basic usage
 * ```ts
 * import { filterEntries } from "@std/collections/filter-entries";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const menu = {
 *   Salad: 11,
 *   Soup: 8,
 *   Pasta: 13,
 * };
 *
 * const myOptions = filterEntries(
 *   menu,
 *   ([item, price]) => item !== "Pasta" && price < 10,
 * );
 *
 * assertEquals(myOptions, { Soup: 8 });
 * ```
 */
export function filterEntries<T>(
  record: Readonly<Record<string, T>>,
  predicate: (entry: [string, T]) => boolean,
): Record<string, T> {
  const result: Record<string, T> = {};
  const entries = Object.entries(record);

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

  return result;
}

// denoCacheMetadata={"headers":{"content-type":"text/typescript","x-robots-tag":"noindex","vary":"Origin","cross-origin-resource-policy":"cross-origin","server":"cloudflare","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=v830eb3aL2nQtILAve%2BPh3YD3gVtZoNqk%2Fsqc%2FCjvZXJeoDpIkpKi200I6zZTvnFg%2FXyKwvkuxpEWBXVRHwDgalWo0DMZ%2Fye2U6zopCAzVFbJv%2FFOo0mqEw%3D\"}]}","content-length":"1262","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","access-control-allow-origin":"*","cf-ray":"a223e6564ef37db6-LAX","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","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';","etag":"\"c78e83cb09d3f801383414bfd10692ec\"","set-cookie":"__cf_bm=gU1vjT3GLfuoTbT7ZPGkhBSTtwLvqD9c1SZ2W2uZvxo-1785241874.952414-1.0.1.1-jJAVSGbeiktaCR9uXHw0BQuktf2TLFkRxgLBojtDqnafbl8k0kS6KWaU7dIaMwEEQLdVIzOcsC5RmvFPZQNGOabXLW.sTr8OUexSVf9helmzttAC6uZ6fUjISCD4kOSG; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","x-jsr-backend":"modules","date":"Tue, 28 Jul 2026 12:31:15 GMT","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/collections/0.224.2/filter_entries.ts","time":1785241875}