Module:Feature: Difference between revisions
Created page with "-- Module copied from the Genshin Impact wiki -- Revision as of 11-10-2024 --- Miscellaneous useful functions. local lib = {} local util = require('libraryUtil') local checkType = util.checkType local checkTypeMulti = util.checkTypeMulti local NIL_OK = true --- Choose one of two values to return. -- @param {boolean} cond Determines which value to return. -- @param T The value to return if `cond` is true (or truthy). -- @param F The value to return if `cond` is false..." |
No edit summary |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
-- Module copied from the Genshin Impact wiki | -- Module copied from the Genshin Impact wiki and Zenless Zone Zero wiki | ||
-- Revision as of | -- Revision as of 09-03-2025 | ||
--- Miscellaneous useful functions. | --- Miscellaneous useful functions. | ||
local lib = {} | local lib = {} | ||
| Line 122: | Line 122: | ||
local output = {} | local output = {} | ||
for item in lib.gsplit(text, delim, opt) do | for item in lib.gsplit(text, delim, opt) do | ||
table.insert(output, item) | if opt ~= nil and opt.keyTable then | ||
output[item] = true | |||
else | |||
table.insert(output, item) | |||
end | |||
end | end | ||
return output | return output | ||
| Line 275: | Line 279: | ||
local i, j, minus, int, fraction = tostring(n):find('([-]?)(%d+)([.]?%d*)') | local i, j, minus, int, fraction = tostring(n):find('([-]?)(%d+)([.]?%d*)') | ||
if not int then return n end | |||
-- reverse the int-string and append a comma to all blocks of 3 digits | -- reverse the int-string and append a comma to all blocks of 3 digits | ||
int = int:reverse():gsub("(%d%d%d)", "%1,") | int = int:reverse():gsub("(%d%d%d)", "%1,") | ||
| Line 324: | Line 328: | ||
end | end | ||
return false | return false | ||
end | |||
-- nullable number | |||
function lib.toNullableNumber(str) | |||
if str == nil then | |||
return nil | |||
end | |||
return tonumber(str) | |||
end | |||
-- array slice | |||
function lib.arraySlice(tbl, first, last) | |||
if first ~= nil and first < 0 then | |||
first = #tbl + 1 + first | |||
end | |||
if last ~= nil and last < 0 then | |||
last = #tbl + last | |||
end | |||
local sliced = {} | |||
for i = first or 1, last or #tbl, 1 do | |||
sliced[#sliced+1] = tbl[i] | |||
end | |||
return sliced | |||
end | |||
--sanitize file name | |||
--removes invalid characters for file name | |||
function lib.sanitizeFileName(str) | |||
str = string.gsub(str, "[:*?\"<>|]", "") | |||
str = string.gsub(str, "[/\\]", " ") | |||
return str | |||
end | end | ||
return lib | return lib | ||