local ext = {} --NOT RETURNING
function ext.nullFunction(param) -- this is just "empty" function that does nothing in order to not having to condition whether parameter carrying function name or not, basically it's cstr.empty ofr functions
return param
end
-------------------------------------- string --------------------------------------
--string.replace
--string.split
--string.trim
function string.replace(text, pattern, replacement)
return text:gsub(pattern, replacement)
end
function string.split(self, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(self, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function string.trim(self)
return self:gsub("^%s+", ""):gsub("%s+$", "")
end
------------------------------------ string END ------------------------------------
-------------------------------------- table --------------------------------------
--table.add (synonym for insert function)
--table.addRange
--table.contains
--table.copy
--table.count
--table.distinct
--table.empty
--table.flatten
--table.get
--table.join
--table.keys
--table.max
--table.min
function table.add(tab, element)
table.insert(tab, element)
end
function table.addRange(self, ...)
list = {}
if type(...) ~= 'table' then
list = {...}
else
list = ...
end
for _, item in ipairs(list) do
table.insert(self, item)
end
return self
end
----Applies a function to all elements
--function table.applyFunction(elements, funct, parameters)
-- require("Module:Strings")
-- local result = {}
-- if elements then
-- for _, el in ipairs(elements) do
-- table.insert(result, funct(el, parameters[1], parameters[2]))
-- end
-- end
-- return result
--end
--tbl = table to search in
--element = searched element in the table
function table.contains(tbl, element)
for _, value in pairs(tbl) do
if type(value) == 'table' and type(element) == 'table' then --table comparison
if table.concat(value) == table.concat(element) then
return true
end
elseif value == element then
return true
end
end
return false
end
--makes copy of table without referring to original table, used for duplicating a read-only tables due to mw.loadData()
function table.copy(self)
local result = {}
for key, value in pairs(self) do
if type(value) == types.table then
result[key] = table.copy(value)
else
result[key] = value
end
end
return result
end
function table.count(tab)
local count = 0
if not tab then return 0 end
for _ in pairs(tab) do
count = count + 1
end
return count
end
function table.distinct(elTable, distinctBy)
distinctBy = distinctBy or "name"
resultDistinct = {}
local addedElements = {}
for _, el in ipairs(elTable) do
if not addedElements[el[distinctBy]] then
addedElements[el[distinctBy]] = 1
table.add(resultDistinct, el)
end
end
mw.log("DISTINCT: " .. mw.dumpObject(table.get(resultDistinct, distinctBy)))
elTable = resultDistinct
return elTable
end
function table.empty(tab)
return next(tab) == nil
end
function table.flatten(input, level, accumulator)
accumulator = accumulator or {}
level = level or 1 --default value will flatten only one level
for _, element in ipairs(input) do
if type(element) == 'table' and (level > 0 or level <= -1) then --level indicates how many times the function is supposed to nest. If negative number is passed, the nesting will happens as long as it allows the table structure
table.flatten(element, level - 1, accumulator)
else
table.insert(accumulator, element)
end
end
return accumulator
end
--function that returns a list of certain field from a list of tables (such as All "names" of DLC list)
function table.get(self, field)
result = {}
for _, item in ipairs(self) do
table.insert(result, item[field])
end
return result
end
--("Hello", "World", " - ") => Hello - World
--({"Hello", "World"}, {" - "}) => Hello - World
--({"Hello", "Nice", "Beautiful", "World"}, {", ", "and "}) => Hello, Nice, Beautiful and World
function table.join(...)
require("Module:Strings") --this should be present just in case
local values
local result = cstr.empty
local sep = ", " --default value is actually not used
local lastSep = false
local additionalFunctionProcess = ext["nullFunction"]
if type(...) == "string" then --type(...) returns type of first passed parameter ("Hello", "World", " - ") becomes: "Hello - World"
values = {...}
sep = table.remove(values)
else --if the parameters are passed in table it will join the strings inside with the table with either second parameter or default separator ", "
-- ({"Hello", "World"}, " - ")
values = {...}
if type(values[2]) == types.string then
sep = table.remove(values)
elseif type(values[2]) == types.table and type(values[2][1]) == types.string then -- ({"Hello", "World"}, {" - "})
if type(values[2][2]) == types.string then -- ({"Hello", "World"}, {" - ", " and "})
lastSep = values[2][2]
end
sep = values[2][1]
--If there is third parameter as a table, it will be considered as a name for additional function that we want to be used onto Value variable
if type(values[3]) == types.table and type(values[3][1]) == types.string then -- ({"Hello", "World"}, {" - ", "and "}, {"bclr"})
additionalFunctionProcess = stringsObjs[values[3][1]]
table.remove(values) -- remove the third table
end
table.remove(values) --remove the second table
end
values = table.remove(values)
end
for i, value in ipairs(values) do
result = result .. additionalFunctionProcess(value) .. ((i == #values - 1 and lastSep) or (i < #values and sep) or cstr.empty)
end
return result
end
--get list of keys from table
function table.keys(t)
local keys = {}
for key, _ in pairs(t) do
table.insert(keys, key)
end
return keys
end
function table.max(t)
table.sort(t)
return t[#t]
end
function table.min(t)
table.sort(t)
return t[1]
end
function spairs(t)
local keys = table.keys(t)
table.sort(keys)
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
------------------------------------ table END ------------------------------------
Advertisement
Module:Extensions
Advertisement