Module:UtilsTable

local p = {} local h = {}

local lang = mw.getLanguage('en') local utilsFunction = require("Module:UtilsFunction") local inspect = require("Module:UtilsTable/Inspect")

local MAX_INT = 9000 -- arbitrary high number local MIN_INT = -9000

-- HELPERS

function h.append(tbl, val) tbl[table.maxn(tbl)+1] = val end

function h.iteratee(iteratee) if type(iteratee) == "function" then return iteratee end if type(iteratee) == "string" or type(iteratee) == "number" then return h.property(iteratee) end if type(iteratee) == "table" then return p.matches(iteratee) end return iteratee end

function h.property(path) return function(tbl) if type(tbl) == "string" or type(tbl) == "number" then return path == tbl and tbl or nil end return p.property(path)(tbl) end end

-- GENERAL

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

function p.hasArray(tbl) if type(tbl) ~= "table" then return false end return p.size(tbl) == 0 or table.maxn(tbl) > 0 end

function p.isArray(tbl) if type(tbl) ~= "table" then return false end return #p.stringKeys(tbl) == 0 end

function p.isEmpty(tbl) return p.size(tbl) == 0 end

function p.isEqual(tbl, other) return p._isEqual(tbl)(other) end function p._isEqual(tbl) return function(other) if type(tbl) ~= "table" or type(other) ~= "table" then return tbl == other end return p.matches(tbl)(other) and p.matches(other)(tbl) end end

