-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruncate.ts
More file actions
32 lines (25 loc) · 784 Bytes
/
Copy pathtruncate.ts
File metadata and controls
32 lines (25 loc) · 784 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
31
32
import type { PrototypeStruct } from '../index.js';
interface Truncate {
truncate(len: number): void;
}
export const truncate: PrototypeStruct = {
label: 'truncate',
fn: function arrayTruncate<T>(len: number): void {
const ctx = this as unknown as T[];
const length = ctx.length;
if ('number' !== typeof(len) || len < 0 || len > length) {
throw new TypeError(`Expect 'len' to be in the range of 0 and length of array`);
}
const lenInt = Number(len);
if (!lenInt) ctx.length = 0;
else if (lenInt < length) {
const slice = ctx.slice(0, lenInt);
ctx.length = 0;
for (const n of slice) ctx.push(n);
}
},
};
declare global {
// tslint:disable-next-line: no-empty-interface
interface Array<T> extends Truncate {}
}