Module:UtilsPackage: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Created page with "local p = {} local utilsTable = require("Module:UtilsTable") function p.submodules(submodules) local module = {} local moduleDoc = {} for submoduleKey, submodulePage in p...")
 
m (1 revision imported: Author request.)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


local utilsTable = require("Module:UtilsTable")
-- 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
function p.submodules(submodules)
-- See [[Module:UtilsLayout]] for example
function p.submodules(submodules, sectionHeadings)
local module = {}
local module = {}
local moduleDoc = {}
local moduleDoc = {}
for submoduleKey, submodulePage in pairs(submodules) do
for i, submodulePage in ipairs(submodules) do
local submodule = require(submodulePage)
local submodule = require(submodulePage)
for exportKey, export in pairs(submodule) do
for exportKey, export in pairs(submodule) do
if module[exportKey] then
if module[exportKey] and exportKey ~= "Documentation" and exportKey ~= "Schemas" then
mw.addWarning(string.format("Module conflict: <code>%</code> is exported by more than one submodule.", exportKey))
mw.addWarning(string.format("Module conflict: <code>%s</code> is exported by more than one submodule.", exportKey))
end
end
module[exportKey] = export
module[exportKey] = export
end
end
moduleDoc[submoduleKey] = submodulePage
moduleDoc[i] = {
heading = sectionHeadings and sectionHeadings[i],
section = submodulePage,
}
end
end
module.Documentation = moduleDoc
module.Documentation = function() return { sections = moduleDoc } end
return module
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
end


return p
return p

Latest revision as of 01:41, 22 January 2023


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