Module:Feature: Difference between revisions

Jump to navigation Jump to search
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..."
 
Kwwxis (talk | contribs)
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')
checkType('Feature.gsplit', 2, delim, 'string', NIL_OK)
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
-- the mediawiki implementation uses ustring, which is slower than string
-- and also not necessary if delim isn't a pattern.
-- https://help.fandom.com/wiki/Extension:Scribunto#mw.text.split_is_very_slow
-- local g = mw.text.gsplit(text, delim, true)
-- Make sure delim is a table of strings
-- local function f()
if type(delim) == 'string' then
-- local value = g()
delim = { delim }
-- if value and not opt.noTrim then -- value is nil when the generator ends
end
-- value = mw.text.trim(value)
-- end
-- if value == "" and opt.removeEmpty then
-- return f()
-- end
-- return value
-- end
-- return f, nil, nil
-- 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
if delim==';' and text:find('(%&[#%d%w]+);') then
text = text:gsub('(%&[#%d%w]+);', '%1‡');
end
local s, l = 1, #text
local s, l = 1, #text
local function f()
local function f()
if s then
if s then
local e, n = string.find( text, delim, s, true )
-- 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
if delim==';' and ret:find('(%&[#%d%w]+)‡') then
ret = ret:gsub('(%&[#%d%w]+)‡', '%1;');
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')
checkType('Feature.split', 2, delim, 'string', NIL_OK)
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