-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.ts
More file actions
29 lines (22 loc) · 742 Bytes
/
Copy pathinsert.ts
File metadata and controls
29 lines (22 loc) · 742 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
import type { PrototypeStruct } from '../index.js';
interface Insert {
insert(index: number, s: string): string;
}
export const insert: PrototypeStruct = {
label: 'insert',
fn: function stringInsert(index: number, s: string): string {
const ctx = this as unknown as string;
const len = ctx.length;
if (index < 0 || index > len) throw new TypeError('String index out of bound');
if (!len) return s;
if (index === len) return `${ctx}${s}`;
if (!index) return `${s}${ctx}`;
const slice0 = ctx.slice(0, index);
const sliceRest = ctx.slice(index);
return `${slice0}${s}${sliceRest}`;
},
};
declare global {
// tslint:disable-next-line: no-empty-interface
interface String extends Insert {}
}