Fandom Developers Wiki
Advertisement

Utility is a meta-module that consists of common and useful Lua functions that can be used in many Lua scripts.

Note: This module should be imported using require. It is not meant to be used directly in a page or template (e.g. {{#invoke:). 

Usage[]

These functions can only be accessed from another module.

For example:

Using a module

local u = require('Module:Utility')

function getmilliseconds()
   return u.getdate('2010-02-01')
end

Module information

Functions

p.getArgs

Put all arguments into a real table - Returns Parent, child args table.
Parameters
NameTypepurpose
frametableA frame object.
targetArgsstringthe target arguments to return, possible values ("parent", "child", "both".
Return typeReturn purpose
tableA table containing arguments.

p.fetchArgs

This is meant to fetch a args table.
Parameters
NameTypepurpose
tTmptableThe template args from the module {{#invoke.
Return typeReturn purpose
tableA table containing arguments or nil.

p.checkArgs

Check if args are enough for script to run.
Parameters
NameTypepurpose
frametableScribunto frame object.
iArgsnumberNumber of arguments required to run checkArgs.
bParentbooleanChecks parent (true) or child arguments.
Return typeReturn purpose
booleanTrue if args are enough for script to run.

p.addDays

Add days to date.
Parameters
NameTypepurpose
sDatestringDate string e.g. 2012-01-02, YYYY-MM-DD.
iDaysnumberNumber of days to add or subtract (negative or positive).
bReturnTablebooleanSet to true to returns a lua date table.
sFormatstringspecific format for date.
Return typeReturn purpose
stringA calculated date.

p.checkdate

Check if date is in appropriate format --Iso YYYY-MM-DD.
Parameters
NameTypepurpose
sDatestringDate string e.g. 2012-01-02, YYYY-MM-DD.
sPatstringA lua pattern for a date, default is (%d%d%d%d)-(%d%d)-(%d%d).
Return typeReturn purpose
booleanTrue for valid date.

p.isempty

Check if table is empty.
Parameters
NameTypepurpose
tTabtableA valid table.
Return typeReturn purpose
booleantrue for empty.

p.round

Round a number.
Parameters
NameTypepurpose
numnumberA number.
idpnumberDecimal points.
Return typeReturn purpose
numberRounded number.

p.datediff

Calculate difference between two dates.
Parameters
NameTypepurpose
sDate1stringA string containing a date.
sDate2stringA string containing a date.
isAbsolutebooleanabsolute number (no negatives).
Return typeReturn purpose
numberDifference between days number or null.

p.getdate

Get lua timestamp (os.time) using custom format.
Parameters
NameTypepurpose
sDatestringA string containing a date.
sPatstringA lua pattern, default: "(%d%d%d%d)-(%d%d)-(%d%d)".
Return typeReturn purpose
tableLua os.time.

p.table_print

Get a printed table.
Parameters
NameTypepurpose
tablenametableA simple lua table.
Return typeReturn purpose
stringA printed lua table.

p.debug

Gets debug information for a function using lua xpcall.
Parameters
NameTypepurpose
oFuncfunctionA function to debug.
bShowErrbooleanTrue to show error.
Return typeReturn purpose
stringA stacktrace containing error details.

p.log

Gets variable content.
Parameters
NameTypepurpose
oDatavariableA variable to output information (e.g. a table {}).
Return typeReturn purpose
stringPrints out variable content.

p.nowiki

Performs a nowiki on content. Same as using mw.text.nowiki on the first argument.
Parameters
NameTypepurpose
frametableScribunto frame.
Return typeReturn purpose
stringPrints out nowiki of first argument.

See also[]

Advertisement