Module:ListFunctions
Jump to navigation
Jump to search
List To Vertical Table (lst2VertTable)
- Input
- A Variable number of parameters in which each parameter is a list of comma separated values
- Output
- A table body (no header or close) in which each parameter is a column
{| class="wikitable" style="text-align:center; ! Header 1 !! Header 2 !! Header 3 {{#invoke:ListFunctions|lsts2VertTable|a,b,c|1,2,3|!,@,#}} |}
Header 1 | Header 2 | Header 3 |
---|---|---|
a | 1 | ! |
b | 2 | @ |
c | 3 | # |
List To Horizontal Table (lst2HorzTable)
- Input
- A Variable number of parameters in which each parameter is a list of comma separated values
- Output
- A table body (no header or close) in which each parameter is a row
{| class="wikitable" style="text-align:center; ! Header 1 !! Header 2 !! Header 3 {{#invoke:ListFunctions|lsts2HorzTable|a,b,c|1,2,3|!,@,#}} |}
Header 1 | Header 2 | Header 3 |
---|---|---|
a | b | c |
1 | 2 | 3 |
! | @ | # |
local p = {}
local trim = mw.text.trim
local utils = require('Module:Utilities')
local strings = require('Module:Strings')
local function morphList(list, separator)
local result = {}
local text = mw.text.split(list, separator)
for i = 1, #text, 1 do
text[i] = trim(text[i])
if not (text[i] == "" or not text[i]) then
result[#result + 1] = text[i]
end
end
return result
end
function p.lstSeparator(frame)
local list = morphList(frame.args[1], frame.args[2])
local output_separator = frame.args[3]
for i = 1, #list, 1 do
list[i] = trim(list[i])
end
list = table.concat(list, output_separator)
return list
end
function p.lstCount(frame)
local list = morphList(frame.args[1], frame.args[2])
return #list
end
function p.lstIndex(frame)
local list = morphList(frame.args[1], frame.args[2])
local index = tonumber(frame.args[3])
if index <= 0 then
index = (#list + 1) + index
end
return trim(list[index])
end
function p.lstSub(frame)
local list = morphList(frame.args[1], frame.args[2])
local output_separator = frame.args[3]
local start_index = tonumber(frame.args[4])
local length = tonumber(frame.args[5])
local result_table = {}
if start_index <= 0 then
start_index = (#list + 1) + start_index
end
if length > 0 then
length = start_index + length - 1
else
length = #list + length
end
for i = start_index, length, 1 do
result_table[#result_table + 1] = list[i]
end
return table.concat(result_table, output_separator)
end
function p.lstFind(frame)
local item = frame.args[1]
local list = morphList(frame.args[2], frame.args[3])
local lowerCase = frame.args[4]
if lowerCase == "cs" then
lowerCase = false
else
lowerCase = true
end
if lowerCase then
for i = 1, #list, 1 do
if string.lower(list[i]) == string.lower(item) then
return list[i]
end
end
else
for i = 1, #list, 1 do
if list[i] == item then
return list[i]
end
end
end
return ""
end
function p.lstApp(frame)
local separator = frame.args[2]
local list = morphList(frame.args[1], separator)
local item = trim(frame.args[3])
if item ~= "" then
list[#list+1] = item
end
return table.concat(list, separator)
end
function p.lstPrep(frame)
local separator = frame.args[2]
local list = morphList(frame.args[3], separator)
local item = trim(frame.args[1])
local new_list = {}
if item ~= "" then
new_list[1] = item
end
if new_list[1] then
for i = 1, #list, 1 do
new_list[i+1] = list[i]
end
else
new_list = list
end
return table.concat(new_list, separator)
end
function p.lstJoin(frame)
local first_list = morphList(frame.args[1], frame.args[2])
local second_list = morphList(frame.args[3], frame.args[4])
local separator = frame.args[5]
for i = 1, #second_list, 1 do
first_list[#first_list + 1] = second_list[i]
end
return table.concat(first_list, separator)
end
function p.lstUniq(frame)
local mode = frame.args[1]
local list = morphList(frame.args[2], frame.args[3])
local separator = frame.args[4]
local lowerCase = frame.args[5]
local seen = {}
if lowerCase == "cs" then
lowerCase = false
else
lowerCase = true
end
for index,item in ipairs(list) do
if lowerCase then
if seen[string.lower(item)] then
table.remove(list, index)
else
seen[string.lower(item)] = true
end
else
if seen[item] then
table.remove(list, index)
else
seen[item] = true
end
end
end
if mode == "cnt" then
return #list
end
return table.concat(list, separator)
end
function p.lstSrt(frame)
local list = morphList(frame.args[1], frame.args[2])
local separator = frame.args[3]
local numeric = frame.args[4]
local reversed = frame.args[5]
if numeric == "y" then
for i = 1, #list, 1 do
list[i] = tonumber(list[i])
end
end
table.sort(list)
if reversed == "y" then
temp_list = list
list = {}
for i = #temp_list, 1, -1 do
list[#list + 1] = temp_list[i]
end
end
return table.concat(list, separator)
end
function p.lstMapSimple(frame)
local list = morphList(frame.args[1], frame.args[2])
local token = frame.args[3]
local pattern = frame.args[4]
local separator = frame.args[5]
local result = {}
for i = 1, #list, 1 do
result[i] = string.gsub(pattern, token, list[i])
end
return table.concat(result, separator)
end
function p.lstMap(frame)
local list = morphList(frame.args[1], frame.args[2])
local token = frame.args[3]
local pattern = frame.args[4]
local separator = frame.args[5]
local result = {}
for i = 1, #list, 1 do
result[i] = string.gsub(pattern, token, list[i])
end
return frame:preprocess(table.concat(result, separator))
end
function p.lstFltr(frame)
local items = morphList(frame.args[1], frame.args[2])
local list = morphList(frame.args[3], frame.args[4])
local separator = frame.args[5]
local lowerCase = frame.args[6]
local result = {}
if lowerCase == "cs" then
lowerCase = false
else
lowerCase = true
end
if lowerCase then
for i = 1, #items, 1 do
for j = 1, #list, 1 do
if string.lower(items[i]) == string.lower(list[j]) then
result[#result+1] = list[j]
end
end
end
else
for i = 1, #items, 1 do
for j = 1, #list, 1 do
if items[i] == list[j] then
result[#result+1] = list[j]
end
end
end
end
return table.concat(result, separator)
end
function p.lstTrm(frame)
local item = frame.args[1]
local list = morphList(frame.args[2], frame.args[3])
local separator = frame.args[4]
local lowerCase = frame.args[5]
local result = {}
if lowerCase == "cs" then
lowerCase = false
else
lowerCase = true
end
if lowerCase then
for i = 1, #list, 1 do
if string.lower(item) ~= string.lower(list[j]) then
result[#result+1] = list[j]
end
end
else
for i = 1, #list, 1 do
if item ~= list[j] then
result[#result+1] = list[j]
end
end
end
return table.concat(result, separator)
end
function p.lsts2VertTable(...)
params = utils.resolveParameter(...,0)
lists = {}
for _,param in ipairs(params) do
lists[#lists+1] = morphList(param,',')
end
t = ''
for minor=1,#lists[1] do
t = t..'|-'..nl
for major=1,#lists do
t = t..'|'..lists[major][minor]..nl
end
end
return t
end
function p.lsts2HorzTable(...)
params = utils.resolveParameter(...,0)
lists = {}
for _,param in ipairs(params) do
lists[#lists+1] = morphList(param,',')
end
t = ''
for major=1,#lists do
t = t..'|-'..nl
for minor=1,#lists[major] do
t = t..'|'..lists[major][minor]..nl
end
end
return t
end
return p