Difference between revisions of "Module:Test/lib/util"
Jump to navigation
Jump to search
(Created page with "local util = { _DESCRIPTION = 'an assortment of useful things', } --ref: https://gist.github.com/ripter/4270799 function util.tprint(tbl, indent) if not indent then inden...") |
|||
Line 1: | Line 1: | ||
− | local util = { | + | local util = {} |
− | |||
− | } | ||
− | -- | + | ------------------------ |
− | function util. | + | -- table manipulation -- |
− | + | ------------------------ | |
+ | |||
+ | util.table = {} | ||
+ | |||
+ | -- procedure | ||
+ | function util.table.overwrite(dest, source, ignoreKeys) | ||
+ | ignoreKeys = ignoreKeys or {} | ||
− | + | for sK,sV in pairs(source) do | |
− | + | local ignore = false | |
− | |||
− | |||
− | + | for _,ignoredK in ipairs(ignoreKeys) do | |
− | + | if sK == ignoredK then | |
− | + | ignore = true | |
− | + | break | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | + | if not ignore then | |
− | + | if type(sV) == "table" then | |
− | + | if type(dest[sK]) == "table" then | |
− | + | util.table.overwrite(dest[sK], sV, ignoreKeys) | |
− | + | else | |
− | + | dest[sK] = {} | |
− | + | util.table.overwrite(dest[sK], sV, ignoreKeys) | |
− | |||
end | end | ||
− | + | else | |
− | + | dest[sK] = sV | |
+ | end | ||
end | end | ||
− | + | end | |
end | end | ||
--ref: https://gist.github.com/balaam/3122129 | --ref: https://gist.github.com/balaam/3122129 | ||
− | function util. | + | function util.table.reverse(tbl) |
− | + | local reversed_table = {} | |
− | + | local length = #tbl | |
− | + | ||
− | + | for i,v in ipairs(tbl) do | |
+ | reversed_table[length + 1 - i] = v | ||
+ | end | ||
+ | |||
+ | for k,v in pairs(tbl) do | ||
+ | if type(k) ~= 'number' then | ||
+ | reversed_table[k] = v | ||
end | end | ||
− | + | end | |
+ | |||
+ | return reversed_table | ||
end | end | ||
− | --- | + | --ref: http://lua-users.org/wiki/CopyTable |
− | function util. | + | function util.table.shallowcopy(original_table) |
− | + | local orig_type = type(original_table) | |
− | + | local copy | |
− | if | + | if orig_type == 'table' then |
− | + | copy = {} | |
− | + | for orig_key, orig_value in pairs(original_table) do | |
− | + | copy[orig_key] = orig_value | |
− | + | end | |
− | + | else -- number, string, boolean, etc | |
− | + | copy = original_table | |
end | end | ||
+ | return copy | ||
end | end | ||
− | function util.count(tbl, key_type) | + | -- this is not as strict as # so might bug out in extreme situations |
+ | -- needs love | ||
+ | function util.table.count(tbl, key_type) | ||
local length = 0; | local length = 0; | ||
for k,v in pairs(tbl) do | for k,v in pairs(tbl) do | ||
Line 78: | Line 85: | ||
end | end | ||
− | -- | + | -- procedure |
− | function util. | + | -- ref: https://gist.github.com/ripter/4270799 |
− | + | function util.table.tprint(tbl, indent) | |
− | + | if not indent then indent = 0 end | |
− | |||
− | + | if type(tbl) ~= "table" then | |
+ | print(tbl) | ||
+ | return 0 | ||
+ | end | ||
− | for k,v in pairs( | + | for k, v in pairs(tbl) do |
− | + | formatting = string.rep(" ", indent) .. k .. ": " | |
+ | if type(v) == "table" then | ||
+ | print(formatting) | ||
+ | util.tprint(v, indent+1) | ||
+ | elseif type(v) == 'boolean' then | ||
+ | print(formatting .. tostring(v)) | ||
+ | else | ||
+ | print(formatting .. v) | ||
+ | end | ||
end | end | ||
− | |||
− | |||
end | end | ||
− | -- | + | -- delimiter must be a single character |
− | + | -- only for strings and numbers | |
− | -- | + | function util.table.toCSVstring(tbl, delimiter) |
− | function util. | + | delimiter = delimiter or "," |
− | + | assert(#delimiter == 1 and type(delimiter) == 'toCSVstring', "toCSVstring: bad argument #2 (single character expected)") | |
− | + | local csv = "" | |
− | |||
− | + | for k,v in pairs(tbl) do | |
− | + | if type(v) == 'string' or type(v) == 'number' then | |
+ | csv = csv .. v .. delimiter | ||
+ | else | ||
+ | assert(false, "toCSVstring: can only handle numbers and strings") | ||
end | end | ||
+ | end | ||
− | + | csv = string.sub(csv, 1, -2) -- remove final delimiter (works only for a single char) | |
− | + | ||
− | + | return csv | |
− | + | end | |
− | + | ||
− | + | ---------- | |
− | + | -- misc -- | |
− | + | ---------- | |
− | + | ||
− | + | -- procedure | |
− | + | function util.hl(title, width) | |
− | + | width = width or 80 | |
+ | |||
+ | if type(title) == "string" then | ||
+ | title = " " .. title .. " " | ||
+ | local before = math.floor((width - #title) / 2) | ||
+ | local after = width - before - #title | ||
+ | print(string.rep("-", before) .. title .. string.rep("-", after)) | ||
+ | else | ||
+ | print(string.rep("-", width)) | ||
end | end | ||
end | end | ||
− | return util | + | return util -- return module |
Revision as of 14:24, 9 May 2021
This page has been marked as needing documentation of its function and purpose. You can help RimWorld Wiki by creating it here |
local util = {} ------------------------ -- table manipulation -- ------------------------ util.table = {} -- procedure function util.table.overwrite(dest, source, ignoreKeys) ignoreKeys = ignoreKeys or {} for sK,sV in pairs(source) do local ignore = false for _,ignoredK in ipairs(ignoreKeys) do if sK == ignoredK then ignore = true break end end if not ignore then if type(sV) == "table" then if type(dest[sK]) == "table" then util.table.overwrite(dest[sK], sV, ignoreKeys) else dest[sK] = {} util.table.overwrite(dest[sK], sV, ignoreKeys) end else dest[sK] = sV end end end end --ref: https://gist.github.com/balaam/3122129 function util.table.reverse(tbl) local reversed_table = {} local length = #tbl for i,v in ipairs(tbl) do reversed_table[length + 1 - i] = v end for k,v in pairs(tbl) do if type(k) ~= 'number' then reversed_table[k] = v end end return reversed_table end --ref: http://lua-users.org/wiki/CopyTable function util.table.shallowcopy(original_table) local orig_type = type(original_table) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in pairs(original_table) do copy[orig_key] = orig_value end else -- number, string, boolean, etc copy = original_table end return copy end -- this is not as strict as # so might bug out in extreme situations -- needs love function util.table.count(tbl, key_type) local length = 0; for k,v in pairs(tbl) do if key_type then if type(k) == key_type then length = length + 1 end else length = length + 1 end end return length end -- procedure -- ref: https://gist.github.com/ripter/4270799 function util.table.tprint(tbl, indent) if not indent then indent = 0 end if type(tbl) ~= "table" then print(tbl) return 0 end for k, v in pairs(tbl) do formatting = string.rep(" ", indent) .. k .. ": " if type(v) == "table" then print(formatting) util.tprint(v, indent+1) elseif type(v) == 'boolean' then print(formatting .. tostring(v)) else print(formatting .. v) end end end -- delimiter must be a single character -- only for strings and numbers function util.table.toCSVstring(tbl, delimiter) delimiter = delimiter or "," assert(#delimiter == 1 and type(delimiter) == 'toCSVstring', "toCSVstring: bad argument #2 (single character expected)") local csv = "" for k,v in pairs(tbl) do if type(v) == 'string' or type(v) == 'number' then csv = csv .. v .. delimiter else assert(false, "toCSVstring: can only handle numbers and strings") end end csv = string.sub(csv, 1, -2) -- remove final delimiter (works only for a single char) return csv end ---------- -- misc -- ---------- -- procedure function util.hl(title, width) width = width or 80 if type(title) == "string" then title = " " .. title .. " " local before = math.floor((width - #title) / 2) local after = width - before - #title print(string.rep("-", before) .. title .. string.rep("-", after)) else print(string.rep("-", width)) end end return util -- return module