Module:UtilsTable/Documentation/Snippets

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

This module contains snippets used to generate documentation and unit tests for Module:UtilsTable.


local p = {}

local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")

function p.clone1()
	local original = { foo = {} }
	local clone = utilsTable.clone(original)
	return {
		clone,
		original ~= clone, 
		original.foo == clone.foo,
	}
end

function p.cloneDeep1()
	local original = { foo = {} }
	local clone = utilsTable.cloneDeep(original)
	return {
		clone,
		original ~= clone, 
		original.foo ~= clone.foo,
	}
end

function p.concat1()
	return utilsTable.concat({1}, 2, {3, 4}, {{5}}, {6})
end

function p.dropWhile1()
	local games = {"TLoZ", "TAoL", "ALttP", "LA", "OoT"}
	return utilsTable.dropWhile(games, function(game)
		return game ~= "ALttP"
	end)
end

function p.dropWhile2()
	local games = {"TLoZ", "TAoL", "ALttP", "LA", "OoT"}
	return utilsTable.dropWhile(games, function(game)
		return game ~= "MM"
	end)
end

function p.dropRightWhile1()
	local games = {"...", "SS", "ALBW", "TFH", "BotW"}
	return utilsTable.dropRightWhile(games, function(game)
		return game ~= "ALBW"
	end)
end

function p.keyBy1()
	local characters = {
		{
			name = "Link",
			game = "TWW",
			age = 10,
		},
		{
			name = "Link",
			game = "TP",
			age = 17
		},
	}
	return utilsTable.keyBy(characters, function(character)
		return character.game .. " " .. character.name
	end)
end

function p.filter1()
	local args = {"foo", "", "bar"}
	return utilsTable.filter(args, utilsString.notEmpty)
end

function p.find1()
	local args = {"foo", "bar", "baz"}
	return utilsTable.find(args, utilsString._startsWith("b"))
end

function p.find2()
	local args = {"foo", "quux", "quux"}
	return utilsTable.find(args, utilsString._startsWith("b"))
end

function p.findIndex1()
	local args = {"foo", "bar", "baz"}
	return utilsTable.findIndex(args, utilsString._startsWith("b"))
end

function p.findIndex2()
	local args = {"foo", "quux", "quux"}
	return utilsTable.findIndex(args, utilsString._startsWith("b"))
end

function p.findLast1()
	local args = {"foo", "bar", "baz"}
	return utilsTable.findLast(args, utilsString._startsWith("b"))
end

function p.findLast2()
	local args = {"foo", "quux", "quux"}
	return utilsTable.findLast(args, utilsString._startsWith("b"))
end

function p.flatMap1()
	local function duplicate(n)
		return {n, n}
	end
	return utilsTable.flatMap({1, 2}, duplicate)
end

function p._flatMap1()
	local duplicate = utilsTable._flatMap(function(n)
		return {n, n}
	end)
	return duplicate({1, 2})
end

function p.groupBy1()
	return utilsTable.groupBy({6.1, 4.2, 6.3}, math.floor)
end

function p.map1()
	local args = {true, false}
	return utilsTable.map(args, tostring)
end

function p.mapValues1()
	local templateArgs = {
		arg1 = "  foo ",
		arg2 = "bar\n",
	}
	return utilsTable.mapValues(templateArgs, mw.text.trim)
end

function p.merge1()
	local tbl = {
		flib = "flob",
		foo = {
			bar = {"flib", "flob"},
			baz = {"quux", "quuz"},
		},
	}
	utilsTable.merge(tbl, {
		foo = {
			bar = {"noot"},
			baz = "noot",
			wibble = "wobble",
		},
	})
	return tbl
end

function p.partition1()
	local tbl = {"foo", "bar", "baz", "quux"}
	return utilsTable.partition(tbl, function(str)
		return str:sub(1, 1) ~= "b"
	end)
end

function p.sortBy1()
	local games = {
		{
			game = "LA",
			systems = {"GB", "GBC", "3DS VC"}
		},
		{
			game = "OoT",
			systems = {"N64", "GCN", "iQue", "Wii VC", "Wii U VC"},
		},
		{
			game = "TWW",
			systems = {"GCN", "Wii U"},
		},
	}
	return utilsTable.sortBy(games, function(game)
		return #game.systems
	end)
end

function p.takeWhile1()
	local games = {"TLoZ", "TAoL", "ALttP", "LA", "OoT", "MM"}
	local gamesBeforeOoT = utilsTable.takeWhile(games, function(game) 
		return game ~= "OoT"
	end)
	return gamesBeforeOoT
end

function p.uniqueBy1()
	local games = {
		{ title = "LA", game = "LA" },
		{ title = "OoT", game = "OoT" },
		{ title = "LADX", game = "LA" },
		{ title = "OoT3D", game = "OoT" },
		{ title = "LANS", game = "LA" },
	}
	return utilsTable.uniqueBy(games, function(game) return game.game end)
end

return p