Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Utils: Difference between revisions

From MCC Island Wiki
added round function (originally local to Module:CosmeticMachine)
added isAnimated function (previously on both Module:CosmeticInfo and Module:CosmeticCrates)
 
Line 33: Line 33:
local sign = n < 0 and -1 or 1
local sign = n < 0 and -1 or 1
return math.floor(n * factor + 0.5) / factor
return math.floor(n * factor + 0.5) / factor
end
function utils.isAnimated(frame, filename)
if not frame.preprocess then
return true
end
local content = frame:preprocess('{{:File:' .. filename .. '}}')
return content:find('Category:Animated images') ~= nil
end
end


return utils
return utils

Latest revision as of 08:42, 8 September 2025

Documentation for this module may be created at Module:Utils/doc

local utils = {}

function utils.expand(frame, title, ...)
	local args = {...}
	local templateArgs = {}
	
	for i, arg in ipairs(args) do
		if type(arg) == "string" and arg:match("^[^=]+=.+$") then
			local key, value = arg:match("^([^=]+)=(.+)$")
			templateArgs[key] = value
		else
			templateArgs[i] = tostring(arg)
		end
	end
	
	if frame.expandTemplate then
		return frame:expandTemplate{ title = title, args = templateArgs }
	else
		local argString = {}
		for i, arg in ipairs(args) do
			table.insert(argString, tostring(arg))
		end
		return string.format("{{%s|%s}}", title, table.concat(argString, "|"))
	end
end

function utils.round(n)
	local frac = math.abs(n) % 1
	if frac == 0 then return n end
	local zeros = tostring(frac):match("%.(0*)") or ""
	local places = #zeros + 2
	local factor = 10 ^ places
	local sign = n < 0 and -1 or 1
	return math.floor(n * factor + 0.5) / factor
end

function utils.isAnimated(frame, filename)
	if not frame.preprocess then
		return true
	end
	local content = frame:preprocess('{{:File:' .. filename .. '}}')
	return content:find('Category:Animated images') ~= nil
end

return utils