Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
((via Mediawiker ST3))
([ST])
(12 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
end
 
end
   
function p.allInPlace(tbl, f, ...)
+
function p.multiParamSafe(tbl, f, ...)
 
if not tbl or #tbl == 0 or #tbl[1] == 0 then return end
 
if not tbl or #tbl == 0 or #tbl[1] == 0 then return end
 
local util_vars = require("Module:VarsUtil")
 
local util_vars = require("Module:VarsUtil")
 
local new = util_table.interlace(tbl)
 
local new = util_table.interlace(tbl)
  +
util_vars.log(tbl)
for i, v in ipairs(new[1]) do
 
  +
util_vars.log('********')
tbl[i] = f(unpack(v))
 
  +
util_vars.log(new)
 
for i, v in ipairs(new) do
 
new[i] = f(unpack(new[i]))
 
end
 
end
return tbl
+
return new
 
end
 
end
   
Line 54: 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 70: Line 81:
 
for _, v in ipairs(tbl) do
 
for _, v in ipairs(tbl) do
 
tbl[v] = f(tbl[v], ...)
 
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
 
end
 
return tbl
 
return tbl
Line 77: Line 95:
 
for k, row in ipairs(tbl) do
 
for k, row in ipairs(tbl) do
 
f(row, ...)
 
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
 
row.index = k
 
end
 
end
Line 118: Line 144:
 
end
 
end
   
function p.allSplitAndConcat(tbl, sep, f, sep2, ...)
+
function p.multiParamSplitAndConcat(tbl, sep, f, sep2, ...)
 
if not tbl or not #tbl == 0 or not tbl[1] then return nil end
 
if not tbl or not #tbl == 0 or not tbl[1] then return nil end
 
if not sep2 then sep2 = '' end
 
if not sep2 then sep2 = '' end
 
p.inPlace(tbl, p.split, sep)
 
p.inPlace(tbl, p.split, sep)
p.allInPlace(tbl, f, ...)
+
local new = p.multiParamSafe(tbl, f, ...)
return table.concat(tbl, sep2)
+
return table.concat(new, sep2)
  +
end
  +
  +
function p.concatField(tbl, field, sep, f, ...)
  +
return util_table.concat(p.extractField(tbl, field), sep, f, ...)
 
end
 
end
   
Line 143: 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, h.format, str)
+
return util_table.concat(tbl, sep, p.formatInto, str)
 
end
 
end
   
Line 149: Line 179:
 
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 p.inPlace(tbl, h.format, str)
+
return p.inPlace(tbl, p.formatInto, str)
 
end
 
end
   
function h.format(sub, str)
+
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