Added lume.pick() and tests

This commit is contained in:
rxi 2015-05-09 15:39:12 +01:00
parent e0d55c8446
commit 044141fefa
2 changed files with 19 additions and 0 deletions

View File

@ -422,6 +422,16 @@ function lume.invert(t)
end
function lume.pick(t, ...)
local rtn = {}
for i = 1, select("#", ...) do
local k = select(i, ...)
rtn[k] = t[k]
end
return rtn
end
function lume.keys(t)
local rtn = {}
local iter = getiter(t)

View File

@ -373,6 +373,15 @@ tests["lume.invert"] = function()
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
end
-- lume.pick
tests["lume.pick"] = function()
local t = { cat = 10, dog = 20, fox = 30, owl = 40 }
testeq( lume.pick(t, "cat", "dog"), { cat = 10, dog = 20 } )
testeq( lume.pick(t, "fox", "owl"), { fox = 30, owl = 40 } )
testeq( lume.pick(t, "owl"), { owl = 40 } )
testeq( lume.pick(t), {} )
end
-- lume.keys
tests["lume.keys"] = function()
testeq( lume.keys({}), {} )