ATTENTION! The process of updating WiKi to version Eco 10.x has begun. Those wishing to participate can find out more Information on our ECO Contribution Wiki Discord.
From April 26 to May 12, errors may occur in the Wiki, as we will be carrying out a major update to the information processing modules.

Module:Utils: Difference between revisions

From Eco - English Wiki
[unchecked revision][checked revision]
No edit summary
(p.tableLen)
Line 32: Line 32:
function p.tableLen(tbl)
function p.tableLen(tbl)
   local count = 0
   local count = 0
     for _, v in ipairs(t) do
     for _, v in ipairs(tbl) do
       if v == nil then
       if v == nil then
         return count
         return count

Revision as of 14:32, 27 February 2021

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.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.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 'NoImage.png'
  end
end

-- mw.LoadData prevents #table from working correctly
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