Module:Utils

From Eco - English Wiki
Revision as of 06:26, 23 November 2024 by StalEF (talk | contribs) (Replaced content with "local p = {} --- Trims and parses the args into a table, then returns the table function p.normalise(args) for k, v in pairs(args) do v = mw.text.trim(tostring(v)) if v ~= '' then args[k] = v end end return args end --- Trims and parses the args into a table, then returns the table -- @author User:Avaren function p.normaliseArgs(frame) local origArgs = frame:getParent().args local args = {} for k, v in pairs(origArgs) do v = mw.text.trim(tostrin...")

This module provides utility functions used from other modules.

Usage

Add the following line of code at the top of your file.

local Utils = require("Module:Utils")

-- You may then call functions from this module in your script. For example:
local tableLength = Utils.tableLen(myTable)

local p = {}

--- Trims and parses the args into a table, then returns the table
function p.normalise(args)

	for k, v in pairs(args) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end

	return args
end

--- Trims and parses the args into a table, then returns the table
-- @author User:Avaren
function p.normaliseArgs(frame)
	local origArgs = frame:getParent().args
	local args = {}

	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end

	return args
end

--- Get path to icon file.
-- @author User:Avaren
function p.checkImage(name, too_expensive)
	local icon = name:gsub('%s+', '') .. '_Icon.png'
	if too_expensive then
		return icon
	end

	if mw.title.makeTitle('File', icon).file.exists then
		return icon
	else
		return 'NoIcon.png'
	end
end

return p