Module:Utils: Difference between revisions

From Eco - English Wiki
[checked revision][checked revision]
Avaren (talk | contribs)
p.tableLen
No edit summary
 
(37 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
  local args = {}
local args = {}
 
 
  for k, v in pairs(origArgs) do
for k, v in pairs(origArgs) do
    v = mw.text.trim(tostring(v))
v = mw.text.trim(tostring(v))
    if v ~= '' then
if v ~= '' then
      args[k] = v
args[k] = v
    end
end
  end
end
 
 
  return args
return args
end
end


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
function p.CheckId(name)
     return icon
local itemData = mw.loadData("Module:ItemData")
  else
    local itemTable = itemData.items[name]
    return 'NoImage.png'
    if itemTable == nil then return 'NoItem' end
  end
     local IconName = itemTable.ID
return IconName
end
 
--- Check if item is in given array.
--- @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
end


-- mw.LoadData prevents #table from working correctly
--- Calculate the length of a table by iterating over every item in it.
--- @author User:Avaren
function p.tableLen(tbl)
function p.tableLen(tbl)
  local count = 0
local count = 0
    for _, v in ipairs(tbl) do
for _, v in ipairs(tbl) do
      if v == nil then
if v == nil then
        return count
return count
      end
end
      count = count + 1
count = count + 1
    end
end
  return count
return count
end
end


return p
return p

Latest revision as of 09:16, 7 December 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


function p.CheckId(name)
	local itemData = mw.loadData("Module:ItemData")
    local itemTable = itemData.items[name]
    if itemTable == nil then return 'NoItem' end
    local IconName = itemTable.ID
	
	return IconName
end

--- Check if item is in given array.
--- @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.
--- @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