Call of Duty Esports Wiki
Advertisement

To edit the documentation or categories for this module, click here.


local p = {}

function p.keyOf(tbl, val)
	for k, v in pairs(tbl) do
		if v == val then
			return k
		end
	end
	return nil
end

function p.hash(tbl)
	local hash = {}
	for k, v in pairs(tbl) do
		hash[v] = true
	end
	return hash
end

-- sorts tblToSort to be in the same order as the elements appear in lookup
function p.sortByKeyOrder(tblToSort,lookup)
	table.sort(tblToSort, function (a,b)
			return (p.keyOf(lookup,a) or 0) < (p.keyOf(lookup,b) or 0)
		end
	)
	return
end

--[[useful for cargo queries. assumes that the structure of tblToSort is as follows:

{
	a1, a2, a3, a4,
	a1 = { key = value, ... },
	a2 = { key = value, ... },
	a3 = { key = value, ... },
	a4 = { key = value, ... }
}

Sort by value.

]]
function p.sortByValueInTable(tblToSort, key, increasing)
	table.sort(tblToSort, function (a,b)
			return (tblToSort[a] and tblToSort[a][key] or 0) > (tblToSort[b] and tblToSort[b][key] or 0) and not increasing
		end
	)
	return
end

function p.mergeArrays(tbl1,tbl2)
	-- tbl1 is modified to include the elements of tbl2 appended to the end. Order is preserved.
	for _, v in ipairs(tbl2) do
		tbl1[#tbl1+1] = v
	end
	return
end

-- table.remove for non-integer key
function p.remove(tbl, key)
	local output = tbl[key]
	tbl[key] = nil
	return output
end

return p
Advertisement