diff --git a/sys/apis/map.lua b/sys/apis/map.lua new file mode 100644 index 0000000..4fc3bc8 --- /dev/null +++ b/sys/apis/map.lua @@ -0,0 +1,37 @@ +-- convience functions for tables with key/value pairs +local Util = require('util') + +local Map = { } + +function Map.removeMatches(t, values) + local function matchAll(entry) + for k, v in pairs(values) do + if entry[k] ~= v then + return + end + end + return true + end + + for k,v in pairs(t) do + if matchAll(v) then + t[k] = nil + end + end +end + +-- remove table entries if passed function returns false +function Map.prune(t, fn) + for _,k in pairs(Util.keys(t)) do + local v = t[k] + if type(v) == 'table' then + t[k] = Util.prune(v, fn) + end + if not fn(t[k]) then + t[k] = nil + end + end + return t +end + +return Map diff --git a/sys/apis/ui/components/Form.lua b/sys/apis/ui/components/Form.lua index 43b3557..6db3085 100644 --- a/sys/apis/ui/components/Form.lua +++ b/sys/apis/ui/components/Form.lua @@ -122,10 +122,7 @@ function UI.Form:save() end for _,child in pairs(self.children) do if child.formKey then - if (child.pruneEmpty and type(child.value) == 'string' and #child.value == 0) or - (child.pruneEmpty and type(child.value) == 'boolean' and not child.value) then - self.values[child.formKey] = nil - elseif child.validate == 'numeric' then + if child.validate == 'numeric' then self.values[child.formKey] = tonumber(child.value) else self.values[child.formKey] = child.value diff --git a/sys/apis/util.lua b/sys/apis/util.lua index 658f875..8b3d26b 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -187,20 +187,6 @@ function Util.deepMerge(obj, args) end end --- remove table entries if passed function returns false -function Util.prune(t, fn) - for _,k in pairs(Util.keys(t)) do - local v = t[k] - if type(v) == 'table' then - t[k] = Util.prune(v, fn) - end - if not fn(t[k]) then - t[k] = nil - end - end - return t -end - function Util.transpose(t) local tt = { } for k,v in pairs(t) do