[checked revision] | [checked revision] |
((via Mediawiker ST3)) |
([ST]) |
||
(43 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
⚫ | |||
local util_table = require("Module:TableUtil") |
local util_table = require("Module:TableUtil") |
||
local util_text = require("Module:TextUtil") |
local util_text = require("Module:TextUtil") |
||
Line 7: | Line 6: | ||
local p = {} |
local p = {} |
||
− | function p. |
+ | function p.inPlace(tbl, f, ...) |
+ | for k, v in pairs(tbl) do |
||
− | return util_args.splitMapConcat(str, sep, p.ucfirst) |
||
+ | tbl[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl |
||
end |
end |
||
− | function p. |
+ | function p.multiParamSafe(tbl, f, ...) |
+ | if not tbl or #tbl == 0 or #tbl[1] == 0 then return end |
||
⚫ | |||
⚫ | |||
+ | local new = util_table.interlace(tbl) |
||
+ | util_vars.log(tbl) |
||
+ | util_vars.log('********') |
||
+ | util_vars.log(new) |
||
+ | for i, v in ipairs(new) do |
||
+ | new[i] = f(unpack(new[i])) |
||
+ | end |
||
+ | return new |
||
+ | end |
||
+ | |||
+ | function p.arrayInPlace(tbl, f, ...) |
||
+ | for k, v in ipairs(tbl) do |
||
+ | tbl[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.arrayInPlaceWithIndex(tbl, f, ...) |
||
+ | for k, v in ipairs(tbl) do |
||
+ | tbl[k] = f(v, k, ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.arrayInPlaceAndMerge(tbl, f, ...) |
||
+ | -- assumes f returns table values |
||
+ | return util_table.mergeArrays(unpack(p.arrayInPlace(tbl, f, ...))) |
||
+ | end |
||
+ | |||
+ | function p.arraySafe(tbl, f, ...) |
||
+ | local tbl2 = {} |
||
+ | for k, v in ipairs(tbl) do |
||
+ | tbl2[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.safe(tbl, f, ...) |
||
+ | local tbl2 = mw.clone(tbl) |
||
+ | util_table.removeFalseEntries(tbl2) |
||
+ | for k, v in pairs(tbl2) do |
||
+ | tbl2[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.copy(tbl, f, ...) |
||
+ | local tbl2 = {} |
||
+ | for k, v in pairs(tbl) do |
||
+ | tbl2[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.dictSafe(tbl, f, ...) |
||
+ | local tbl2 = mw.clone(tbl) |
||
+ | for _, v in ipairs(tbl2) do |
||
+ | tbl2[v] = f(tbl2[v], ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.dictInPlace(tbl, f, ...) |
||
+ | for _, v in ipairs(tbl) do |
||
+ | tbl[v] = f(tbl[v], ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.selfDictInPlace(self, tbl, f, ...) |
||
+ | for _, v in ipairs(tbl) do |
||
+ | tbl[v] = f(self, tbl[v], ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.rowsInPlace(tbl, f, ...) |
||
+ | for k, row in ipairs(tbl) do |
||
+ | f(row, ...) |
||
+ | row.index = k |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.selfRowsInPlace(self, tbl, f, ...) |
||
+ | for k, row in ipairs(tbl) do |
||
+ | f(self, row, ...) |
||
+ | row.index = k |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.rowBlobInPlace(tbl, f, ...) |
||
+ | for k, row in pairs(tbl) do |
||
+ | f(row, ...) |
||
+ | row.index = k |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.dictRowsInPlace(tbl, f, ...) |
||
+ | for _, v in ipairs(tbl) do |
||
+ | f(tbl[v], ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.arrayToLookupSafe(tbl, f, ...) |
||
+ | local tbl2 = {} |
||
+ | for _, v in ipairs(tbl) do |
||
+ | tbl2[v] = f(v, ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.split(str, sep, f, ...) |
||
+ | local tbl = util_text.split(str,sep) |
||
+ | if not f then return tbl end |
||
+ | return p.inPlace(tbl, f, ...) |
||
+ | end |
||
+ | |||
+ | function p.splitAndConcat(str, sep, f, sep2, ...) |
||
+ | if not str or str == '' then return nil end |
||
+ | if not sep2 then sep2 = '' end |
||
+ | local tbl = p.split(str, sep, f, ...) |
||
+ | return table.concat(tbl, sep2) |
||
+ | end |
||
+ | |||
+ | function p.multiParamSplitAndConcat(tbl, sep, f, sep2, ...) |
||
+ | if not tbl or not #tbl == 0 or not tbl[1] then return nil end |
||
+ | if not sep2 then sep2 = '' end |
||
+ | p.inPlace(tbl, p.split, sep) |
||
+ | local new = p.multiParamSafe(tbl, f, ...) |
||
+ | return table.concat(new, sep2) |
||
+ | end |
||
+ | |||
+ | function p.concatField(tbl, field, sep, f, ...) |
||
+ | return util_table.concat(p.extractField(tbl, field), sep, f, ...) |
||
+ | end |
||
+ | |||
+ | function p.extractField(tbl, field, f, ...) |
||
+ | local tbl2 = {} |
||
+ | if f then |
||
+ | for _, row in ipairs(tbl) do |
||
+ | tbl2[#tbl2+1] = f(row[field], ...) |
||
+ | end |
||
+ | else |
||
+ | for _, row in ipairs(tbl) do |
||
+ | tbl2[#tbl2+1] = row[field] |
||
+ | end |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.formatAndConcat(tbl, sep, str) |
||
+ | if not tbl then return nil end |
||
+ | if not next(tbl) then return nil end |
||
⚫ | |||
+ | end |
||
+ | |||
+ | function p.format(tbl, str) |
||
+ | if not tbl then return nil end |
||
+ | if not next(tbl) then return nil end |
||
+ | return p.inPlace(tbl, p.formatInto, str) |
||
+ | end |
||
+ | |||
+ | function p.formatInto(sub, str) |
||
+ | return str:format(sub) |
||
end |
end |
||
Revision as of 08:40, 12 June 2021
Edit the documentation or categories for this module.
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local h = {}
local p = {}
function p.inPlace(tbl, f, ...)
for k, v in pairs(tbl) do
tbl[k] = f(v, ...)
end
return tbl
end
function p.multiParamSafe(tbl, f, ...)
if not tbl or #tbl == 0 or #tbl[1] == 0 then return end
local util_vars = require("Module:VarsUtil")
local new = util_table.interlace(tbl)
util_vars.log(tbl)
util_vars.log('********')
util_vars.log(new)
for i, v in ipairs(new) do
new[i] = f(unpack(new[i]))
end
return new
end
function p.arrayInPlace(tbl, f, ...)
for k, v in ipairs(tbl) do
tbl[k] = f(v, ...)
end
return tbl
end
function p.arrayInPlaceWithIndex(tbl, f, ...)
for k, v in ipairs(tbl) do
tbl[k] = f(v, k, ...)
end
return tbl
end
function p.arrayInPlaceAndMerge(tbl, f, ...)
-- assumes f returns table values
return util_table.mergeArrays(unpack(p.arrayInPlace(tbl, f, ...)))
end
function p.arraySafe(tbl, f, ...)
local tbl2 = {}
for k, v in ipairs(tbl) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.safe(tbl, f, ...)
local tbl2 = mw.clone(tbl)
util_table.removeFalseEntries(tbl2)
for k, v in pairs(tbl2) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.copy(tbl, f, ...)
local tbl2 = {}
for k, v in pairs(tbl) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.dictSafe(tbl, f, ...)
local tbl2 = mw.clone(tbl)
for _, v in ipairs(tbl2) do
tbl2[v] = f(tbl2[v], ...)
end
return tbl2
end
function p.dictInPlace(tbl, f, ...)
for _, v in ipairs(tbl) do
tbl[v] = f(tbl[v], ...)
end
return tbl
end
function p.selfDictInPlace(self, tbl, f, ...)
for _, v in ipairs(tbl) do
tbl[v] = f(self, tbl[v], ...)
end
return tbl
end
function p.rowsInPlace(tbl, f, ...)
for k, row in ipairs(tbl) do
f(row, ...)
row.index = k
end
return tbl
end
function p.selfRowsInPlace(self, tbl, f, ...)
for k, row in ipairs(tbl) do
f(self, row, ...)
row.index = k
end
return tbl
end
function p.rowBlobInPlace(tbl, f, ...)
for k, row in pairs(tbl) do
f(row, ...)
row.index = k
end
return tbl
end
function p.dictRowsInPlace(tbl, f, ...)
for _, v in ipairs(tbl) do
f(tbl[v], ...)
end
return tbl
end
function p.arrayToLookupSafe(tbl, f, ...)
local tbl2 = {}
for _, v in ipairs(tbl) do
tbl2[v] = f(v, ...)
end
return tbl2
end
function p.split(str, sep, f, ...)
local tbl = util_text.split(str,sep)
if not f then return tbl end
return p.inPlace(tbl, f, ...)
end
function p.splitAndConcat(str, sep, f, sep2, ...)
if not str or str == '' then return nil end
if not sep2 then sep2 = '' end
local tbl = p.split(str, sep, f, ...)
return table.concat(tbl, sep2)
end
function p.multiParamSplitAndConcat(tbl, sep, f, sep2, ...)
if not tbl or not #tbl == 0 or not tbl[1] then return nil end
if not sep2 then sep2 = '' end
p.inPlace(tbl, p.split, sep)
local new = p.multiParamSafe(tbl, f, ...)
return table.concat(new, sep2)
end
function p.concatField(tbl, field, sep, f, ...)
return util_table.concat(p.extractField(tbl, field), sep, f, ...)
end
function p.extractField(tbl, field, f, ...)
local tbl2 = {}
if f then
for _, row in ipairs(tbl) do
tbl2[#tbl2+1] = f(row[field], ...)
end
else
for _, row in ipairs(tbl) do
tbl2[#tbl2+1] = row[field]
end
end
return tbl2
end
function p.formatAndConcat(tbl, sep, str)
if not tbl then return nil end
if not next(tbl) then return nil end
return util_table.concat(tbl, sep, p.formatInto, str)
end
function p.format(tbl, str)
if not tbl then return nil end
if not next(tbl) then return nil end
return p.inPlace(tbl, p.formatInto, str)
end
function p.formatInto(sub, str)
return str:format(sub)
end
return p