Module:Sequences

local p = {} local h = {}

local utilsArg = require('Module:UtilsArg') local utilsCargo = require('Module:UtilsCargo') local utilsTable = require('Module:UtilsTable')

local cargoTable = "UniverseOrder" -- "Order" can't be used because it's an SQL keyword

function p.CargoDeclare(frame) return utilsCargo.declare(cargoTable, {		media = "String",		universeOrder = "String",		name = "String",		ordinal = "Integer",	}) end

function p.CargoStore(frame) local args = frame:getParent.args return h.store({		media = args.media or mw.title.getCurrentTitle.subpageText,		order = args[1],		names = utilsTable.slice(2)(args),	}) end

--@return a function that sorts @names according to the given @order for the given @media defined at `Data:Orders` function p.sort(media, order) utilsArg.nonEmpty(media, "media") utilsArg.nonEmpty(order, "order") -- @return sorted names, minus those that were not defined for the given sort order -- @return names not defined for the given sort order return function(names) local fields = "name" local queryResults = utilsCargo.query(cargoTable, fields, {			where= ("media='%s' AND universeOrder='%s'"):format(media, order),			orderBy= "ordinal",		}) local validNames = utilsTable.map("name")(queryResults) local sorted = utilsTable.intersection(validNames)(names) local unsorted = utilsTable.difference(names)(sorted) return sorted, unsorted end end

function h.store(args) local validator = utilsArg.validator({		media = { nonEmpty = true },		order = { nonEmpty = true },		names = { nonEmpty = true },	}) local err = validator(args) if err then return utilsMarkup.categories(err) end for i, v in ipairs(args.names) do		utilsCargo.store(cargoTable, {			media = args.media,			universeOrder = args.order,			ordinal = i,			name = v,		}) end end

p.Documentation = { description = "This module is used for storing data at Special:CargoTables/" .. cargoTable .. ", and leveraging that data to sort strings according to some in-universe order. See Template:Sort Value for further documentation on usage.", {		name = "sort", order = 2, params = { {				{					name = "media", description = "A string representing a game or other published media." },				{					name = "order", description = "A string identifiying some sort order." },			},			{				{					name = "names", description = "An array of strings to sort according to the given sort order." }			},		},		returns = { "The array of names, sorted according to the given order for the given media, minus those that aren't defined for that sort order.", "An array of the names that were not sorted." },		cases = { {				args = { { "ST", "Realm" }, { {"Fire Realm", "Sand Realm", "💩Realm", "Ocean Realm", "Forest Realm", "Fire Realm" } }, },				expected = { { "Forest Realm", "Ocean Realm", "Fire Realm", "Sand Realm" }, { "💩Realm" }, },			},		},	} }

return p