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..." |
Add stuff from ZZZ wiki |
||
| 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 11-10-2024 | -- Revision as of 11-10-2024 | ||
--- Miscellaneous useful functions. | --- Miscellaneous useful functions. | ||
| Line 42: | Line 42: | ||
function lib.gsplit(text, delim, opt) | function lib.gsplit(text, delim, opt) | ||
checkType('Feature.gsplit', 1, text, 'string') | checkType('Feature.gsplit', 1, text, 'string') | ||
checkTypeMulti('Feature.gsplit', 2, delim, {'string', 'table', 'nil'}) | |||
checkType('Feature.gsplit', 3, opt, 'table', NIL_OK) | checkType('Feature.gsplit', 3, opt, 'table', NIL_OK) | ||
if delim == nil then delim = " " end | if delim == nil then delim = " " end | ||
if opt == nil then opt = {} end | if opt == nil then opt = {} end | ||
-- | -- Make sure delim is a table of strings | ||
if type(delim) == 'string' then | |||
delim = { delim } | |||
end | |||
-- based on https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/1eecdac6def6418fb36829cc2f20b464c30e4b37/includes/Engines/LuaCommon/lualib/mw.text.lua#L222 | -- based on https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/1eecdac6def6418fb36829cc2f20b464c30e4b37/includes/Engines/LuaCommon/lualib/mw.text.lua#L222 | ||
local s, l = 1, #text | local s, l = 1, #text | ||
local function f() | local function f() | ||
if s then | if s then | ||
local e, | -- e: start index | ||
-- n: end index | |||
local e = nil | |||
local n = nil | |||
for _, dv in ipairs(delim) do | |||
local de, dn = string.find( text, dv, s, true ) | |||
if de then | |||
if e == nil or de < e then | |||
e = de | |||
n = dn | |||
end | |||
end | |||
end | |||
local ret | local ret | ||
if not e then | if not e then | ||
| Line 93: | Line 93: | ||
if ret == '' and opt.removeEmpty then | if ret == '' and opt.removeEmpty then | ||
return f() | return f() | ||
end | end | ||
return ret | return ret | ||
| Line 118: | Line 115: | ||
function lib.split(text, delim, opt) | function lib.split(text, delim, opt) | ||
checkType('Feature.split', 1, text, 'string') | checkType('Feature.split', 1, text, 'string') | ||
checkTypeMulti('Feature.split', 2, delim, {'string', 'table', 'nil'}) | |||
checkType('Feature.split', 3, opt, 'table', NIL_OK) | checkType('Feature.split', 3, opt, 'table', NIL_OK) | ||
local output = {} | local output = {} | ||
| Line 324: | Line 321: | ||
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 | ||