-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretain.ts
More file actions
30 lines (22 loc) · 715 Bytes
/
Copy pathretain.ts
File metadata and controls
30 lines (22 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { PrototypeStruct } from '../index.js';
type RetainPredicate<T> = (element: T, index: number) => void;
interface Retain<T> {
retain(predicate: RetainPredicate<T>): void;
}
export const retain: PrototypeStruct = {
label: 'retain',
fn: function arrayRetain<T = unknown>(predicate: RetainPredicate<T>): void {
const ctx = this as unknown as T[];
if ('function' !== typeof(predicate)) {
throw new TypeError(`Expect 'predicate' to be a function`);
}
const len = ctx.length;
if (!len) return;
const retained = ctx.filter(predicate);
ctx.length = 0;
for (const n of retained) ctx.push(n);
},
};
declare global {
interface Array<T> extends Retain<T> {}
}