diff --git a/lume.lua b/lume.lua index e1b25c3..47b8df9 100644 --- a/lume.lua +++ b/lume.lua @@ -158,6 +158,23 @@ function lume.push(t, ...) end +function lume.remove(t, x) + local iter = getiter(t) + for i, v in iter(t) do + if v == x then + if isarray(t) then + table.remove(t, i) + break + else + t[i] = nil + break + end + end + end + return x +end + + function lume.shuffle(t) local rtn = {} for i = 1, #t do diff --git a/test/test_lume.lua b/test/test_lume.lua index 1a4289b..ac7b2d1 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -117,6 +117,18 @@ tests["lume.push"] = function() testeq(t, { 1, 2, 3, 4, 5, 6, 7 }) end +-- lume.remove +tests["lume.remove"] = function() + local t = { 1, 2, 3, 4, 5 } + lume.remove(t, 3) + testeq(t, { 1, 2, 4, 5 }) + lume.remove(t, 1) + testeq(t, { 2, 4, 5 }) + lume.remove(t, 5) + testeq(t, { 2, 4 }) + local m = { a = 1, b = 2, c = 3 } +end + -- lume.shuffle tests["lume.shuffle"] = function() local t = {1, 2, 3, 4, 5}