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
| Name | Type | purpose |
|---|---|---|
| frame | table | A frame object. |
| targetArgs | string | the target arguments to return, possible values ("parent", "child", "both". |
| Return type | Return purpose |
|---|---|
| table | A table containing arguments. |
p.fetchArgs
This is meant to fetch a args table.
Parameters
| Name | Type | purpose |
|---|---|---|
| tTmp | table | The template args from the module {{#invoke. |
| Return type | Return purpose |
|---|---|
| table | A table containing arguments or nil. |
p.checkArgs
Check if args are enough for script to run.
Parameters
| Name | Type | purpose |
|---|---|---|
| frame | table | Scribunto frame object. |
| iArgs | number | Number of arguments required to run checkArgs. |
| bParent | boolean | Checks parent (true) or child arguments. |
| Return type | Return purpose |
|---|---|
| boolean | True if args are enough for script to run. |
p.addDays
Add days to date.
Parameters
| Name | Type | purpose |
|---|---|---|
| sDate | string | Date string e.g. 2012-01-02, YYYY-MM-DD. |
| iDays | number | Number of days to add or subtract (negative or positive). |
| bReturnTable | boolean | Set to true to returns a lua date table. |
| sFormat | string | specific format for date. |
| Return type | Return purpose |
|---|---|
| string | A calculated date. |
p.checkdate
Check if date is in appropriate format --Iso YYYY-MM-DD.
Parameters
| Name | Type | purpose |
|---|---|---|
| sDate | string | Date string e.g. 2012-01-02, YYYY-MM-DD. |
| sPat | string | A lua pattern for a date, default is (%d%d%d%d)-(%d%d)-(%d%d). |
| Return type | Return purpose |
|---|---|
| boolean | True for valid date. |
p.isempty
Check if table is empty.
Parameters
| Name | Type | purpose |
|---|---|---|
| tTab | table | A valid table. |
| Return type | Return purpose |
|---|---|
| boolean | true for empty. |
p.round
Round a number.
Parameters
| Name | Type | purpose |
|---|---|---|
| num | number | A number. |
| idp | number | Decimal points. |
| Return type | Return purpose |
|---|---|
| number | Rounded number. |
p.datediff
Calculate difference between two dates.
Parameters
| Name | Type | purpose |
|---|---|---|
| sDate1 | string | A string containing a date. |
| sDate2 | string | A string containing a date. |
| isAbsolute | boolean | absolute number (no negatives). |
| Return type | Return purpose |
|---|---|
| number | Difference between days number or null. |
p.getdate
Get lua timestamp (os.time) using custom format.
Parameters
| Name | Type | purpose |
|---|---|---|
| sDate | string | A string containing a date. |
| sPat | string | A lua pattern, default: "(%d%d%d%d)-(%d%d)-(%d%d)". |
| Return type | Return purpose |
|---|---|
| table | Lua os.time. |
p.table_print
Get a printed table.
Parameters
| Name | Type | purpose |
|---|---|---|
| tablename | table | A simple lua table. |
| Return type | Return purpose |
|---|---|
| string | A printed lua table. |
p.debug
Gets debug information for a function using lua xpcall.
Parameters
| Name | Type | purpose |
|---|---|---|
| oFunc | function | A function to debug. |
| bShowErr | boolean | True to show error. |
| Return type | Return purpose |
|---|---|
| string | A stacktrace containing error details. |
p.log
Gets variable content.
Parameters
| Name | Type | purpose |
|---|---|---|
| oData | variable | A variable to output information (e.g. a table {}). |
| Return type | Return purpose |
|---|---|
| string | Prints out variable content. |
p.nowiki
Performs a nowiki on content. Same as using mw.text.nowiki on the first argument.
Parameters
| Name | Type | purpose |
|---|---|---|
| frame | table | Scribunto frame. |
| Return type | Return purpose |
|---|---|
| string | Prints out nowiki of first argument. |