Jump to content

Module:Pn

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by RexxS (talk | contribs) at 17:51, 15 May 2020 (copy frame arguments explicitly). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
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
Negatives 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 error("No values supplied")
	end
	local idx = tonumber(args.idx) or 1
	if idx < 1 then idx = #args + idx + 1 end
	if idx > #args then idx = #args end
	return (args[idx] or " arg[idx] ") .. (#args or " #args ") .. (idx or " idx ")
end

return p