Bước tới nội dung

Mô đun:parameters/lite/list

Từ điển mở Wiktionary
local function clean(x) return x and #x > 0 and x or nil end

-- implements a list parameter, somewhat
return function (args, param, allow_holes, allow_empty)
	local results = {clean(args[param] and mw.text.trim(args[param]))}
	local maximum_index = 0
	local pattern = "^" .. require("Module:String/pattern escape")(param) .. "([0-9]+)$"
	
	for key, value in pairs(args) do
		local index = mw.ustring.match(key, pattern)
		if index then
			index = tonumber(index)
			if index and index > 0 then
				value = value and mw.text.trim(value)
				if not allow_empty then value = clean(value) end
				results[index] = value
				maximum_index = math.max(maximum_index, index)
			end
		end
	end
	
	if maximum_index > 0 then
		if not allow_holes then
			local i = 1
			local j = 1
			while i <= maximum_index do
				if results[i] then
					results[j] = results[i]
					if j < i then results[i] = nil end
					j = j + 1
				end
				i = i + 1
			end
		else
			results.maxindex = maximum_index
		end
	end
	
	return results
end