mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
89ca4ad93b
inline utility function
27 lines
573 B
TypeScript
27 lines
573 B
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Filters the given array, removing all elements that do not match the given predicate
|
|
* **in place. This means `array` will be modified!**.
|
|
*/
|
|
export function filterInPlace<T>(
|
|
array: Array<T>,
|
|
predicate: (el: T) => boolean,
|
|
): Array<T> {
|
|
let outputIndex = 0;
|
|
|
|
for (const cur of array) {
|
|
if (!predicate(cur)) {
|
|
continue;
|
|
}
|
|
|
|
array[outputIndex] = cur;
|
|
outputIndex += 1;
|
|
}
|
|
|
|
array.splice(outputIndex);
|
|
|
|
return array;
|
|
}
|