mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-14 20:11:27 +07:00
builder:has_all/any
This commit is contained in:
@@ -120,6 +120,8 @@ collect_garbage :: ()
|
||||
```
|
||||
builder :: builder
|
||||
builder:has :: fragment -> boolean
|
||||
builder:has_all :: fragment... -> boolean
|
||||
builder:has_any :: fragment... -> boolean
|
||||
builder:get :: fragment -> component
|
||||
builder:set :: fragment, component -> builder
|
||||
builder:remove :: fragment -> builder
|
||||
|
||||
@@ -8622,9 +8622,15 @@ do
|
||||
do
|
||||
local b = evo.builder()
|
||||
|
||||
assert(b:has_all())
|
||||
assert(not b:has_any())
|
||||
|
||||
assert(not b:has(f1) and b:get(f1) == nil)
|
||||
assert(not b:has(f2) and b:get(f2) == nil)
|
||||
|
||||
assert(not b:has_all(f1, f2))
|
||||
assert(not b:has_any(f1, f2))
|
||||
|
||||
do
|
||||
local e = b:build(true)
|
||||
|
||||
@@ -8637,6 +8643,9 @@ do
|
||||
assert(b:has(f1) and b:get(f1) == 41)
|
||||
assert(not b:has(f2) and b:get(f2) == nil)
|
||||
|
||||
assert(not b:has_all(f1, f2))
|
||||
assert(b:has_any(f1, f2))
|
||||
|
||||
do
|
||||
local e = b:build(true)
|
||||
|
||||
@@ -8649,6 +8658,9 @@ do
|
||||
assert(b:has(f1) and b:get(f1) == 41)
|
||||
assert(b:has(f2) and b:get(f2) == 42)
|
||||
|
||||
assert(b:has_all(f1, f2))
|
||||
assert(b:has_any(f1, f2))
|
||||
|
||||
do
|
||||
local e = b:build(true)
|
||||
|
||||
@@ -8661,6 +8673,9 @@ do
|
||||
assert(not b:has(f1) and b:get(f1) == nil)
|
||||
assert(b:has(f2) and b:get(f2) == 42)
|
||||
|
||||
assert(not b:has_all(f1, f2))
|
||||
assert(b:has_any(f1, f2))
|
||||
|
||||
do
|
||||
local e = b:build(true)
|
||||
|
||||
@@ -8670,6 +8685,9 @@ do
|
||||
|
||||
b:remove(f2)
|
||||
|
||||
assert(not b:has_all(f1, f2))
|
||||
assert(not b:has_any(f1, f2))
|
||||
|
||||
assert(not b:has(f1) and b:get(f1) == nil)
|
||||
assert(not b:has(f2) and b:get(f2) == nil)
|
||||
|
||||
|
||||
80
evolved.lua
80
evolved.lua
@@ -6220,6 +6220,86 @@ function __evolved_builder_mt:has(fragment)
|
||||
return true
|
||||
end
|
||||
|
||||
---@param ... evolved.fragment fragments
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_builder_mt:has_all(...)
|
||||
local fragment_count = select("#", ...)
|
||||
|
||||
if fragment_count == 0 then
|
||||
return true
|
||||
end
|
||||
|
||||
local has = self.has
|
||||
local has_all = self.has_all
|
||||
|
||||
if fragment_count == 1 then
|
||||
local f1 = ...
|
||||
return has(self, f1)
|
||||
end
|
||||
|
||||
if fragment_count == 2 then
|
||||
local f1, f2 = ...
|
||||
return has(self, f1) and has(self, f2)
|
||||
end
|
||||
|
||||
if fragment_count == 3 then
|
||||
local f1, f2, f3 = ...
|
||||
return has(self, f1) and has(self, f2) and has(self, f3)
|
||||
end
|
||||
|
||||
if fragment_count == 4 then
|
||||
local f1, f2, f3, f4 = ...
|
||||
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4)
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2, f3, f4 = ...
|
||||
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4) and
|
||||
has_all(self, __lua_select(5, ...))
|
||||
end
|
||||
end
|
||||
|
||||
---@param ... evolved.fragment fragments
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_builder_mt:has_any(...)
|
||||
local fragment_count = select("#", ...)
|
||||
|
||||
if fragment_count == 0 then
|
||||
return false
|
||||
end
|
||||
|
||||
local has = self.has
|
||||
local has_any = self.has_any
|
||||
|
||||
if fragment_count == 1 then
|
||||
local f1 = ...
|
||||
return has(self, f1)
|
||||
end
|
||||
|
||||
if fragment_count == 2 then
|
||||
local f1, f2 = ...
|
||||
return has(self, f1) or has(self, f2)
|
||||
end
|
||||
|
||||
if fragment_count == 3 then
|
||||
local f1, f2, f3 = ...
|
||||
return has(self, f1) or has(self, f2) or has(self, f3)
|
||||
end
|
||||
|
||||
if fragment_count == 4 then
|
||||
local f1, f2, f3, f4 = ...
|
||||
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4)
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2, f3, f4 = ...
|
||||
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4) or
|
||||
has_any(self, __lua_select(5, ...))
|
||||
end
|
||||
end
|
||||
|
||||
---@param fragment evolved.fragment
|
||||
---@return evolved.component?
|
||||
---@nodiscard
|
||||
|
||||
Reference in New Issue
Block a user