From de37bd6d65b7e3cff030a8e5c4e8e8d24812a528 Mon Sep 17 00:00:00 2001 From: rxi Date: Fri, 12 Dec 2014 20:37:07 +0000 Subject: [PATCH] Added lume.keys() and tests, updated docs --- README.md | 3 +++ lume.lua | 7 +++++++ test/test_lume.lua | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 2aec81a..15bdf76 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/lume.lua b/lume.lua index e36becf..d9cd133 100644 --- a/lume.lua +++ b/lume.lua @@ -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 diff --git a/test/test_lume.lua b/test/test_lume.lua index d2d0734..4331e03 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -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}