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

/**
 * Returns all elements in the given collection until the first element that
 * does not match the given predicate.
 *
 * @template T The type of the array elements.
 *
 * @param array The array to take elements from.
 * @param predicate The predicate function to determine if an element should be
 * included.
 *
 * @returns A new array containing all elements until the first element that
 * does not match the predicate.
 *
 * @example Basic usage
 * ```ts
 * import { takeWhile } from "@std/collections/take-while";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const numbers = [1, 2, 3, 4, 5, 6];
 *
 * const result = takeWhile(numbers, (number) => number < 4);
 *
 * assertEquals(result, [1, 2, 3]);
 * ```
 */
export function takeWhile<T>(
  array: readonly T[],
  predicate: (el: T) => boolean,
): T[] {
  let offset = 0;
  const length = array.length;

  while (length > offset && predicate(array[offset] as T)) {
    offset++;
  }

  return array.slice(0, offset);
}

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-expose-headers":"*","x-robots-tag":"noindex","content-length":"1116","etag":"\"36287b2f79fbf227e61b729d0e0bed46\"","server":"cloudflare","access-control-allow-origin":"*","vary":"Origin","content-type":"text/typescript","x-content-type-options":"nosniff","cf-ray":"a223e6564f427db6-LAX","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=IvigbMP1dQcKpkUjsaw3G1eBOq4COakYomtn0FPVN9om1KrGERFpaaFASl4HVowx%2BudZf3hx3LHpFJ5nuYwPiuS0R%2FxzGUJvG6t2HS0ViyzT7DCpz8RwDbo%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';","set-cookie":"__cf_bm=gsuyUtafHq0kkqDRRScFA7ZtXnWPV7_hlCrC5NHZA00-1785241874.9664283-1.0.1.1-HZGVE.jFh_0qzDUqzy8Jdm_GfkweIPck4hDyW.cG0ZRFtUfeGP2IJUrjKX36ErmzYwNRasOt0blwoT6GYrEWG9Y.3Nm2_OFAl3700_sfZ6nwQfbZ1bSg9hVxSLzz1U4J; 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}","cache-control":"public, max-age=31536000, immutable","cross-origin-resource-policy":"cross-origin","x-jsr-backend":"modules"},"url":"https://jsr.io/@std/collections/0.224.2/take_while.ts","time":1785241875}