Skip to content

Commit fe8179a

Browse files
authored
feat: std.flattenDeepArray Concatenate an array containing values and arrays into a single flattened array. (#1082)
* std.flattenDeepArray recursively flattens multiple arrays
1 parent 8f1e262 commit fe8179a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,20 @@ local html = import 'html.libsonnet';
12781278
},
12791279
],
12801280
},
1281+
{
1282+
name: 'flattenDeepArray',
1283+
params: ['value'],
1284+
availableSince: 'upcoming',
1285+
description: |||
1286+
Concatenate an array containing values and arrays into a single flattened array.
1287+
|||,
1288+
examples: [
1289+
{
1290+
input: 'std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])',
1291+
output: std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]]),
1292+
},
1293+
],
1294+
},
12811295
{
12821296
name: 'reverse',
12831297
params: ['arrs'],

stdlib/std.jsonnet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ limitations under the License.
865865
flattenArrays(arrs)::
866866
std.foldl(function(a, b) a + b, arrs, []),
867867

868+
flattenDeepArray(value)::
869+
if std.isArray(value) then
870+
[y for x in value for y in std.flattenDeepArray(x)]
871+
else
872+
[value],
873+
868874
manifestIni(ini)::
869875
local body_lines(body) =
870876
std.join([], [

test_suite/stdlib.jsonnet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') &&
313313

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

316+
std.assertEqual(std.flattenDeepArray([]), []) &&
317+
std.assertEqual(std.flattenDeepArray([1, 2, 3]), [1, 2, 3]) &&
318+
std.assertEqual(std.flattenDeepArray([1, [2, 3]]), [1, 2, 3]) &&
319+
std.assertEqual(std.flattenDeepArray([[1], [2, 3], [[null]]]), [1, 2, 3, null]) &&
320+
316321
std.assertEqual(
317322
std.manifestIni({
318323
main: { a: '1', b: '2' },

0 commit comments

Comments
 (0)