Add table.foreachi(), phase out ipairs() in API so nil values can be included in lists
This commit is contained in:
parent
2e278af5e2
commit
453cae7362
@ -140,9 +140,9 @@
|
||||
|
||||
function recurse(value)
|
||||
if type(value) == "table" then
|
||||
for _, v in ipairs(value) do
|
||||
table.foreachi(value, function(v)
|
||||
recurse(v)
|
||||
end
|
||||
end)
|
||||
else
|
||||
remover(removes, value)
|
||||
end
|
||||
@ -182,7 +182,9 @@
|
||||
if type(field.allowed) == "function" then
|
||||
return field.allowed(value)
|
||||
else
|
||||
for _,v in ipairs(field.allowed) do
|
||||
local n = #field.allowed
|
||||
for i = 1, n do
|
||||
local v = field.allowed[i]
|
||||
if value:lower() == v:lower() then
|
||||
return v
|
||||
end
|
||||
@ -341,10 +343,10 @@
|
||||
function api.setfile(target, name, field, value)
|
||||
if value:find("*") then
|
||||
local values = os.matchfiles(value)
|
||||
for _, value in ipairs(values) do
|
||||
api.setfile(target, name, field, value)
|
||||
table.foreachi(values, function(v)
|
||||
api.setfile(target, name, field, v)
|
||||
name = name + 1
|
||||
end
|
||||
end)
|
||||
else
|
||||
target[name] = path.getabsolute(value)
|
||||
end
|
||||
@ -353,10 +355,10 @@
|
||||
function api.setdirectory(target, name, field, value)
|
||||
if value:find("*") then
|
||||
local values = os.matchdirs(value)
|
||||
for _, value in ipairs(values) do
|
||||
api.setdirectory(target, name, field, value)
|
||||
table.foreachi(values, function(v)
|
||||
api.setdirectory(target, name, field, values[i])
|
||||
name = name + 1
|
||||
end
|
||||
end)
|
||||
else
|
||||
target[name] = path.getabsolute(value)
|
||||
end
|
||||
@ -411,9 +413,9 @@
|
||||
local result = {}
|
||||
function recurse(value)
|
||||
if type(value) == "table" then
|
||||
for _, v in ipairs(value) do
|
||||
recurse(v)
|
||||
end
|
||||
table.foreachi(value, function (value)
|
||||
recurse(value)
|
||||
end)
|
||||
else
|
||||
setter(result, #result + 1, field, value)
|
||||
end
|
||||
|
@ -81,13 +81,15 @@
|
||||
--
|
||||
|
||||
function table.flatten(arr)
|
||||
local result = { }
|
||||
local result = {}
|
||||
|
||||
local function flatten(arr)
|
||||
for _, v in ipairs(arr) do
|
||||
local n = #arr
|
||||
for i = 1, n do
|
||||
local v = arr[i]
|
||||
if type(v) == "table" then
|
||||
flatten(v)
|
||||
else
|
||||
elseif v then
|
||||
table.insert(result, v)
|
||||
end
|
||||
end
|
||||
@ -98,6 +100,29 @@
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Walk the elements of an array and call the specified function
|
||||
-- for each non-nil element. This works around a "feature" of the
|
||||
-- ipairs() function that stops iteration at the first nil.
|
||||
--
|
||||
-- @param arr
|
||||
-- The array to iterate.
|
||||
-- @param func
|
||||
-- The function to call. The value (not the index) will be passed
|
||||
-- as the only argument.
|
||||
--
|
||||
|
||||
function table.foreachi(arr, func)
|
||||
local n = #arr
|
||||
for i = 1, n do
|
||||
local v = arr[i]
|
||||
if v then
|
||||
func(v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Merge two lists into an array of objects, containing pairs
|
||||
-- of values, one from each list.
|
||||
|
Loading…
Reference in New Issue
Block a user