The wiki has moved!

Visit the new wiki at stationeers-wiki.com The old wiki here at legacy.stationeers-wiki.com will sunset eventually.

Edits made after the 7th of March 6PM EST were NOT carried over to the new server as previously announced right here in this box.

 Actions

Module

Difference between revisions of "Parameters"

From Unofficial Stationeers Wiki

(Fix disparate parameters logic)
(Doc some of the module's inner workings; add sorting to strMap and sort compare's output)
Line 20: Line 20:
 
end
 
end
  
local function strMap(iter, transform)
+
local function strMap(iter, transform, sort)
 
local returnTable = {}
 
local returnTable = {}
 
for _, v in pairs(iter) do
 
for _, v in pairs(iter) do
 
table.insert(returnTable, transform(v))
 
table.insert(returnTable, transform(v))
 +
end
 +
if sort then
 +
table.sort(returnTable)
 
end
 
end
 
return table.concat(returnTable)
 
return table.concat(returnTable)
Line 40: Line 43:
 
local function normaliseParams(parameters)
 
local function normaliseParams(parameters)
 
local paramsNorm = {}
 
local paramsNorm = {}
 +
-- Prepare a key lookup metatable, which will hold the original
 +
-- parameter names for each normalised parameter, e.g.
 +
-- [test] = {TEST, Test}. paramIndex functions like a Python
 +
-- defaultdict, where the default is a table.
 
local paramIndex = setmetatable({}, {__index = function(t, k)
 
local paramIndex = setmetatable({}, {__index = function(t, k)
 
if not rawget(t, k) then
 
if not rawget(t, k) then
Line 54: Line 61:
  
 
paramsNorm = removeDuplicates(paramsNorm)
 
paramsNorm = removeDuplicates(paramsNorm)
 +
-- Overload key lookup in paramsNorm. While [[Module:Set]] will
 +
-- operate on the table (which is to say, the normalised parameters
 +
-- array), key access will be by way of the paramIndex metatable.
 
setmetatable(paramsNorm, {__index = paramIndex})
 
setmetatable(paramsNorm, {__index = paramIndex})
 
return paramsNorm
 
return paramsNorm
Line 71: Line 81:
 
%s]],
 
%s]],
 
strMap(Set.valueIntersection(baseParams, otherParams),
 
strMap(Set.valueIntersection(baseParams, otherParams),
function(v) return string.format('* %s\n', v) end),
+
function(v) return string.format('* %s\n', v) end, true),
 
strMap(Set.valueIntersection(baseNormParams, otherNormParams),
 
strMap(Set.valueIntersection(baseNormParams, otherNormParams),
 
function(v) return string.format('* %s < %s [%s]\n',
 
function(v) return string.format('* %s < %s [%s]\n',
Line 77: Line 87:
 
table.concat(otherNormParams[v], '; '),
 
table.concat(otherNormParams[v], '; '),
 
v)
 
v)
end),
+
end, true),
 
strMap(Set.valueComplement(otherNormParams, baseNormParams),
 
strMap(Set.valueComplement(otherNormParams, baseNormParams),
 
function(v) return strMap(baseNormParams[v],
 
function(v) return strMap(baseNormParams[v],
function(s) return string.format('* %s\n', s) end)
+
function(s) return string.format('* %s\n', s) end, true)
end))
+
end, true))
 
end
 
end
  

Revision as of 07:33, 8 June 2015


-- This module implements [[Template:Parameters]].
-- [SublimeLinter luacheck-globals:mw]

local p = {}
local removeDuplicates = require('Module:TableTools').removeDuplicates

local function makeInvokeFunction(funcName)
	return function(frame)
		local getArgs = require('Module:Arguments').getArgs
		return p[funcName](getArgs(frame))
	end
end

local function extractParams(source)
	local parameters = {}
	for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
		table.insert(parameters, parameter)
	end
	return removeDuplicates(parameters)
end

local function strMap(iter, transform, sort)
	local returnTable = {}
	for _, v in pairs(iter) do
		table.insert(returnTable, transform(v))
	end
	if sort then
		table.sort(returnTable)
	end
	return table.concat(returnTable)
end

function p._code(args)
	local title = args._base or mw.title.getCurrentTitle().baseText
	return string.format([[{{%s
%s}}]], title, strMap(extractParams(args.base),
		function(s) return string.format('| %s = \n', s) end))
end

function p._compare(args)
	local Set = require('Module:Set')

	local function normaliseParams(parameters)
		local paramsNorm = {}
		-- Prepare a key lookup metatable, which will hold the original
		-- parameter names for each normalised parameter, e.g.
		-- [test] = {TEST, Test}. paramIndex functions like a Python
		-- defaultdict, where the default is a table.
		local paramIndex = setmetatable({}, {__index = function(t, k)
			if not rawget(t, k) then
				rawset(t, k, {})
			end
			return rawget(t, k)
		end})
		for _, parameter in pairs(parameters) do
			table.insert(paramsNorm,
				string.lower(string.gsub(parameter, '%A', '')))
			table.insert(paramIndex[
				string.lower(string.gsub(parameter, '%A', ''))], parameter)
		end

		paramsNorm = removeDuplicates(paramsNorm)
		-- Overload key lookup in paramsNorm. While [[Module:Set]] will
		-- operate on the table (which is to say, the normalised parameters
		-- array), key access will be by way of the paramIndex metatable.
		setmetatable(paramsNorm, {__index = paramIndex})
		return paramsNorm
	end

	local baseParams = extractParams(args.base)
	local otherParams = extractParams(args.other)
	local baseNormParams = normaliseParams(Set.valueComplement(
			otherParams, baseParams))
	local otherNormParams = normaliseParams(otherParams)

	return string.format([[Identical:
%s
Similar:
%s
Disparate:
%s]],
	strMap(Set.valueIntersection(baseParams, otherParams),
		function(v) return string.format('* %s\n', v) end, true),
	strMap(Set.valueIntersection(baseNormParams, otherNormParams),
		function(v) return string.format('* %s < %s [%s]\n',
			table.concat(baseNormParams[v], '; '),
			table.concat(otherNormParams[v], '; '),
			v)
		end, true),
	strMap(Set.valueComplement(otherNormParams, baseNormParams),
		function(v) return strMap(baseNormParams[v],
			function(s) return string.format('* %s\n', s) end, true)
		end, true))
end

function p._demo(args)
	local title = args._base and ('|_template=' .. args._base) or ''
	return string.format('{{Parameter names example%s|%s}}', title,
		table.concat(extractParams(args.base), '|'))
end

function p._list(args)
	return strMap(extractParams(args.base),
		function(s) return string.format('* %s\n', s) end)
end

p.code = makeInvokeFunction('_code')
p.compare = makeInvokeFunction('_compare')
p.demo = makeInvokeFunction('_demo')
p.list = makeInvokeFunction('_list')

return p