Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,26 @@ local html = import 'html.libsonnet';
|||,
]),
},
{
name: 'remove',
params: ['arr', 'elem'],
availableSince: 'upcoming',
description: html.paragraphs([
|||
Remove first occurrence of <code>elem</code> from <code>arr</code>.
|||,
]),
},
{
name: 'removeAt',
params: ['arr', 'idx'],
availableSince: 'upcoming',
description: html.paragraphs([
|||
Remove element at <code>idx</code> index from <code>arr</code>.
|||,
]),
},
],
},
{
Expand Down
15 changes: 15 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,21 @@ limitations under the License.

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

removeAt(arr, at):: [
arr[i],
for i in std.range(0, std.length(arr) - 1)
Copy link
Copy Markdown
Contributor

@sparkprime sparkprime May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Never mind, this is fine

if i != at
],

remove(arr, elem)::
local indexes = std.find(elem, arr);
if std.length(indexes) == 0
then
arr
else
std.removeAt(arr, indexes[0])
,

objectRemoveKey(obj, key):: {
[k]: obj[k],
for k in std.objectFields(obj)
Expand Down
3 changes: 3 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,9 @@ std.assertEqual(std.isEmpty('non-empty string'), false) &&
std.assertEqual(std.contains([1, 2, 3], 2), true) &&
std.assertEqual(std.contains([1, 2, 3], "foo"), false) &&

std.assertEqual(std.remove([1, 2, 3], 2), [1, 3]) &&
std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&

std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&

true