feat: add utils function for array
This commit is contained in:
parent
8e8e29941f
commit
19428bef81
1 changed files with 10 additions and 6 deletions
|
|
@ -1,11 +1,15 @@
|
||||||
export function removeAt<T extends any[]>(arr: T, idx: number) {
|
const arr = { move, removeAt, insertAt };
|
||||||
|
|
||||||
|
export function move<T extends any>(arr: T[], idx: number, offset: number) {
|
||||||
|
arr.splice(idx + offset, 0, ...arr.splice(idx, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeAt<T extends any>(arr: T[], idx: number) {
|
||||||
return arr.splice(idx, 1);
|
return arr.splice(idx, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertAt<T extends any[]>(
|
export function insertAt<T extends any>(arr: T[], idx: number, item: T) {
|
||||||
arr: T,
|
|
||||||
idx: number,
|
|
||||||
item: T[number],
|
|
||||||
) {
|
|
||||||
return arr.splice(idx, 0, item);
|
return arr.splice(idx, 0, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default arr;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue