Module:Util
This is a meta module.
This module is meant to be used only by other modules. It should not be invoked in wikitext.
Overview
Provides utility functions for programming modules.
Structure
Group | Description |
---|---|
util.cast | utilities for casting values (i.e. from arguments) |
util.html | shorthand functions for creating some html tags |
util.misc | miscellaneous functions |
Usage
This module should be loaded with require()
.
The above documentation is transcluded from Module:Util/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-- Utility stuff
local util = {cast={}}
util.cast = {}
util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''}
function util.cast.bool(str)
-- Takes a string and casts it to a bool value
-- False will be according to util.cast.bool_false
if str == nil then
return false
end
local tmp = string.lower(str)
for _, v in ipairs(util.cast.bool_false) do
if v == tmp then
return false
end
end
return true
end
function util.cast.int(str, default)
-- Attempts to cast a string to int
-- If default is nil and the conversion fails, an error will be returned
if string.match(str, '^%d+$') then
return tonumber(str)
elseif default ~= nil then
return default
else
error('"' .. str .. '" is not an integer')
end
end
return util