function p.keyBy(tbl, iteratee) return p._keyBy(iteratee)(tbl) end function p._keyBy(iteratee) iteratee = h.iteratee(iteratee) return function(tbl) local result = {} for i, v in ipairs(tbl) do			result[iteratee(v)] = v		end return result end end

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.keys(tbl) local keyset = {} for k in pairs(tbl) do     keyset[#keyset + 1] = k   end return keyset end

function p.mapValues(tbl, iteratee) return p._mapValues(iteratee)(tbl) end function p._mapValues(iteratee) local mappingFunction = h.iteratee(iteratee) return function(tbl) local result = {} for k, v in pairs(tbl) do			result[k] = mappingFunction(v) end return result end end

function p.matches(sourceTbl) return function(tbl) if sourceTbl == tbl then return true end if type(sourceTbl) ~= type(tbl) then return false end if p.size(sourceTbl) == 0 and p.size(tbl) > 0 then return false end for k, v in pairs(sourceTbl) do			if type(v) ~= "table" and v ~= tbl[k] then return false end if type(v) == "table" and not p.matches(v)(tbl[k]) then return false end end return true end end

function p.merge(tbl, ...) return p.mergeWith(utilsFunction.noop)(tbl, ...) end

function p.mergeWith(customizer) return function(tbl, ...) if tbl == nil then return nil end local result for _, source in ipairs({...}) do			result = customizer(tbl, source) if not result and type(source) == "table" then for k, v in pairs(source) do					if type(v) == "table" and type(tbl[k]) == "table" then tbl[k] = p.mergeWith(customizer)({}, tbl[k], v)					else tbl[k] = v					end end else tbl = result end end return tbl end end

function p.path(pathComponents) local path = "" for _, pathComponent in pairs(pathComponents or {}) do		if string.match(pathComponent, "^[A-Za-z_][A-Za-z_0-9]*$") then path = path .. "." .. pathComponent elseif string.match(pathComponent, "^%[.*%]$") then path = path .. pathComponent else path = path .. "[" .. p.print(pathComponent) .. "]"		end end return path end

function p.print(tbl) return inspect(tbl, {		multiline = function(t)			local size = 0			for key, val in pairs(t) do				if type(val) == "table" then					return true				end				size = size + 1			end			local singleLineLength = #inspect(t, { multiline = false })			return singleLineLength > 50 or size > #t and size - #t > 1		end	}) end

function p.property(path) return function(tbl) if type(path) ~= "table" then path = { path } end local result = tbl for i, key in ipairs(path) do			result = result[key] or result[mw.text.trim(key, '%[%]"')]			if result == nil then				return result			end		end		return result	end end

function p.stringKeys(tbl) local result = {} for k in pairs(tbl) do		if type(k) == "string" then table.insert(result, k)		end end return result end

function p.shallowClone(tbl) -- mostly to be able to use # operator on something from mw.loadData local tbl2 = {} for k, v in pairs(tbl) do		tbl2[k] = v	end return tbl2 end

function p.size(tbl) return #p.keys(tbl) end

-- See also p.values function p.ivalues(tbl) local valueset = {} for i=1, table.maxn(tbl) do  	  if tbl[i] then table.insert(valueset, tbl[i]) end end return valueset end

--	"Array" functions (using the `ipairs` iterator, mostly) --

function p.compactNils(tbl) local result = {} local j = 1 for i = 1, table.maxn(tbl) do		if tbl[i] then result[j] = tbl[i] j = j + 1 end end return result end

function p.concat(array, ...) local result = mw.clone(array) for i, arrayOrValue in ipairs({...}) do		if type(arrayOrValue) ~= "table" then h.append(result, arrayOrValue) else for i, value in ipairs(arrayOrValue) do				h.append(result, value) end end end return result end

function p.difference(tbl) return function(other) local result = {} for i, v in ipairs(tbl) do			if not p.find(other, v) then table.insert(result, v)			end end return result end end

function p.filter(tbl, iteratee) return p._filter(iteratee)(tbl) end function p._filter(iteratee) return function (tbl) local predicateFn = h.iteratee(iteratee) local results = {} for i = 1, table.maxn(tbl) do			if predicateFn(tbl[i]) then table.insert(results, tbl[i]) end end return results end end

function p.find(tbl, iteratee) return p._find(iteratee)(tbl) end function p._find(iteratee) iteratee = h.iteratee(iteratee) return function(tbl) for i, v in ipairs(tbl) do			if iteratee(v) then return i, v			end end return nil, nil end end

function p.findLast(iteratee) iteratee = h.iteratee(iteratee) return function(tbl) local i, v = p.find(p.reverse(tbl), iteratee) if not i then return nil, nil end return #tbl - i + 1, v	end end

function p.flatten(tbl) local result = {} for _, v in ipairs(tbl) do		if p.isArray(v) then result = p.concat(result, v)		else table.insert(result, v)		end end return result end

function p.flattenDeep(tbl) local result = {} for _, v in ipairs(tbl) do		if p.isArray(v) then result = p.concat(result, p.flattenDeep(v)) else table.insert(result, v)		end end return result end

function p.flatMap(mappingFunction) return function(tbl) return p.flatten(p.map(tbl, mappingFunction)) end end

function p.groupBy(key) return function (tbl) local result = {} for _, v in ipairs(tbl) do			local groupingKey = v[key] or "" local group = result[groupingKey] or {} table.insert(group, v)			result[groupingKey] = group end return result end end

function p.intersection(tbl) return function(other) local result = {} for i, v in ipairs(tbl) do			if p.find(other, v) then table.insert(result, v)			end end return result end end

function p.map(tbl, iteratee) return p._map(iteratee)(tbl) end function p._map(iteratee) return function(tbl) local mappingFunction = iteratee if type(iteratee) == "string" then mappingFunction = function(val) return val[iteratee] end end local tbl2 = {} for k, v in ipairs(tbl) do			tbl2[k] = mappingFunction(v) end return tbl2 end end

function p.mapMultiple(iteratee) mappingFunction = h.iteratee(iteratee) return function(tbl) local res = {} for k, v in ipairs(tbl) do			res[k] = {mappingFunction(v)} end return res end end

function p.max(tbl) tbl = p.padNils(MIN_INT)(tbl) return math.max(unpack(tbl)) end

function p.padNils(padValue, max) return function(tbl) padValue = padValue or '' max = max or table.maxn(tbl) local result = {} for i = 1, max do			if tbl[i] == nil then result[i] = padValue else result[i] = tbl[i] end end return result end end

-- returns a copy of tbl with the elements in opposite order (not a deep copy) function p.reverse(tbl) local tbl2 = {} local len = #tbl for i = len, 1, -1 do		tbl2[len - i + 1] = tbl[i] end return tbl2 end

function p.slice(tbl, s, e)	return p._slice(s, e)(tbl) end function p._slice(s, e)	return function(tbl) local tbl2 = {} e = e or table.maxn(tbl) for k = s, e do			tbl2[#tbl2+1] = tbl[k] end return tbl2 end end

function p.tail(tbl) local result = {} for i = 2, #tbl do		table.insert(result, tbl[i]) end return result end

function p.takeWhile(tbl, iteratee) iteratee = h.iteratee(iteratee) local i = 1 while i <= table.maxn(tbl) and iteratee(tbl[i], i) do		i = i + 1 end return p.slice(tbl, 1, i - 1) end

function p.dropRightWhile(iteratee) iteratee = h.iteratee(iteratee) return function(tbl) local result = {} local i = #tbl while i > 0 and iteratee(tbl[i], i) do			i = i - 1 end return p.slice(tbl, 1, i)	end end

function p.unique(tbl) local result = {} for _, v in ipairs(tbl) do		if not p.find(result, p._isEqual(v)) then table.insert(result, v)		end end return result end

function p.uniqueBy(iteratee) iteratee = h.iteratee(iteratee) return function(tbl) local result = {} for _, v in ipairs(tbl) do			local match = p.find(result, function(val)				return iteratee(val) == iteratee(v)			end) if not match then table.insert(result, v)			end end return result end end

-- Based on https://github.com/lua-stdlib/functional -- See https://lua-stdlib.github.io/lua-stdlib/modules/std.functional.html#zip function p.zip(tbl, emptyValue) local result = {} for outerk, innerTbl in ipairs(tbl) do		innerTbl = p.padNils("NIL")(innerTbl) -- to properly handle sparse arrays for k, v in ipairs(innerTbl) do			result[k] = result[k] or {} if v ~= "NIL" then result[k][outerk] = v			end end if emptyValue then for k in ipairs(result) do				result[k] = p.padNils(emptyValue, #tbl)(result[k]) end end end return result end

p.Documentation = { sections = { {			heading = "Tables", doc = { {					name = "hasArray", params = {"tbl"}, returns = "True if the table contains integer keys (or is empty).", cases = { {							args = , expect = true, },						{							args = – , expect = true, },						{							args = , expect = false, },						{							args = , expect = true, },					}				},				{					name = "isArray", params = {"tbl"}, returns = "True if the table contains only integer keys (or is empty).", cases = { {							args = , expect = true, },						{							args = – , expect = true, },						{							args = , expect = true, },						{							args = , expect = false, },						{							args = , expect = false, },					},				},				{					name = "isEmpty", params = {"tbl"}, returns = "True if  has no keys whatsoever", cases = { {							args = – , expect = true, },						{							args = , expect = false, },						{							args = , expect = false, },					}				},				{					name = "isEqual", params = {"tbl", "other"}, _params = {{"tbl"}, {"other"}}, returns = " if   deep equals  .", cases = { {							args = { { foo = { bar = "baz" } }, { foo = { bar = "baz" } }, },							expect = true, },						{							args = { { foo = { bar = "baz" } }, { foo = { bar = "quux" } }, },							expect = false, },					}				},				{					name = "mapValues", params = {"tbl", "iteratee"}, returns = "Creates a table with the same keys as  and values generated by running each value of   thru iteratee.", cases = { {							snippet = 1, expect = { arg1 = "foo", arg2 = "bar"} }					}				}			},		},		{			heading = "Arrays", doc = { {					name = "keyBy", params = {"tbl", "iteratee"}, _params = {{"iteratee"}, {"tbl"}}, returns = "Creates a table composed of keys generated from the results of running each element of  thru  ", cases = { {							args = {{ {									name = "Link", age = 10, },								{									name = "Zelda", age = 10, },								{									name = "Zelda", age = 17, },							}, "name"}, expect = { ["Link"] = { name = "Link", age = 10 },								["Zelda"] = { name = "Zelda", age = 17, },							}						},						{							snippet = 1, expect = { ["TWW Link"] = { name = "Link", game = "TWW", age = 10, },								["TP Link"] = { name = "Link", game = "TP", age = 17, },							},						},					},				},				{					name = "filter", params = {"tbl", "iteratee"}, _params = {{"iteratee"}, {"tbl"}}, returns = "Iterates over array elements in, returning an array of all elements   returns truthy for.", cases = { {							args = { {									{ game = "The Wind Waker", canon = true }, { game = "Twilight Princess", canon = true }, { game = "Tingle's Rosy Rupeeland", canon = false }, },								"canon", },							expect = { { game = "The Wind Waker", canon = true }, { game = "Twilight Princess", canon = true }, },						},						{							args = { {									{ game = "The Wind Waker", type = "main" }, { game = "Twilight Princess", type = "main" }, { game = "Tingle's Rosy Rupeeland", type = "spinoff" }, },								{ type = "main" }, },							expect = { { game = "The Wind Waker", type = "main" }, { game = "Twilight Princess", type = "main" }, },						},						{							snippet = 1, expect = {"foo", "bar"}, },					},				},				{					name = "map", params = {"tbl", "iteratee"}, _params = {{"iteratee"}, {"tbl"}}, returns = "Creates an array of values by running each array element in  thru  .", cases = { {							args = { {									{										name = "Link", triforce = "Courage", },									{										name = "Zelda", triforce = "Wisdom", },									{										name = "Ganon", triforce = "Power" },								},								"triforce" },							expect = {"Courage", "Wisdom", "Power"}, },					},				},			},		},	}, }

return p