Skip to content

Commit

Permalink
Changing compactArray function to flattenDeepArray
Browse files Browse the repository at this point in the history
  • Loading branch information
bitmaybewise committed Jun 3, 2023
1 parent 0403886 commit 1a5c906
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
10 changes: 5 additions & 5 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Expand Up @@ -1350,16 +1350,16 @@ local html = import 'html.libsonnet';
],
},
{
name: 'compactArrays',
params: ['arrs'],
name: 'flattenDeepArray',
params: ['value'],
availableSince: 'upcoming',
description: |||
Concatenate an array of arrays into a single flattened array, removing null values.
Concatenate an array containing values and arrays into a single flattened array.
|||,
examples: [
{
input: 'std.compactArrays([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])',
output: std.compactArrays([1, 2, 3, 4, 5, 6, 7, 8]),
input: 'std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])',
output: std.flattenDeepArray([1, 2, 3, 4, 5, 6, null, 7, 8]),
},
],
},
Expand Down
14 changes: 5 additions & 9 deletions stdlib/std.jsonnet
Expand Up @@ -865,15 +865,11 @@ limitations under the License.
flattenArrays(arrs)::
std.foldl(function(a, b) a + b, arrs, []),

compactArray(arrs)::
local foldable = function(accumulator, value)
if std.isArray(value) then
accumulator + std.compactArray(value)
else if value == null then
accumulator
else
accumulator + [value];
std.foldl(foldable, arrs, []),
flattenDeepArray(value)::
if std.isArray(value) then
[y for x in value for y in std.flattenDeepArray(x)]
else
[value],

manifestIni(ini)::
local body_lines(body) =
Expand Down
8 changes: 4 additions & 4 deletions test_suite/stdlib.jsonnet
Expand Up @@ -313,10 +313,10 @@ std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') &&

std.assertEqual(std.flattenArrays([[1, 2, 3], [4, 5, 6], []]), [1, 2, 3, 4, 5, 6]) &&

std.assertEqual(std.compactArray([]), []) &&
std.assertEqual(std.compactArray([1, 2, 3]), [1, 2, 3]) &&
std.assertEqual(std.compactArray([1, [2, 3]]), [1, 2, 3]) &&
std.assertEqual(std.compactArray([[1], [2, 3], []]), [1, 2, 3]) &&
std.assertEqual(std.flattenDeepArray([]), []) &&
std.assertEqual(std.flattenDeepArray([1, 2, 3]), [1, 2, 3]) &&
std.assertEqual(std.flattenDeepArray([1, [2, 3]]), [1, 2, 3]) &&
std.assertEqual(std.flattenDeepArray([[1], [2, 3], [[null]]]), [1, 2, 3, null]) &&

std.assertEqual(
std.manifestIni({
Expand Down

0 comments on commit 1a5c906

Please sign in to comment.