Module:Util: Difference between revisions
Jump to navigation
Jump to search
>OmegaK2 (Created page with "-- Utility stuff local util = {cast={}} util.cast = {} util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''} function util.cast.bool(str) -- Takes a string...") |
>OmegaK2 (renamed the functions and made them take abitary values) |
||
Line 1: | Line 1: | ||
-- Utility stuff | -- Utility stuff | ||
local util = { | local util = {} | ||
util.cast = {} | util.cast = {} | ||
util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''} | util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''} | ||
function util.cast. | function util.cast.boolean(value) | ||
-- Takes | -- Takes an abitary value and casts it to a bool value | ||
-- | -- | ||
if | -- for strings false will be according to util.cast.bool_false | ||
local t = type(value) | |||
if t == 'nil' then | |||
return false | return false | ||
end | elseif t == 'boolean' then | ||
local tmp = string.lower( | return value | ||
elseif t == 'number' then | |||
if t == 0 then return false end | |||
return true | |||
elseif t == 'string' then | |||
local tmp = string.lower(value) | |||
for _, v in ipairs(util.cast.bool_false) do | |||
if v == tmp then | |||
return false | |||
end | |||
end | end | ||
return true | |||
else | |||
error('"' .. value .. ' is of uncaptured type "' .. t .. '"') | |||
end | end | ||
end | end | ||
function util.cast. | function util.cast.number(value, default) | ||
-- | -- Takes an abitary value and attempts to cast it to int | ||
-- | -- | ||
if | -- For strings: if default is nil and the conversion fails, an error will be returned | ||
return tonumber( | local t = type(value) | ||
return | if t == 'nil' then | ||
return 0 | |||
elseif t == 'boolean' then | |||
if value then | |||
return 1 | |||
else | |||
return 0 | |||
end | |||
elseif t == 'number' then | |||
return value | |||
elseif t == 'string' then | |||
val = tonumber(value) | |||
if val == nil then | |||
if default ~= nil then | |||
return default | |||
else | |||
error('"' .. value .. '" is not an integer') | |||
end | |||
else | |||
return val | |||
end | |||
else | else | ||
error('"' .. | error('"' .. value .. ' is of uncaptured type "' .. t .. '"') | ||
end | end | ||
end | end | ||
return util | return util |
Revision as of 14:44, 9 July 2015
![](/images/thumb/5/51/New_meta_module.png/45px-New_meta_module.png)
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 = {}
util.cast = {}
util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''}
function util.cast.boolean(value)
-- Takes an abitary value and casts it to a bool value
--
-- for strings false will be according to util.cast.bool_false
local t = type(value)
if t == 'nil' then
return false
elseif t == 'boolean' then
return value
elseif t == 'number' then
if t == 0 then return false end
return true
elseif t == 'string' then
local tmp = string.lower(value)
for _, v in ipairs(util.cast.bool_false) do
if v == tmp then
return false
end
end
return true
else
error('"' .. value .. ' is of uncaptured type "' .. t .. '"')
end
end
function util.cast.number(value, default)
-- Takes an abitary value and attempts to cast it to int
--
-- For strings: if default is nil and the conversion fails, an error will be returned
local t = type(value)
if t == 'nil' then
return 0
elseif t == 'boolean' then
if value then
return 1
else
return 0
end
elseif t == 'number' then
return value
elseif t == 'string' then
val = tonumber(value)
if val == nil then
if default ~= nil then
return default
else
error('"' .. value .. '" is not an integer')
end
else
return val
end
else
error('"' .. value .. ' is of uncaptured type "' .. t .. '"')
end
end
return util