mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Updated lume.each() to use iter func based on table type
This commit is contained in:
26
lume.lua
26
lume.lua
@@ -21,6 +21,13 @@ local math_sqrt = math.sqrt
|
|||||||
local math_abs = math.abs
|
local math_abs = math.abs
|
||||||
local math_pi = math.pi
|
local math_pi = math.pi
|
||||||
|
|
||||||
|
local noop = function()
|
||||||
|
end
|
||||||
|
|
||||||
|
local identity = function(x)
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
local patternescape = function(str)
|
local patternescape = function(str)
|
||||||
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
||||||
end
|
end
|
||||||
@@ -35,8 +42,18 @@ local iscallable = function(x)
|
|||||||
return mt and mt.__call ~= nil
|
return mt and mt.__call ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local identity = function(x)
|
local isarray = function(x)
|
||||||
return x
|
return x[1] and true or false
|
||||||
|
end
|
||||||
|
|
||||||
|
local getiter = function(x)
|
||||||
|
if x == nil then
|
||||||
|
return noop
|
||||||
|
elseif isarray(x) then
|
||||||
|
return ipairs
|
||||||
|
else
|
||||||
|
return pairs
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local iteratee = function(x)
|
local iteratee = function(x)
|
||||||
@@ -145,10 +162,11 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.each(t, fn, ...)
|
function lume.each(t, fn, ...)
|
||||||
|
local iter = getiter(t)
|
||||||
if type(fn) == "string" then
|
if type(fn) == "string" then
|
||||||
for _, v in pairs(t) do v[fn](v, ...) end
|
for _, v in iter(t) do v[fn](v, ...) end
|
||||||
else
|
else
|
||||||
for _, v in pairs(t) do fn(v, ...) end
|
for _, v in iter(t) do fn(v, ...) end
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user