Updated lume.each() to use iter func based on table type

This commit is contained in:
rxi
2015-01-10 14:13:07 +00:00
parent 0e4256ef0d
commit 0c21cfcf5b

View File

@@ -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