diff --git a/README.md b/README.md index 6dbfe46..2a26b53 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ and `t2` have a conflicting key, the value from `t2` is used. lume.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5} ``` +### lume.concat(...) +Returns a new array consisting of all the given arrays concatenated into one. +```lua +lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6} +``` + ### lume.find(t, value) Returns the index/key of `value` in `t`. Returns `nil` if that value does not exist in the table. diff --git a/lume.lua b/lume.lua index 0d38aad..7a0877a 100644 --- a/lume.lua +++ b/lume.lua @@ -260,6 +260,19 @@ function lume.merge(t, t2, retainkeys) end +function lume.concat(...) + local rtn = {} + for i = 1, select("#", ...) do + local t = select(i, ...) + local iter = getiter(t) + for k, v in iter(t) do + rtn[#rtn + 1] = v + end + end + return rtn +end + + function lume.find(t, value) local iter = getiter(t) for k, v in iter(t) do diff --git a/test/test_lume.lua b/test/test_lume.lua index 8f07ffb..c44ab0e 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -220,6 +220,14 @@ tests["lume.merge"] = function() testeq( lume.merge({a=1, b=2}, {b=3, c=4}, true), {a=1, b=3, c=4} ) end +-- lume.concat +tests["lume.concat"] = function() + testeq( lume.concat(nil), {} ) + testeq( lume.concat({1, 2, 3}), {1, 2, 3} ) + testeq( lume.concat({1, 2, 3}, {4, 5, 6}), {1, 2, 3, 4, 5, 6} ) + testeq( lume.concat({1, 2, 3}, {4, 5, 6}, nil, {7}), {1, 2, 3, 4, 5, 6, 7} ) +end + -- lume.find tests["lume.find"] = function() testeq( lume.find({"a", "b", "c"}, "b"), 2 )