diff --git a/README.md b/README.md index fdc4ce1..f04cbc9 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,13 @@ an error is raised, lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6 ``` +### lume.invert(t) +Returns a copy of the table where the keys have become the values and the +values the keys. +```lua +lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"} +``` + ### lume.set(t [, retainkeys]) Returns a copy of the `t` table with all the duplicate values removed. If `retainkeys` is true the table is not treated as an array and retains its diff --git a/lume.lua b/lume.lua index 4ef2d11..e4f39fb 100644 --- a/lume.lua +++ b/lume.lua @@ -126,6 +126,13 @@ function lume.reduce(t, fn, first) end +function lume.invert(t) + local rtn = {} + for k, v in pairs(t) do rtn[v] = k end + return rtn +end + + function lume.set(t, retainkeys) local tmp = {} for k, v in pairs(t) do tmp[v] = k end