From 044141fefaa8e2f46a8f7f4812a9f72fd77f3c1d Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 9 May 2015 15:39:12 +0100 Subject: [PATCH] Added lume.pick() and tests --- lume.lua | 10 ++++++++++ test/test_lume.lua | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lume.lua b/lume.lua index 18074e4..4a37cf5 100644 --- a/lume.lua +++ b/lume.lua @@ -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) diff --git a/test/test_lume.lua b/test/test_lume.lua index bf9d58a..3f1d350 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -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({}), {} )