mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
d102a10235
* refactor: import from `@std/assert` * update
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Returns a new array that drops all elements in the given collection until the
|
|
* first element that does not match the given predicate.
|
|
*
|
|
* @typeParam T The type of the elements in the input array.
|
|
*
|
|
* @param array The array to drop elements from.
|
|
* @param predicate The function to test each element for a condition.
|
|
*
|
|
* @returns A new array that drops all elements until the first element that
|
|
* does not match the given predicate.
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { dropWhile } from "@std/collections/drop-while";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* const numbers = [3, 2, 5, 2, 5];
|
|
* const dropWhileNumbers = dropWhile(numbers, (number) => number !== 2);
|
|
*
|
|
* assertEquals(dropWhileNumbers, [2, 5, 2, 5]);
|
|
* ```
|
|
*/
|
|
export function dropWhile<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(offset, length);
|
|
}
|