Module:Array

From Zombie Panic! Official Wiki
Revision as of 12:05, 11 June 2025 by Wuffesan (talk | contribs) (Created page with "local p = {} local lib = require('Module:Feature') function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { parentFirst = true, wrapper = { 'Template:Array' } }) return p._main(args, frame) end function p._main(args, frame) local arrayString = args[1] or args["arrayString"] or nil local separator = args[2] or args["sep"] or args["separator"] or nil local format = args[3] or args["format"] or "{item}" local join = args[4] or args["join...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Array/doc

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Array' }
	})
	return p._main(args, frame)
end

function p._main(args, frame)
	local arrayString = args[1] or args["arrayString"] or nil
	local separator = args[2] or args["sep"] or args["separator"] or nil
	local format = args[3] or args["format"] or "{item}"
	local join = args[4] or args["join"] or ""
	local dedupe = args["dedupe"] or nil
	local sort = args["sort"] or nil
	local sentence = args["sentence"] or nil
	local sentence_last = args['sentence_last'] or ' and '
	local sentence_join = args['sentence_join'] or ', '
	local firstItemOnly = args["firstItemOnly"] or nil
	local offsetStart = args["offsetStart"] or nil
	local offsetEnd = args["offsetEnd"] or nil
	local prefix = args['prefix'] or ''
	local suffix = args['suffix'] or ''
	local template = args['template'] or nil
	local split_opt = {
		removeEmpty = args['removeEmpty'] or nil,
		noTrim = args['noTrim'] or nil
	}

	-- argument validation
	if arrayString == nil then return "" end
	if separator == nil then return error("Second argument (separator) must not be empty.") end
	if separator == "" then return error("Second argument (separator) must not be empty string.") end
	if format == nil then return error("Third argument (format) must not be empty.") end

	-- split string to array
	if string.find(separator, "{newline}") then
		separator = separator:gsub("{newline}", "\n")
	end
	if string.find(separator, "{space}") then
		separator = separator:gsub("{space}", " ")
	end
	local array = lib.split(arrayString, separator, split_opt)

	-- remove duplicates from array
	if dedupe ~= nil then
		array = p._removeDuplicates(array)
	end
	
	-- sort array
	if sort ~= nil then
		array = p._sort(array, sort)
	end
	
	-- filter by offset
	if offsetStart ~= nil or offsetEnd ~= nil then
		array = lib.arraySlice(array, lib.toNullableNumber(offsetStart), lib.toNullableNumber(offsetEnd))
	end
	
	-- replace keywords in array
	for key, value in pairs(array) do
		local item = format
		if args["format" .. key] ~= nil then
			item = args["format" .. key]
		end
		if string.find(item, "{item}") then
			item = item:gsub("{item}", value)
		end
		if string.find(item, "{itemLink}") then
			item = item:gsub("{itemLink}", mw.InfoboxBuilderHF.Link(value))
		end
		if string.find(item, "{itemLinkSafe}") then
			item = item:gsub("{itemLinkSafe}", mw.InfoboxBuilderHF.Link_safe(value))
		end
		if string.find(item, "{index}") then
			item = item:gsub("{index}", key)
		end
		if string.find(item, "{newline}") then
			item = item:gsub("{newline}", "\n")
		end
		if string.find(item, "{space}") then
			item = item:gsub("{space}", " ")
		end
		array[key] = item
	end
	
	-- mw.logObject(array)
	
	-- create result array
	local result = ""
	
	-- join as sentence
	if sentence ~= nil then
		if #array == 0 then return ""
		elseif #array == 1 then return array[1]
		elseif #array == 2 then return array[1] .. sentence_last .. array[2]
		else
			local last = table.remove(array, #array)
			result = table.concat(array, sentence_join) .. sentence_join .. sentence_last .. last
		end
	-- first item only
	elseif firstItemOnly ~= nil then
		return array[1]
	-- join with join string
	else
		if string.find(join, "{newline}") then
			join = join:gsub("{newline}", "\n")
		end
		if string.find(join, "{space}") then
			join = join:gsub("{space}", " ")
		end
		result = table.concat(array, join)
	end
	result = prefix .. result .. suffix
	
	if template then
		if not args.nodpl then
			result = result
				:gsub('²{','{{')
				:gsub('}²','}}')
				:gsub('¦','|')
		end
		result = result
			:gsub('%^2{','{{')	-- for use inside DPL
			:gsub('}%^2','}}')	--
			:gsub('¹','|')		--
		result = frame:preprocess(result)
	end
	
	return result
end

function p._removeDuplicates(arr)
	local hash = {}
	local res = {}

	for _,v in ipairs(arr) do
	   if (not hash[v]) then
	       res[#res+1] = v
	       hash[v] = true
	   end
	end

	return res
end

function p._sort(arr, dir)
	local order = nil
	if (dir == -1 or dir == "-1") then
		order = function(tVal, a, b) return a > b end
	elseif (dir == 1 or dir == "1") then
		order = function(tVal, a, b) return a < b end
	else
		return arr
	end
	table.sort(arr, function(a, b) return order(t, a, b) end)
	return arr
end

return p