Module:Extensions
Jump to navigation
Jump to search
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.padding
--string.replace
--string.split
--string.trim
function string.padding(number, padding)
return string.format("%0" .. (padding or 2) .. "d", number)
end
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.split(text, delim, opt)
local s, l = 1, #text
local function f()
if s then
local e, n = string.find( text, delim, s, true )
local ret
if not e then
ret = string.sub( text, s )
s = nil
elseif n < e then
-- Empty separator!
ret = string.sub( text, s, e )
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and string.sub( text, s, e - 1 ) or ''
s = n + 1
end
if not opt.noTrim then
ret = string.trim(ret)
end
if ret == '' and opt.removeEmpty then
return f()
end
return ret
end
end
return f, nil, nil
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
--table.replace
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
--caseInsensitive flag indicating whether we want ignore case sensitivity
function table.contains(tbl, element, caseInsensitive)
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 caseInsensitive and string.lower(value) == string.lower(element) then
return true
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 tab and next(tab) == nil
end
function table.flatten(input, accumulator)
accumulator = accumulator or {}
for _, element in ipairs(input) do
if type(element) == 'table' then
table.flatten(element, accumulator)
else
table.insert(accumulator, element)
end
end
return accumulator
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
local tmpTable = table.copy(element)
table.insert(accumulator, tmpTable)
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
--tbl = table with elements
--pattern = searched pattern to be replaced
--replacement = substitution of original pattern
--field = optionally, if the table has tables oinstead of strings, whcih field is supposed to be processed
function table.replace(tbl, pattern, replacement, field)
for _, el in pairs(tbl) do
if field and type(el) == 'table' then
el[field] = string.replace(el[field], pattern, replacement)
else
el = string.replace(el, pattern, replacement)
end
end
return tbl
end
---sorted pairs based on keys
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 ------------------------------------