Skip to content

Commit 1294aa9

Browse files
committed
builder:has_all/any
1 parent 63172bb commit 1294aa9

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ collect_garbage :: ()
120120
```
121121
builder :: builder
122122
builder:has :: fragment -> boolean
123+
builder:has_all :: fragment... -> boolean
124+
builder:has_any :: fragment... -> boolean
123125
builder:get :: fragment -> component
124126
builder:set :: fragment, component -> builder
125127
builder:remove :: fragment -> builder

develop/untests.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8622,9 +8622,15 @@ do
86228622
do
86238623
local b = evo.builder()
86248624

8625+
assert(b:has_all())
8626+
assert(not b:has_any())
8627+
86258628
assert(not b:has(f1) and b:get(f1) == nil)
86268629
assert(not b:has(f2) and b:get(f2) == nil)
86278630

8631+
assert(not b:has_all(f1, f2))
8632+
assert(not b:has_any(f1, f2))
8633+
86288634
do
86298635
local e = b:build(true)
86308636

@@ -8637,6 +8643,9 @@ do
86378643
assert(b:has(f1) and b:get(f1) == 41)
86388644
assert(not b:has(f2) and b:get(f2) == nil)
86398645

8646+
assert(not b:has_all(f1, f2))
8647+
assert(b:has_any(f1, f2))
8648+
86408649
do
86418650
local e = b:build(true)
86428651

@@ -8649,6 +8658,9 @@ do
86498658
assert(b:has(f1) and b:get(f1) == 41)
86508659
assert(b:has(f2) and b:get(f2) == 42)
86518660

8661+
assert(b:has_all(f1, f2))
8662+
assert(b:has_any(f1, f2))
8663+
86528664
do
86538665
local e = b:build(true)
86548666

@@ -8661,6 +8673,9 @@ do
86618673
assert(not b:has(f1) and b:get(f1) == nil)
86628674
assert(b:has(f2) and b:get(f2) == 42)
86638675

8676+
assert(not b:has_all(f1, f2))
8677+
assert(b:has_any(f1, f2))
8678+
86648679
do
86658680
local e = b:build(true)
86668681

@@ -8670,6 +8685,9 @@ do
86708685

86718686
b:remove(f2)
86728687

8688+
assert(not b:has_all(f1, f2))
8689+
assert(not b:has_any(f1, f2))
8690+
86738691
assert(not b:has(f1) and b:get(f1) == nil)
86748692
assert(not b:has(f2) and b:get(f2) == nil)
86758693

evolved.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6220,6 +6220,86 @@ function __evolved_builder_mt:has(fragment)
62206220
return true
62216221
end
62226222

6223+
---@param ... evolved.fragment fragments
6224+
---@return boolean
6225+
---@nodiscard
6226+
function __evolved_builder_mt:has_all(...)
6227+
local fragment_count = select("#", ...)
6228+
6229+
if fragment_count == 0 then
6230+
return true
6231+
end
6232+
6233+
local has = self.has
6234+
local has_all = self.has_all
6235+
6236+
if fragment_count == 1 then
6237+
local f1 = ...
6238+
return has(self, f1)
6239+
end
6240+
6241+
if fragment_count == 2 then
6242+
local f1, f2 = ...
6243+
return has(self, f1) and has(self, f2)
6244+
end
6245+
6246+
if fragment_count == 3 then
6247+
local f1, f2, f3 = ...
6248+
return has(self, f1) and has(self, f2) and has(self, f3)
6249+
end
6250+
6251+
if fragment_count == 4 then
6252+
local f1, f2, f3, f4 = ...
6253+
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4)
6254+
end
6255+
6256+
do
6257+
local f1, f2, f3, f4 = ...
6258+
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4) and
6259+
has_all(self, __lua_select(5, ...))
6260+
end
6261+
end
6262+
6263+
---@param ... evolved.fragment fragments
6264+
---@return boolean
6265+
---@nodiscard
6266+
function __evolved_builder_mt:has_any(...)
6267+
local fragment_count = select("#", ...)
6268+
6269+
if fragment_count == 0 then
6270+
return false
6271+
end
6272+
6273+
local has = self.has
6274+
local has_any = self.has_any
6275+
6276+
if fragment_count == 1 then
6277+
local f1 = ...
6278+
return has(self, f1)
6279+
end
6280+
6281+
if fragment_count == 2 then
6282+
local f1, f2 = ...
6283+
return has(self, f1) or has(self, f2)
6284+
end
6285+
6286+
if fragment_count == 3 then
6287+
local f1, f2, f3 = ...
6288+
return has(self, f1) or has(self, f2) or has(self, f3)
6289+
end
6290+
6291+
if fragment_count == 4 then
6292+
local f1, f2, f3, f4 = ...
6293+
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4)
6294+
end
6295+
6296+
do
6297+
local f1, f2, f3, f4 = ...
6298+
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4) or
6299+
has_any(self, __lua_select(5, ...))
6300+
end
6301+
end
6302+
62236303
---@param fragment evolved.fragment
62246304
---@return evolved.component?
62256305
---@nodiscard

0 commit comments

Comments
 (0)