Модуль:Utils: различия между версиями
Материал из Eco - Русская Wiki
| [непроверенная версия] | [досмотренная версия] |
StalEF (обсуждение | вклад) Нет описания правки |
StalEF (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 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. | 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 | end | ||
function p. | --- 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 | end | ||
return args | |||
end | 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 | |||
if | |||
end | end | ||
function | --- 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 | ||
-- | --- 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 | |||
for _, v in ipairs(tbl) do | |||
if v == nil then | |||
return count | |||
end | |||
count = count + 1 | |||
end | |||
return count | |||
end | end | ||
| Строка 143: | Строка 70: | ||
end | end | ||
function p.getLanguageName( | function p.getLanguageName() | ||
local languageName = "English" | local languageName = "English" | ||
if | local language = mw.language.getContentLanguage() | ||
if | local languageCode = language:getCode() | ||
if | if languageCode == "ru" then languageName = "Russian" end | ||
if languageCode == "de" then languageName = "German" end | |||
if languageCode == "fr" then languageName = "French" end | |||
return languageName | return languageName | ||
end | |||
function p.checkImage(filename) | |||
if filename then | |||
if mw.title.makeTitle('Media', filename).file.exists then return "Y" else return "N" end | |||
else return "Error name" end | |||
end | end | ||
return p | return p | ||
Версия от 06:50, 2 июля 2025
Для документации этого модуля может быть создана страница Модуль:Utils/doc
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
function p.getLanguageCode()
local language = mw.language.getContentLanguage()
local languageCode = language:getCode()
return languageCode
end
function p.getLanguageName()
local languageName = "English"
local language = mw.language.getContentLanguage()
local languageCode = language:getCode()
if languageCode == "ru" then languageName = "Russian" end
if languageCode == "de" then languageName = "German" end
if languageCode == "fr" then languageName = "French" end
return languageName
end
function p.checkImage(filename)
if filename then
if mw.title.makeTitle('Media', filename).file.exists then return "Y" else return "N" end
else return "Error name" end
end
return p