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.hasArray(tbl) if type(tbl) ~= "table" then return false end return p.size(tbl) == 0 or table.maxn(tbl) > 0 end

function p.invert(tbl) local inverted = {} for k, v in pairs(tbl) do		inverted[v] = k	end return inverted 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.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(array) local result = {} local j = 1 for i = 1, table.maxn(array) do		if array[i] then result[j] = array[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(array) return function(other) local result = {} for i, v in ipairs(array) do			if not p.find(other, v) then table.insert(result, v)			end end return result end end

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

function p.find(array, iteratee) return p._find(iteratee)(array) end function p._find(iteratee) iteratee = h.iteratee(iteratee) return function(array) for i, v in ipairs(array) 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(array) local i, v = p.find(p.reverse(array), iteratee) if not i then return nil, nil end return #array - i + 1, v	end end

function p.flatten(array) local result = {} for i = 1, table.maxn(array) do		result = p.concat(result, array[i]) end return result end

function p.flattenDeep(array) local result = {} for _, v in ipairs(array) 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(array, mappingFunction) return p._flatMap(mappingFunction)(array) end function p._flatMap(mappingFunction) return function(array) return p.flatten(p.map(array, mappingFunction)) end end

function p.groupBy(key) return function (array) local result = {} for _, v in ipairs(array) 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.includes(array, value) for i = 1, table.maxn(array) do		if array[i] == value then return true end end return false end

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

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

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

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

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

function p.padNils(padValue, max) return function(array) padValue = padValue or '' max = max or table.maxn(array) local result = {} for i = 1, max do			if array[i] == nil then result[i] = padValue else result[i] = array[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(array) local tbl2 = {} local len = #array for i = len, 1, -1 do		tbl2[len - i + 1] = array[i] end return tbl2 end

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

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

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

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

function p.unique(array) local result = {} for _, v in ipairs(array) 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(array) local result = {} for _, v in ipairs(array) 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(array, emptyValue) local result = {} for outerk, innerTbl in ipairs(array) 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, #array)(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 = "invert", params = {"tbl"}, returns = "A table with the values of  as its keys, and vice-versa.", cases = { {							args = , expect = { foo = 1, bar = 2, quux = "baz" },						},						{							desc = "Values will be overwritten if  has duplicate values. Overwrite order is not guaranteeed.", args = , expect = { Power = "Ganondorf", Courage = "Link", Wisdom = "Zelda", },						},					},				},				{					name = "keyOf", params = {"tbl", "value"}, returns = "First key found whose value is shallow-equal to, or nil if none found.", cases = { outputOnly = true, {							args = {{"foo", nil, "bar"}, "bar"}, expect = 3, },						{							args = {{foo = "bar", baz = "quux"}, "quux"}, expect = "baz", },						{							args = {{"foo", "bar"}, "quux"}, expect = nil },						{							desc = "Does not perform deep-equal checks on tables.", args = {{{}, {}}, {}}, expect = nil },					},				},				{					name = "keys", desc = "See also and .", params = {"tbl"}, returns = "Array of  keys.", cases = { {							args = , expect = {1, 2, 3}, },						{							args = , expect = {"foo", "baz"}, },						{							args = , expect = {1, 3, "bar"} },					},				},				{					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 = {"array", "iteratee"}, _params = {{"iteratee"}, {"array"}}, 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 = {"array", "iteratee"}, _params = {{"iteratee"}, {"array"}}, 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 = "includes", params = {"array", "value"}, returns = "True if and only if  is in  .", cases = { {							args = {{"foo", "bar"}, "foo"}, expect = true },						{							args = {{"foo", "bar"}, "baz"}, expect = false },					},				},				{					name = "map", params = {"array", "iteratee"}, _params = {{"iteratee"}, {"array"}}, 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