• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/17_splice

  1 The `splice()` function performs in-place addition and removal of elements
  2 on the given array.
  3 
  4 If no offset, remove count and additional items are supplied, all elements
  5 are removed from the array.
  6 
  7 If just an offset, but no remove count and not additional items are given,
  8 all elements beginning with the given offset until the end of the array
  9 are removed.
 10 
 11 If at least an offset and a remove count are given, then that amount of
 12 items are removed from the array, beginning at the specified offset. Any
 13 further supplied additional item (if any) is inserted in the same order
 14 beginning at the given offset.
 15 
 16 If either the offset or the remove count are negative, they're treated
 17 as counting towards the end of the array. If either value exceeds the
 18 array length, it is capped to the length of the array.
 19 
 20 Returns the modified input array.
 21 
 22 Returns `null` if the given input array value is not an array.
 23 
 24 
 25 -- Testcase --
 26 {%
 27         let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
 28 
 29         print(join("\n", [
 30                 // remove all items
 31                 splice([ ...arr ]),
 32 
 33                 // remove all items from index 4 till end
 34                 splice([ ...arr ], 4),
 35 
 36                 // remove item 2 and 3
 37                 splice([ ...arr ], 1, 2),
 38 
 39                 // remove last two items
 40                 splice([ ...arr ], -2),
 41 
 42                 // remove items 4 and 5
 43                 splice([ ...arr ], -3, -1),
 44 
 45                 // replace item 2
 46                 splice([ ...arr ], 1, 1, 7.9),
 47 
 48                 // add item between 3 and 4
 49                 splice([ ...arr ], 3, 0, 34),
 50 
 51                 // append three items
 52                 splice([ ...arr ], length(arr), 0, 123, 456, 789)
 53         ]), "\n");
 54 %}
 55 -- End --
 56 
 57 -- Expect stdout --
 58 [ ]
 59 [ 6, 4.3, 1, 45 ]
 60 [ 6, 45, 3.01, 2 ]
 61 [ 6, 4.3, 1, 45 ]
 62 [ 6, 4.3, 1, 2 ]
 63 [ 6, 7.9, 1, 45, 3.01, 2 ]
 64 [ 6, 4.3, 1, 34, 45, 3.01, 2 ]
 65 [ 6, 4.3, 1, 45, 3.01, 2, 123, 456, 789 ]
 66 -- End --
 67 
 68 
 69 Supplying an invalid array will yield `null`.
 70 
 71 -- Testcase --
 72 {%
 73         printf("%.J\n", splice("not_an_array", 0, 1));
 74 %}
 75 -- End --
 76 
 77 -- Expect stdout --
 78 null
 79 -- End --
 80 
 81 
 82 Invalid, non-numeric offset or index values are treated as 0.
 83 
 84 -- Testcase --
 85 {%
 86         let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
 87 
 88         print(join("\n", [
 89                 splice([ ...arr ], "foo", "bar"),
 90                 splice([ ...arr ], "foo", "bar", "baz")
 91         ]), "\n");
 92 %}
 93 -- End --
 94 
 95 -- Expect stdout --
 96 [ 6, 4.3, 1, 45, 3.01, 2 ]
 97 [ "baz", 6, 4.3, 1, 45, 3.01, 2 ]
 98 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt