User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions
No edit summary |
|||
| Line 98: | Line 98: | ||
=== Use of functions === | === Use of functions === | ||
===== | ===== Modules that implement templates ===== | ||
Modules that implement templates should consist of at least two functions: a "main" function prefixed with an underscore, and a wrapper function of the same name but without an underscore. | |||
The use of a wrapper and "main" function allows for a module to be used directly in another module or indirectly through its corresponding template. A module should only provide '''one''' wrapper for '''one''' template. If the "main" function is short enough, its code may be merged with the wrapper function. | |||
A tester function may be added for testing purposes, which is itself a wrapper that also calls the "main" function. This allows it to be tested using the in-browser console by calling mw.logObject(p.tester()). Tester functions may be removed if the "main" function is determined to be functional under expected conditions.<syntaxhighlight lang="lua">-- "Main" function to be called by wrapper or another module | A tester function may be added for testing purposes, which is itself a wrapper that also calls the "main" function. This allows it to be tested using the in-browser console by calling mw.logObject(p.tester()). Tester functions may be removed if the "main" function is determined to be functional under expected conditions.<syntaxhighlight lang="lua">-- "Main" function to be called by wrapper or another module | ||
| Line 108: | Line 110: | ||
end | end | ||
-- Wrapper function; to be called by template | -- Wrapper function; to be called by template using {{#invoke}} | ||
function p.call_me(frame) | function p.call_me(frame) | ||
return p._call_me(frame.args) | return p._call_me(frame.args) | ||
| Line 117: | Line 119: | ||
local args = { ["something"] = 123 } | local args = { ["something"] = 123 } | ||
return p._call_me(args) | return p._call_me(args) | ||
end</syntaxhighlight> | end</syntaxhighlight><syntaxhighlight lang="lua">-- Wrapper function; to be called by template | ||
function p.call_me(frame) | |||
local arg = frame.args["arg"] | |||
-- Code goes here | |||
return call_me(arg) | |||
end</syntaxhighlight> | |||
==== Modules that serve as libraries ==== | |||
Modules that serve as libraries for other modules do not need a wrapper function, apart from the tester. Examples include [[Module:MOS]], [[Module:Rational]], and [[Module:Utils]]. | |||
Modules that serve as libraries for multiple, related templates may provide multiple wrapper functions. If a module provides multiple wrappers that each call a corresponding function, the name of the function called within the wrapper must be prefixed with an underscore.<syntaxhighlight lang="lua">-- MAIN FUNCTIONS | |||
-- Usable by other modules | |||
function p._clamp(arg1, arg2, arg3) | |||
-- Code goes here | |||
end | |||
function p._truncate(arg1) | |||
-- Code goes here | |||
end | |||
-- WRAPPER FUNCTIONS | |||
-- Usable by templates with {{#invoke}} | |||
function p.clamp(frame) | |||
return p._clamp(frame.args["arg1"], frame.args["arg2"], frame.args["arg3"]) | |||
end | |||
function p.truncate(frame) | |||
return p._truncate(frame.args["arg1"]) | |||
end</syntaxhighlight>Modules that only provide one function may simply consist of that function as a return value. Example: [[Module:Yesno]]. | |||
==== Helper functions ==== | ==== Helper functions ==== | ||