Module:Existsmod: Difference between revisions
Jump to navigation
Jump to search
m (1 revision imported) |
No edit summary |
||
Line 10: | Line 10: | ||
local checkType = require("libraryUtil").checkType | local checkType = require("libraryUtil").checkType | ||
local normalize = require(" | local normalize = require("Module:NormalizeModuleName") | ||
local getArgs = require(" | local getArgs = require("Module:Arguments").getArgs | ||
function p.module_exists(name, foundVal, missingVal) | function p.module_exists(name, foundVal, missingVal) | ||
checkType(" | checkType("Module:Existsmod", 1, name, "string", true) | ||
if name then | if name then |
Latest revision as of 05:11, 2 May 2021
Documentation for this module may be created at Module:Existsmod/doc
-- <nowiki> -------------------------------------------------------------------------------- -- An inexpensive alternative to {{#ifexist}} which looks for a given module, -- and returns one of two values depending on whether it was found. -- -- Syntax: -- {{#invoke:Existsmod|main|<module-name>|<value-if-found>|<value-if-missing>}} -------------------------------------------------------------------------------- local p = {} local checkType = require("libraryUtil").checkType local normalize = require("Module:NormalizeModuleName") local getArgs = require("Module:Arguments").getArgs function p.module_exists(name, foundVal, missingVal) checkType("Module:Existsmod", 1, name, "string", true) if name then name = table.concat{normalize(name)} if package.loaded[name] then return foundVal else local _, val = pcall(package.loaders[2], name) if type(val) == "function" or type(val) == "string" then return foundVal end end end return missingVal end function p.main(frame) local args = getArgs(frame) return p.module_exists(args[1], args[2], args[3]) end return p -- </nowiki> -- (Add categories here.)