We're currently performing an overhaul of our entire tournament results system so results will be missing from team and player pages for a while, and there may be some errors on tournament pages. Please bear with us as we complete this update.

Module:MapUtil

From Call of Duty Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:MapUtil/doc

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.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, h.format, 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, h.format, str)
end

function h.format(sub, str)
	return str:format(sub)
end

return p