mirror of
https://github.com/kepler155c/opus
synced 2025-01-25 22:56:53 +00:00
javascript style table functions
This commit is contained in:
parent
1264b0149b
commit
6343589183
@ -119,13 +119,13 @@ function Util.signum(num)
|
||||
end
|
||||
end
|
||||
|
||||
function Util.clamp(lo, n, hi)
|
||||
function Util.clamp(lo, num, hi)
|
||||
if num <= lo then
|
||||
return lo
|
||||
elseif num >= hi then
|
||||
return hi
|
||||
else
|
||||
return n
|
||||
return num
|
||||
end
|
||||
end
|
||||
|
||||
@ -314,15 +314,33 @@ function Util.size(list)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function isArray(value)
|
||||
-- dubious
|
||||
return type(value) == "table" and (value[1] or next(value) == nil)
|
||||
end
|
||||
|
||||
function Util.removeByValue(t, e)
|
||||
for k,v in pairs(t) do
|
||||
if v == e then
|
||||
table.remove(t, k)
|
||||
if isArray(t) then
|
||||
table.remove(t, k)
|
||||
else
|
||||
t[k] = nil
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.every(t, fn)
|
||||
for _,v in pairs(t) do
|
||||
if not fn(v) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function Util.each(list, func)
|
||||
for index, value in pairs(list) do
|
||||
func(value, index, list)
|
||||
|
Loading…
Reference in New Issue
Block a user