Added lume.concat(), updated README and tests

This commit is contained in:
rxi 2015-01-10 17:13:15 +00:00
parent ce86f4b0ea
commit 397dce4c5e
3 changed files with 27 additions and 0 deletions

View File

@ -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.

View File

@ -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

View File

@ -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 )