diff options
Diffstat (limited to 'script/utility.lua')
-rw-r--r-- | script/utility.lua | 77 |
1 files changed, 70 insertions, 7 deletions
diff --git a/script/utility.lua b/script/utility.lua index 47b0c8d8..7f9d559b 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -8,6 +8,7 @@ local ipairs = ipairs local next = next local rawset = rawset local move = table.move +local tableRemove = table.remove local setmetatable = debug.setmetatable local mathType = math.type local mathCeil = math.ceil @@ -157,8 +158,10 @@ function m.dump(tbl, option) local tp = type(value) local format = option['format'] and option['format'][key] if format then - lines[#lines+1] = ('%s%s%s,'):format(TAB[deep+1], keyWord, format(value, unpack, deep+1, stack)) - elseif tp == 'table' then + value = format(value, unpack, deep+1, stack) + tp = type(value) + end + if tp == 'table' then if mark[value] and mark[value] > 0 then lines[#lines+1] = ('%s%s%s,'):format(TAB[deep+1], keyWord, option['loop'] or '"<Loop>"') elseif deep >= (option['deep'] or mathHuge) then @@ -272,7 +275,7 @@ local function sortTable(tbl) end --- 创建一个有序表 ----@param tbl table {optional = 'self'} +---@param tbl? table ---@return table function m.container(tbl) return sortTable(tbl) @@ -287,6 +290,9 @@ function m.loadFile(path, keepBom) end local text = f:read 'a' f:close() + if not text then + return nil + end if not keepBom then if text:sub(1, 3) == '\xEF\xBB\xBF' then return text:sub(4) @@ -521,6 +527,14 @@ function m.revertTable(t) return t end +function m.revertMap(t) + local nt = {} + for k, v in pairs(t) do + nt[v] = k + end + return nt +end + function m.randomSortTable(t, max) local len = #t if len <= 1 then @@ -567,7 +581,7 @@ end ---遍历文本的每一行 ---@param text string ---@param keepNL? boolean # 保留换行符 ----@return fun(text:string):string, integer +---@return fun():string, integer function m.eachLine(text, keepNL) local offset = 1 local lineCount = 0 @@ -649,9 +663,6 @@ function m.trim(str, mode) end function m.expandPath(path) - if type(path) ~= 'string' then - return nil - end if path:sub(1, 1) == '~' then local home = getenv('HOME') if not home then -- has to be Windows @@ -791,8 +802,60 @@ function m.multiTable(count, default) return current end +---@param t table +---@param sorter boolean|function +function m.getTableKeys(t, sorter) + local keys = {} + for k in pairs(t) do + keys[#keys+1] = k + end + if sorter == true then + tableSort(keys) + elseif type(sorter) == 'function' then + tableSort(keys, sorter) + end + return keys +end + +function m.arrayHas(array, value) + for i = 1, #array do + if array[i] == value then + return true + end + end + return false +end + +function m.arrayInsert(array, value) + if not m.arrayHas(array, value) then + array[#array+1] = value + end +end + +function m.arrayRemove(array, value) + for i = 1, #array do + if array[i] == value then + tableRemove(array, i) + return + end + end +end + m.MODE_K = { __mode = 'k' } m.MODE_V = { __mode = 'v' } m.MODE_KV = { __mode = 'kv' } +---@generic T: fun(param: any):any +---@param func T +---@return T +function m.cacheReturn(func) + local cache = {} + return function (param) + if cache[param] == nil then + cache[param] = func(param) + end + return cache[param] + end +end + return m |