/
splice.ts
58 lines (54 loc) · 1.76 KB
/
splice.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { purry } from "./purry";
/**
* Removes elements from an array and, inserts new elements in their place.
*
* @param items - The array to splice.
* @param start - The index from which to start removing elements.
* @param deleteCount - The number of elements to remove.
* @param replacement - The elements to insert into the array in place of the deleted elements.
* @signature
* R.splice(items, start, deleteCount, replacement)
* @example
* R.splice([1,2,3,4,5,6,7,8], 2, 3, []); //=> [1,2,6,7,8]
* R.splice([1,2,3,4,5,6,7,8], 2, 3, [9, 10]); //=> [1,2,9,10,6,7,8]
* @dataFirst
* @category Array
*/
export function splice<T>(
items: ReadonlyArray<T>,
start: number,
deleteCount: number,
replacement: ReadonlyArray<T>,
): Array<T>;
/**
* Removes elements from an array and, inserts new elements in their place.
*
* @param start - The index from which to start removing elements.
* @param deleteCount - The number of elements to remove.
* @param replacement - The elements to insert into the array in place of the deleted elements.
* @signature
* R.splice(start, deleteCount, replacement)(items)
* @example
* R.pipe([1,2,3,4,5,6,7,8], R.splice(2, 3, [])) // => [1,2,6,7,8]
* R.pipe([1,2,3,4,5,6,7,8], R.splice(2, 3, [9, 10])) // => [1,2,9,10,6,7,8]
* @dataLast
* @category Array
*/
export function splice<T>(
start: number,
deleteCount: number,
replacement: ReadonlyArray<T>,
): (items: ReadonlyArray<T>) => Array<T>;
export function splice(): unknown {
return purry(_splice, arguments);
}
function _splice<T>(
items: ReadonlyArray<T>,
start: number,
deleteCount: number,
replacement: ReadonlyArray<T>,
): Array<T> {
const result = items.slice();
result.splice(start, deleteCount, ...replacement);
return result;
}