Module:Variables

From Liquipedia Commons Wiki
Module documentation[view] [edit] [history] [purge]

Wrapper for the VariablesLua extension, and offers some extra utility.

API[edit]

Programmatic name: Variables

varDefine(name: string, value: any) → void

Defines this variable


varDefault(name: string, default: any) → any

Returns the variable defined for name, or returns the default if the variable has not been defined (or is nil)


varDefaultMulti(...: strings) → any

Returns the first existing variable in ..., or returns the last variables name.


See all our documentation here.


---
-- @Liquipedia
-- wiki=commons
-- page=Module:Variables
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Class = require('Module:Class')

local Variables = {}

---Stores a wiki-variable and returns the empty string
---@param name wikiVariableKey Key of the wiki-variable
---@param value wikiVariableValue Value of the wiki-variable
---@return string #always the empty string
function Variables.varDefine(name, value)
	return mw.ext.VariablesLua.vardefine(name, value)
end

---Stores a wiki-variable and returns the stored value
---@param name wikiVariableKey Key of the wiki-variable
---@param value wikiVariableValue Value of the wiki-variable
---@return string
function Variables.varDefineEcho(name, value)
	return mw.ext.VariablesLua.vardefineecho(name, value)
end

---Gets the stored value of a wiki-variable
---@generic T
---@param name wikiVariableKey Key of the wiki-variable
---@param default T fallback value if wiki-variable is not defined
---@return string|T
---@overload fun(name: wikiVariableKey):string?
function Variables.varDefault(name, default)
	local val = mw.ext.VariablesLua.var(name)
	return (val ~= '' and val ~= nil) and val or default
end

---
---@param ... wikiVariableKey wiki-variable keys
---@return string
function Variables.varDefaultMulti(...)
	--pack varargs
	local varargs = { n = select('#', ...), ... }

	for i = 1, varargs.n do
		local val = Variables.varDefault(varargs[i])
		if val then
			return val
		end
	end

	-- If even the last var didn't bring anything return the last argument
	return varargs[varargs.n]
end

---@param name wikiVariableKey Key of the wiki-variable
---@return boolean
function Variables.varExists(name)
	return Variables.varDefault(name) ~= nil
end

return Class.export(Variables, {removeBlanks = false})