Added lume.keys() and tests, updated docs

This commit is contained in:
rxi 2014-12-12 20:37:07 +00:00
parent ac920c8f5e
commit de37bd6d65
3 changed files with 20 additions and 0 deletions

View File

@ -185,6 +185,9 @@ values the keys.
lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"}
```
### lume.keys(t)
Returns an array containing each key of the table.
### lume.clone(t)
Returns a shallow copy of the table `t`.

View File

@ -264,6 +264,13 @@ function lume.invert(t)
end
function lume.keys(t)
local rtn = {}
for k, v in pairs(t) do rtn[#rtn + 1] = k end
return rtn
end
function lume.clone(t)
local rtn = {}
for k, v in pairs(t) do rtn[k] = v end

View File

@ -274,6 +274,16 @@ tests["lume.invert"] = function()
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
end
-- lume.keys
tests["lume.keys"] = function()
testeq( lume.keys({}), {} )
local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 })
table.sort(t)
testeq( t, {"aaa", "bbb", "ccc"} )
local t = lume.keys({ "x", "x", "x" })
testeq( t, {1, 2, 3} )
end
-- lume.clone
tests["lume.clone"] = function()
local t = {6, 7, 4, 5}