Added function lume.extend(), updated README and tests

This commit is contained in:
rxi 2015-05-05 19:21:56 +01:00
parent fce4a5e5df
commit 2e00c753d8
3 changed files with 32 additions and 0 deletions

View File

@ -98,6 +98,15 @@ local t = { 1, 2, 3 }
lume.clear(t) -- `t` becomes {}
```
### lume.extend(t, ...)
Copies all the fields from the source tables to the table `t` and returns `t`.
Tables are iterated in order, so the right-most table overrides existing
fields.
```
local t = { a = 1, b = 2 }
lume.extend(t, { b = 4, c = 6 }) -- `t` becomes { a = 1, b = 4, c = 6 }
```
### lume.shuffle(t)
Returns a shuffled copy of the array `t`.

View File

@ -183,6 +183,19 @@ function lume.clear(t)
end
function lume.extend(t, ...)
for i = 1, select("#", ...) do
local x = select(i, ...)
if x then
for k, v in pairs(x) do
t[k] = v
end
end
end
return t
end
function lume.shuffle(t)
local rtn = {}
for i = 1, #t do

View File

@ -145,6 +145,16 @@ tests["lume.clear"] = function()
testeq( lume.clear(t) == t, true )
end
-- lume.extend
tests["lume.extend"] = function()
local t = { a = 10, b = 20, c = 30 }
testeq( lume.extend(t) == t, true )
lume.extend(t, { d = 40 }, { e = 50 })
testeq( t, { a = 10, b = 20, c = 30, d = 40, e = 50 } )
lume.extend(t, { a = "cat", b = "dog" }, { b = "owl", c = "fox" })
testeq( t, { a = "cat", b = "owl", c = "fox", d = 40, e = 50 } )
end
-- lume.shuffle
tests["lume.shuffle"] = function()
local t = {1, 2, 3, 4, 5}