Module:Utils

-- This module is intended for functions that can be used in many other modules. For such functions, it is better to have a single location and standard to avoid unneeded and potentially problematic duplication of code and effort. local p = {}

--[[ Sets a variable using Extension:Variables. Such variables persist between module invocations and template calls.

Parameters: • var_name (string): the name of the variable to set • value (string): the value to set the variable to

Returns nothing. ]] function p.set_var(var_name, value) mw.getCurrentFrame:callParserFunction({		name = "#vardefine",		args = {var_name, value}	}) end

--[[ Retrieves a variable set using Extension:Variables. The value is trimmed automatically by MediaWiki.

Parameters: • var_name (string): the name of the variable to get

Returns: • a string with the contents of the variable, if it is set; • nil, if no such variable is previously defined on the page. ]] function p.get_var(var_name) return mw.getCurrentFrame:callParserFunction({		name = "#var",		args = {var_name}	}) end

--[[ Given a table of arguments supplied to a module, retrieves an argument from the table. Allows to specify a fallback value that is returned if the argument table did not have the specified argument. Without fallbacks, missing arguments are normalized to nil values.

This function automatically trims leading and trailing whitespace off the argument since MediaWiki/Scribunto does not do so automatically, and this is typically the desired behaviour.

Parameters: • args (table): the argument table, typically retrieved from the frame used as the argument to a module entry point (the value of "f" in "function p.main(f)") • arg_name(number or string): the argument to retrieve; a number for unnamed parameters, a string for unnamed parameters • fallback(optional, any value): if this value is specified, it is returned if the argument table did not contain the specified argument. If no fallback value is needed, call the function with two arguments; the fallback will be inferred to be nil, which is the desired behaviour for this function.

Returns: • a string with the trimmed contents of the specified argument, if it is  present; • the fallback value, of any type if the specified argument is missing, but a  fallback value is provided; • nil, if the specified argument is missing, and there is no fallback. ]] function p.get_arg(args, arg_name, fallback) local arg = mw.text.trim(args[arg_name] or "") if arg == "" then return fallback end return arg end

--[[ Similar to p.get_arg, but allows a fallback list of parameter names. If the first parameter has no value specified, the function tries to retrieve the value for the second parameter in the list, and so on. The fallback value is only used if all listed options have been exhausted.

For example, `get_arg_one_of(args, {"name", 2}, fallback)` would work like this: 1) If `args` contains a value for parameter `name`, returns that value as if by  `get_arg(args, "name")`. 2) If `args` contains a value for parameter `2` (unnamed), returns that value as if by `get_arg(args, 2)`. 3) If `args` contains neither value, returns `fallback`.

Parameters: • args (table): the argument table, typically retrieved from the frame used as the argument to a module entry point (the value of "f" in "function p.main(f)") • arg_names(table of numbers and/or strings): a list of parameter names for the retrieved argument, from most preferred to least preferred • fallback(optional, any value): if this value is specified, it is returned if the argument table did not contain any of the specified arguments. If no fallback value is needed, call the function with two arguments; the fallback will be inferred to be    nil, which is the desired behaviour for this function.

Returns: • a string with the trimmed contents of the most preferred available argument, if such a value is found; • the fallback value, of any type if the specified argument is missing, but a  fallback value is provided; • nil, if the specified argument is missing, and there is no fallback. ]] function p.get_arg_one_of(args, arg_names, fallback) for i, arg_name in ipairs(arg_names) do		local arg = p.get_arg(args, arg_name) if arg ~= nil then return arg end end return fallback end

return p