-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
56 lines (49 loc) · 1.5 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local wrangler = {}
function wrangler.new()
local c = setmetatable({}, wrangler)
c.filters = {
["not"] = function(self, object, criterion, criteria)
for criterion, value in pairs(criteria) do
assert(self.filters[criterion], "No such filter (" .. criterion .. ")")
if self.filters[criterion](self, object, value) then
return false
end
end
end,
["or"] = function(self, object, criterion, criteria)
for criterion, value in pairs(criteria) do
assert(self.filters[criterion], "No such filter (" .. criterion .. ")")
if self.filters[criterion](self, object, value) then
return true
end
end
end
}
return c
end
function wrangler:filter(list, criteria)
local i = 1
while i < #list do
local object = list[i]
local rem = false
for criterion, value in pairs(criteria) do
assert(self.filters[criterion], "No such filter (" .. criterion .. ")")
if not self.filters[criterion](self, object, criterion, value) then
table.remove(list, i)
rem = true
break
end
end
if not rem then i = i + 1 end
end
return list
end
function wrangler:addFilter(name, filter)
assert(type(filter) == "function", "bad argument #2 to 'addFilter' (function expected, got " .. type(filter) .. ")")
self.filters[name] = filter
end
function wrangler:removeFilter(name)
self.filters[name] = nil
end
wrangler.__index = wrangler
return setmetatable(wrangler, { __call = wrangler.new })