Halo Esports Wiki

We are currently performing extensive maintenance to this wiki and as a result there will likely be errors. Please be patient while we work to fix all problems.

READ MORE

Halo Esports Wiki
Advertisement

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

local util_args = require("Module:ArgsUtil")
local util_sort = require("Module:SortUtil")
local util_table = require("Module:TableUtil")
local util_vars = require("Module:VarsUtil")

local h = {}

local p = {}

function p.removeUnusedColumns(COLUMNS, data)
	-- remove any columns from COLUMNS for which data is nil
	-- note that this means that if you want to show placeholder data, you need to
	-- break up data markup into two phases
	-- first to establish columns that should be present
	-- and second to add placeholder values to columns that are locally but not globally nil
	util_args.require(2, COLUMNS, data)
	local uncheckedColumns = util_table.arrayHash(COLUMNS)
	h.discoverNonExistingColumns(uncheckedColumns, data)
	h.removeNonExistingColumns(uncheckedColumns, COLUMNS)
end

function h.discoverNonExistingColumns(uncheckedColumns, data)
	for _, row in ipairs(data) do
		if not next(uncheckedColumns) then return end
		for col, _ in pairs(uncheckedColumns) do
			if row[col] then
				uncheckedColumns[col] = nil
			end
		end
	end
end

function h.removeNonExistingColumns(uncheckedColumns, COLUMNS)
	local columnsToRemove = {}
	for col, index in pairs(uncheckedColumns) do
		columnsToRemove[#columnsToRemove+1] = { col = col, index = index }
	end
	util_sort.tablesByKeys(columnsToRemove, 'index', false)
	-- columnsToRemove is now in order of decreasing key
	-- so it's safe to table.remove in order without updating any indices
	for _, row in ipairs(columnsToRemove) do
		table.remove(COLUMNS, row.index)
	end
end

return p
Advertisement