Skip to content

Commit 49909f1

Browse files
committed
updates the flat function, adding support to depth
1 parent 888240c commit 49909f1

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/array/init.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,19 @@ array = {
426426
return output
427427
end,
428428

429-
-- Create a new table with the sub-table elements concatenated into it
430-
-- @param obj {table}
429+
-- Create a new table with the sub-table elements concatenated into it up to the specific depth
430+
-- @param obj {table, depth}
431431
-- @return {table}
432-
flat = function(obj)
432+
flat = function(obj, depth)
433+
if depth == nil then
434+
depth = 1 / 0 -- Infinity
435+
end
436+
433437
return array.reduce(obj, function(acc, item)
434-
if array.is_array(item) then
438+
if array.is_array(item) and depth > 0 then
435439
return array.concat(
436440
acc,
437-
array.flat(item)
441+
array.flat(item, depth - 1)
438442
)
439443
else
440444
table.insert(acc, item)

0 commit comments

Comments
 (0)