Module:Utils: Difference between revisions
From Eco - English Wiki
[unchecked revision] | [checked revision] |
m Use tabs for indentation, as per MediaWiki coding conventions. |
No edit summary |
||
(18 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Trims and parses the args into a table, then returns the table | --- 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) | function p.normaliseArgs(frame) | ||
local origArgs = frame:getParent().args | local origArgs = frame:getParent().args | ||
Line 16: | Line 30: | ||
end | end | ||
--- Get path to icon file. | |||
-- @author User:Avaren | |||
function p.checkImage(name, too_expensive) | function p.checkImage(name, too_expensive) | ||
local icon = name:gsub('%s+', '') .. '_Icon.png' | local icon = name:gsub('%s+', '') .. '_Icon.png' | ||
Line 25: | Line 41: | ||
return icon | return icon | ||
else | else | ||
return ' | return 'NoIcon.png' | ||
end | end | ||
end | end | ||
function p.itemId(name) | |||
return name:gsub('%s+', '') .. 'Item' | |||
end | |||
--- Check if <code>item</code> is in given <code>array</code>. | |||
-- @param item Item to look for | |||
-- @param #table array Table to check | |||
-- @return #bool <code>true</code> if <code>item</code> is in <code>array</code> | |||
-- @author User:Avaren | |||
local function in_array(item, array) | local function in_array(item, array) | ||
-- Should only use on short arrays | -- Should only use on short arrays | ||
Line 38: | Line 63: | ||
end | end | ||
--- | --- Calculate the length of a table by iterating over every item in it. | ||
-- | |||
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly. | |||
-- @param #table tbl Table to calculate the length of | |||
-- @return #number Length of the table. | |||
-- @author User:Avaren | |||
-- | |||
function p.tableLen(tbl) | function p.tableLen(tbl) | ||
local count = 0 | local count = 0 |
Latest revision as of 05:09, 24 November 2024
This module provides utility functions used from other modules.
Usage[edit source]
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
function p.itemId(name)
return name:gsub('%s+', '') .. 'Item'
end
--- Check if <code>item</code> is in given <code>array</code>.
-- @param item Item to look for
-- @param #table array Table to check
-- @return #bool <code>true</code> if <code>item</code> is in <code>array</code>
-- @author User:Avaren
local function in_array(item, array)
-- Should only use on short arrays
local set = {}
for _, l in ipairs(array) do
set[l] = true
end
return set[item] ~= nil
end
--- Calculate the length of a table by iterating over every item in it.
--
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly.
-- @param #table tbl Table to calculate the length of
-- @return #number Length of the table.
-- @author User:Avaren
function p.tableLen(tbl)
local count = 0
for _, v in ipairs(tbl) do
if v == nil then
return count
end
count = count + 1
end
return count
end
return p