Module:Util

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

This is the root module for Zelda Wiki's libraries or utility functions. These are functions that are not directly invoked by any template—they are used only by other modules that share a common need for subroutines that can parse template arguments, manipulate tables, compare strings, and so on.

Each function is located on its own subpage. These are inteded to eventually replace the existing utility modules, such as Module:UtilsTable, which cause large job queues due to the fact that high-usage functions are packaged together. The hope is that by separating each of these functions into their own module, each individual module will be stable and unlikely to change, and new utility functions can be added without triggering any job queue at all.

Libaries

Utility functions are grouped together under the following base pages (or "libaries") according to their functionality.


local p = {}

local util = {
	markup = {
		listBulleted = require("Module:Util/markup/listBulleted"),	
	},
	pages = {
		listSubpages = require("Module:Util/pages/listSubpages"),
	},
	tables = {
		invert = require("Module:Util/tables/invert"),
		map = require("Module:Util/tables/map"),
		partition = require("Module:Util/tables/partition"),
	}
}

function p.ListLibraries(frame)
	return p.listSubpages()
end

function p.ListFunctions(frame)
	return "==Functions==\n"..p.listSubpages()
end

function p.listSubpages()
	local title = mw.title.getCurrentTitle()
	local modulePage = string.gsub(title.prefixedText, "/Documentation$", "")
	local subpages = util.pages.listSubpages(modulePage, { depth = 1, noDocumentation = true })
	
	local curriedFuncNames = {}
	local funcs = {}
	for i, v in ipairs(subpages) do
		local subpage = string.match(v, "[^/]+$")
		if string.find(subpage, "^ ") then -- the _ prefix comes out as a space in the DPL query
			table.insert(curriedFuncNames, subpage)
		else
			table.insert(funcs, {
				subpage = subpage,
				page = v
			})
		end
	end
	local curriedFuncsLookup = util.tables.invert(curriedFuncNames)

	local links = {}
	for i, v in ipairs(funcs) do
		links[i] = string.format("[[%s|<kbd>%s</kbd>]]", v.page, v.subpage)
		if curriedFuncsLookup[" "..v.subpage] then
			links[i] = links[i] .. string.format(" ([[_%s|<kbd>_%s</kbd>]])", v.page, v.subpage)
		end
	end
	local list = util.markup.listBulleted(links)
	return list
end

return p