317
редагувань
en>Alistair3149 (Created page with "--[[ {{Helper module |name=Paramtest |fname1 = is_empty(arg) |ftype1 = String |fuse1 = Returns true if arg is not defined or contains only whitespace |fname2 = has_content(arg...") |
м (2 ревізії: Template:Infobox_Species from StarCitizenTools) |
||
| (Не показано 3 проміжні версії 2 користувачів) | |||
| Рядок 1: | Рядок 1: | ||
-- Imported from: https://runescape.wiki/w/Module:Paramtest | |||
--[[ | --[[ | ||
{{Helper module | {{Helper module | ||
| Рядок 13: | Рядок 15: | ||
|fname4 = defaults{ {arg1,arg2},...} | |fname4 = defaults{ {arg1,arg2},...} | ||
|ftype4 = {String, Any value}... | |ftype4 = {String, Any value}... | ||
|fuse4 = Does the same as <code>default_to()</code> run over every table passed | |fuse4 = Does the same as <code>default_to()</code> run over every table passed | ||
|fname5 = table_is_empty(arg) | |||
|ftype5 = Table | |||
|fuse5 = Returns true if the table has no content, it does not check if the content of the table contains anything | |||
|fname6 = table_has_content(arg) | |||
|ftype6 = Table | |||
|fuse6 = returns true if the table has content, it does not check if the content of the table contains anything | |||
}} | }} | ||
--]] | --]] | ||
local checkType, checkTypeForNamedArg | |||
do | |||
local _libraryUtil = require("libraryUtil"); | |||
checkType = _libraryUtil.checkType; | |||
checkTypeForNamedArg = _libraryUtil.checkTypeForNamedArg; | |||
end | |||
-- | -- | ||
-- Tests basic properties of parameters | -- Tests basic properties of parameters | ||
| Рядок 28: | Рядок 44: | ||
function p.is_empty(arg) | function p.is_empty(arg) | ||
return not string.find(arg or '', '%S') | return not string.find(arg or '', '%S') | ||
end | |||
-- | |||
-- Tests if the table parameter is empty | |||
-- | |||
function p.table_is_empty(arg) | |||
for _, _ in pairs(arg) do | |||
return false | |||
end | |||
return true | |||
end | end | ||
| Рядок 45: | Рядок 72: | ||
-- Returns a list of paramaters if it has any content, or the default | -- Returns a list of paramaters if it has any content, or the default | ||
-- | -- | ||
function p.defaults( | function p.defaults(args) | ||
checkType("defaults", 1, args, "table"); | |||
local ret = {} | local ret = {} | ||
for i, v in ipairs( | for i, v in ipairs(args) do | ||
checkTypeForNamedArg("defaults", i, v, "table"); | |||
ret[i] = p.default_to(v[1], v[2]); | |||
end | end | ||
return unpack(ret) | return unpack(ret, 1, #args); | ||
end | end | ||
| Рядок 65: | Рядок 89: | ||
function p.has_content(arg) | function p.has_content(arg) | ||
return string.find(arg or '', '%S') | return string.find(arg or '', '%S') | ||
end | |||
-- | |||
-- Tests if the table parameter has content | |||
-- The same as !table_is_empty, but this is more readily clear | |||
-- | |||
function p.table_has_content(arg) | |||
for _, _ in pairs(arg) do | |||
return true | |||
end | |||
return false | |||
end | end | ||