Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Documentation for this module may be created at Module:UtilsPackage/Documentation

local p = {}

-- Combine several submodules together and present them as one module
-- Useful when a module is too large to keep all on one page but should still be presented as a unified API to consumers
-- See [[Module:UtilsLayout]] for example
function p.submodules(submodules, sectionHeadings)
	local module = {}
	local moduleDoc = {}
	for i, submodulePage in ipairs(submodules) do
		local submodule = require(submodulePage)
		for exportKey, export in pairs(submodule) do
			if module[exportKey] and exportKey ~= "Documentation" and exportKey ~= "Schemas" then
				mw.addWarning(string.format("Module conflict: <code>%s</code> is exported by more than one submodule.", exportKey))
			end
			module[exportKey] = export
		end
		moduleDoc[i] = {
			heading = sectionHeadings and sectionHeadings[i],
			section = submodulePage,
		}
	end
	module.Documentation = function() return { sections = moduleDoc } end
	return module
end

-- Load a module only when needed
-- Useful when a module is only needed for certain edge cases
-- Lazy loading prevents the module from appearing in Special:Whatlinkshere when unused
-- It may also be helpful for performance
-- @returns a function which, when called, returns the module, or nil if it doesn't exist
function p.lazyLoad(moduleName)
	local module
	local moduleExists = true
	return function()
		if not module and moduleExists then
			local moduleLoaded, loadedModule = pcall(function() return require(moduleName) end)
			if moduleLoaded then
				module = loadedModule
			else
				moduleExists = false
			end
		end
		return module
	end
end

return p
Advertisement