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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
The `splice()` function performs in-place addition and removal of elements
on the given array.
If no offset, remove count and additional items are supplied, all elements
are removed from the array.
If just an offset, but no remove count and not additional items are given,
all elements beginning with the given offset until the end of the array
are removed.
If at least an offset and a remove count are given, then that amount of
items are removed from the array, beginning at the specified offset. Any
further supplied additional item (if any) is inserted in the same order
beginning at the given offset.
If either the offset or the remove count are negative, they're treated
as counting towards the end of the array. If either value exceeds the
array length, it is capped to the length of the array.
Returns the modified input array.
Returns `null` if the given input array value is not an array.
-- Testcase --
{%
let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
print(join("\n", [
// remove all items
splice([ ...arr ]),
// remove all items from index 4 till end
splice([ ...arr ], 4),
// remove item 2 and 3
splice([ ...arr ], 1, 2),
// remove last two items
splice([ ...arr ], -2),
// remove items 4 and 5
splice([ ...arr ], -3, -1),
// replace item 2
splice([ ...arr ], 1, 1, 7.9),
// add item between 3 and 4
splice([ ...arr ], 3, 0, 34),
// append three items
splice([ ...arr ], length(arr), 0, 123, 456, 789)
]), "\n");
%}
-- End --
-- Expect stdout --
[ ]
[ 6, 4.3, 1, 45 ]
[ 6, 45, 3.01, 2 ]
[ 6, 4.3, 1, 45 ]
[ 6, 4.3, 1, 2 ]
[ 6, 7.9, 1, 45, 3.01, 2 ]
[ 6, 4.3, 1, 34, 45, 3.01, 2 ]
[ 6, 4.3, 1, 45, 3.01, 2, 123, 456, 789 ]
-- End --
Supplying an invalid array will yield `null`.
-- Testcase --
{%
printf("%.J\n", splice("not_an_array", 0, 1));
%}
-- End --
-- Expect stdout --
null
-- End --
Invalid, non-numeric offset or index values are treated as 0.
-- Testcase --
{%
let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
print(join("\n", [
splice([ ...arr ], "foo", "bar"),
splice([ ...arr ], "foo", "bar", "baz")
]), "\n");
%}
-- End --
-- Expect stdout --
[ 6, 4.3, 1, 45, 3.01, 2 ]
[ "baz", 6, 4.3, 1, 45, 3.01, 2 ]
-- End --
|