sprites/util/lua-ext.lua
Christopher Monsanto 0c47e34a43 Build refactor
There's still more implementation cleanup to do, but I at least wanted
to publish the new build DSL, which is stable-ish at this point
2020-05-06 01:45:56 -04:00

52 lines
950 B
Lua

-- Allow `for v in iter(table)` instead of `for _, v in ipairs(table)`
function iter(table)
local i = 1
return function ()
local v = table[i]
i = i + 1
return v
end
end
function astable(table)
local t = type(table)
if t == 'table' then
return table
elseif t == 'string' then
return {table}
elseif t == 'nil' then
return {}
end
end
-- Adapted from premake
function flatten(arr)
local result = {}
local function flatten(arr)
for v in iter(arr) do
if type(v) == "table" then
flatten(v)
else
table.insert(result, v)
end
end
end
flatten(arr)
return result
end
function trim(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
function table_keys(t)
local result = {}
for k, v in pairs(t) do
table.insert(result, k)
end
return result
end