Module:Utils: Difference between revisions
add clamp function |
Fix for clamp. Improve documentation |
||
| (One intermediate revision by one other user not shown) | |||
| Line 3: | Line 3: | ||
local get_args = require("Module:Arguments").getArgs | local get_args = require("Module:Arguments").getArgs | ||
local yesno = require("Module:Yesno") | local yesno = require("Module:Yesno") | ||
-- TODO??? Enforce rule for prefixing module-callable functions with an | |||
-- underscore, regardless of whether a wrapper is needed or can be made???? | |||
-- Trim a string (remove leading and trailing, but not interior, whitespace) | -- Trim a string (remove leading and trailing, but not interior, whitespace) | ||
| Line 16: | Line 19: | ||
-- Clamp function | -- Clamp function | ||
-- Underscore-prefixed in case a wrapper for a template is ever needed | -- Underscore-prefixed in case a wrapper for a template is ever needed | ||
function p._clamp( | function p._clamp(value, min_value, max_value) | ||
return math.min(math.max(value, | return math.min(math.max(value, min_value), max_value) | ||
end | end | ||
| Line 69: | Line 72: | ||
end | end | ||
-- | -- Parse a ratio string like "3/2" into a number | ||
-- Use default on error; cannot be used with {{#invoke:}} | |||
function p.eval_num_arg(input, def_value) | function p.eval_num_arg(input, def_value) | ||
local result = input | local result = input | ||