-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-at.ts
More file actions
30 lines (22 loc) · 718 Bytes
/
Copy pathsplit-at.ts
File metadata and controls
30 lines (22 loc) · 718 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';
interface SplitAt<T> {
splitAt(valueToFind: T): [T[], T[]];
}
export const splitAt: PrototypeStruct = {
label: 'splitAt',
fn: function arraySplitAt<T>(at: number): [T[], T[]] {
const ctx = this as unknown as T[];
const len = ctx.length;
if ('number' !== typeof(at) || at < 0 || at > len) {
throw new TypeError(`Expect 'at' to be in the range of 0 and length of array`);
}
if (!len) return [[], []];
const atInt = Number(at);
if (!atInt) return [[], ctx];
if (atInt === len) return [ctx, []];
return [ctx.slice(0, atInt), ctx.slice(atInt)];
},
};
declare global {
interface Array<T> extends SplitAt<T> {}
}