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)
|
function recurse(value)
|
||||||
if type(value) == "table" then
|
if type(value) == "table" then
|
||||||
for _, v in ipairs(value) do
|
table.foreachi(value, function(v)
|
||||||
recurse(v)
|
recurse(v)
|
||||||
end
|
end)
|
||||||
else
|
else
|
||||||
remover(removes, value)
|
remover(removes, value)
|
||||||
end
|
end
|
||||||
@ -182,7 +182,9 @@
|
|||||||
if type(field.allowed) == "function" then
|
if type(field.allowed) == "function" then
|
||||||
return field.allowed(value)
|
return field.allowed(value)
|
||||||
else
|
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
|
if value:lower() == v:lower() then
|
||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
@ -341,10 +343,10 @@
|
|||||||
function api.setfile(target, name, field, value)
|
function api.setfile(target, name, field, value)
|
||||||
if value:find("*") then
|
if value:find("*") then
|
||||||
local values = os.matchfiles(value)
|
local values = os.matchfiles(value)
|
||||||
for _, value in ipairs(values) do
|
table.foreachi(values, function(v)
|
||||||
api.setfile(target, name, field, value)
|
api.setfile(target, name, field, v)
|
||||||
name = name + 1
|
name = name + 1
|
||||||
end
|
end)
|
||||||
else
|
else
|
||||||
target[name] = path.getabsolute(value)
|
target[name] = path.getabsolute(value)
|
||||||
end
|
end
|
||||||
@ -353,10 +355,10 @@
|
|||||||
function api.setdirectory(target, name, field, value)
|
function api.setdirectory(target, name, field, value)
|
||||||
if value:find("*") then
|
if value:find("*") then
|
||||||
local values = os.matchdirs(value)
|
local values = os.matchdirs(value)
|
||||||
for _, value in ipairs(values) do
|
table.foreachi(values, function(v)
|
||||||
api.setdirectory(target, name, field, value)
|
api.setdirectory(target, name, field, values[i])
|
||||||
name = name + 1
|
name = name + 1
|
||||||
end
|
end)
|
||||||
else
|
else
|
||||||
target[name] = path.getabsolute(value)
|
target[name] = path.getabsolute(value)
|
||||||
end
|
end
|
||||||
@ -411,9 +413,9 @@
|
|||||||
local result = {}
|
local result = {}
|
||||||
function recurse(value)
|
function recurse(value)
|
||||||
if type(value) == "table" then
|
if type(value) == "table" then
|
||||||
for _, v in ipairs(value) do
|
table.foreachi(value, function (value)
|
||||||
recurse(v)
|
recurse(value)
|
||||||
end
|
end)
|
||||||
else
|
else
|
||||||
setter(result, #result + 1, field, value)
|
setter(result, #result + 1, field, value)
|
||||||
end
|
end
|
||||||
|
@ -81,13 +81,15 @@
|
|||||||
--
|
--
|
||||||
|
|
||||||
function table.flatten(arr)
|
function table.flatten(arr)
|
||||||
local result = { }
|
local result = {}
|
||||||
|
|
||||||
local function flatten(arr)
|
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
|
if type(v) == "table" then
|
||||||
flatten(v)
|
flatten(v)
|
||||||
else
|
elseif v then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -98,6 +100,29 @@
|
|||||||
end
|
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
|
-- Merge two lists into an array of objects, containing pairs
|
||||||
-- of values, one from each list.
|
-- of values, one from each list.
|
||||||
|
Loading…
Reference in New Issue
Block a user