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}