Skip to content

Commit 9f9cf3a

Browse files
feat: implement std.remove and std.removeAt (#1071)
Co-authored-by: Dave Cunningham <[email protected]>
1 parent 72a74d8 commit 9f9cf3a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,26 @@ local html = import 'html.libsonnet';
13521352
|||,
13531353
]),
13541354
},
1355+
{
1356+
name: 'remove',
1357+
params: ['arr', 'elem'],
1358+
availableSince: 'upcoming',
1359+
description: html.paragraphs([
1360+
|||
1361+
Remove first occurrence of <code>elem</code> from <code>arr</code>.
1362+
|||,
1363+
]),
1364+
},
1365+
{
1366+
name: 'removeAt',
1367+
params: ['arr', 'idx'],
1368+
availableSince: 'upcoming',
1369+
description: html.paragraphs([
1370+
|||
1371+
Remove element at <code>idx</code> index from <code>arr</code>.
1372+
|||,
1373+
]),
1374+
},
13551375
],
13561376
},
13571377
{

stdlib/std.jsonnet

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,21 @@ limitations under the License.
17251725

17261726
contains(arr, elem):: std.any([e == elem for e in arr]),
17271727

1728+
removeAt(arr, at):: [
1729+
arr[i],
1730+
for i in std.range(0, std.length(arr) - 1)
1731+
if i != at
1732+
],
1733+
1734+
remove(arr, elem)::
1735+
local indexes = std.find(elem, arr);
1736+
if std.length(indexes) == 0
1737+
then
1738+
arr
1739+
else
1740+
std.removeAt(arr, indexes[0])
1741+
,
1742+
17281743
objectRemoveKey(obj, key):: {
17291744
[k]: obj[k],
17301745
for k in std.objectFields(obj)

test_suite/stdlib.jsonnet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,9 @@ std.assertEqual(std.isEmpty('non-empty string'), false) &&
15621562
std.assertEqual(std.contains([1, 2, 3], 2), true) &&
15631563
std.assertEqual(std.contains([1, 2, 3], "foo"), false) &&
15641564

1565+
std.assertEqual(std.remove([1, 2, 3], 2), [1, 3]) &&
1566+
std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&
1567+
15651568
std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&
15661569

15671570
true

0 commit comments

Comments
 (0)