Module:Pn

MyWikiBiz, Author Your Legacy — Friday November 15, 2024
Revision as of 05:18, 16 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The module returns one value from its list of unnamed parameters.

The named parameter Template:Para is the index of the parameter that is to be returned.

Negative indices count backward from the end of the list.

A wrapper template may be used to simplify usage.

Examples

  • {{#invoke:Pn |getVal | idx=1}}
  • {{#invoke:Pn |getVal | idx= | a | b | c | d | e | f }} → a
  • {{#invoke:Pn |getVal | idx=0 | a | b | c | d | e | f }}
  • {{#invoke:Pn |getVal | idx=1 | a | b | c | d | e | f }} → a
  • {{#invoke:Pn |getVal | idx=2 | a | b | c | d | e | f }} → b
  • {{#invoke:Pn |getVal | idx=-1 | a | b | c | d | e | f }} → f
  • {{#invoke:Pn |getVal | idx=99 | a | b | c | d | e | f }}

Using a wrapper template, {{P-1}}:


--[[
Module that returns one value from a list of unnamed parameters
Named parameter idx is the index of the parameter that is to be returned
Negative indices count backward from the end of the list
==]]

local p = {}

p.getVal = function(frame)
	local args = {}
	-- copy arguments from frame object and its parent
	for k, v in pairs(frame.args) do
		args[k] = v
	end
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end
	if not args[1] then
		return nil
	end
	local idx = tonumber(args.idx) or 1
	if idx < 0 then idx = #args + idx + 1 end
	return args[idx]
end

return p