Module:List

From Puella Magi Wiki
Jump to navigation Jump to search

Formats its arguments as a comma-separated list. Specify "and" for a conjunctive list or "or" to make the list disjunctive.


local getArgs = require('Module:Arguments').getArgs
local p = {}

local function joinList(elems, conj)
	if #elems == 0 then return "<span class='error'>No elements in list</span>" end
	if #elems == 1 then return elems[1] end
	if #elems == 2 then return table.concat(elems, ' ' .. conj .. ' ') end
	return table.concat(elems, ', ', 1, #elems - 1) .. ', ' .. conj .. ' ' .. elems[#elems]
end

local function cleanList(elems)
	local newElems = {}
	local i = 0
	for j, value in ipairs(elems) do
		value = mw.text.trim(value)
		if value and value ~= "" then
			i = i + 1
			newElems[i] = value
		end
	end
	return newElems
end

p['and'] = function(frame)
	return joinList(cleanList(getArgs(frame)), 'and')
end

p['or'] = function(frame)
	return joinList(cleanList(getArgs(frame)), 'or')
end

return p