[checked revision] | [checked revision] |
(odictSafe (via Mediawiker ST3)) |
([ST]) |
||
(30 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
end |
end |
||
return tbl |
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) |
||
⚫ | |||
+ | new[i] = f(unpack(new[i])) |
||
+ | end |
||
+ | return new |
||
end |
end |
||
Line 16: | Line 29: | ||
for k, v in ipairs(tbl) do |
for k, v in ipairs(tbl) do |
||
tbl[k] = f(v, ...) |
tbl[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.arrayInPlaceWithIndex(tbl, f, ...) |
||
⚫ | |||
⚫ | |||
end |
end |
||
return tbl |
return tbl |
||
Line 37: | Line 57: | ||
util_table.removeFalseEntries(tbl2) |
util_table.removeFalseEntries(tbl2) |
||
for k, v in pairs(tbl2) do |
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, ...) |
tbl2[k] = f(v, ...) |
||
end |
end |
||
Line 57: | Line 85: | ||
end |
end |
||
− | function p. |
+ | function p.selfDictInPlace(self, tbl, f, ...) |
− | + | for _, v in ipairs(tbl) do |
|
+ | tbl[v] = f(self, tbl[v], ...) |
||
⚫ | |||
⚫ | |||
end |
end |
||
− | return |
+ | return tbl |
end |
end |
||
+ | |||
− | |||
function p.rowsInPlace(tbl, f, ...) |
function p.rowsInPlace(tbl, f, ...) |
||
for k, row in ipairs(tbl) do |
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, ...) |
f(row, ...) |
||
row.index = k |
row.index = k |
||
Line 94: | Line 137: | ||
end |
end |
||
− | function p.splitAndConcat(str, sep, f, sep2) |
+ | function p.splitAndConcat(str, sep, f, sep2, ...) |
if not str or str == '' then return nil end |
if not str or str == '' then return nil end |
||
if not sep2 then sep2 = '' end |
if not sep2 then sep2 = '' end |
||
− | local tbl = p.split(str, sep, f) |
+ | local tbl = p.split(str, sep, f, ...) |
return table.concat(tbl, sep2) |
return table.concat(tbl, sep2) |
||
end |
end |
||
− | function p. |
+ | 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 = {} |
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 |
||
⚫ | |||
+ | end |
||
end |
end |
||
return tbl2 |
return tbl2 |
||
Line 112: | Line 173: | ||
if not tbl then return nil end |
if not tbl then return nil end |
||
if not next(tbl) then return nil end |
if not next(tbl) then return nil end |
||
− | return util_table.concat(tbl, sep, |
+ | 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 |
end |
||
− | function |
+ | function p.formatInto(sub, str) |
return str:format(sub) |
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