Jump to content

Module:Infobox/utilities

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 11:36, 23 July 2020 (+distribution_sort() & occupation_sort();). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;


--[[--------------------------< C O M P >----------------------------------------------------------------------

compare function for result{} table descending sort

]]

local function comp (a, b)
	return tonumber (a[1]) > tonumber (b[1]);
end
	

--[[--------------------------< S O R T _ C O M M O N >--------------------------------------------------------

common function to render sorted distribution, ethnicity, and occupation lists.

inputs:
	result - table of percentages and labels
	ref - value from |distribution ref=, |ethnicity ref=, or |occupation ref= as appropriate
	frame - calling frame required for expandTemplate()
	
returns sorted list on success; empty string else

]]

local function sort_common (result, ref, frame)
	for i=#result, 1, -1 do
		if not tonumber (result[i][1]) then										-- if cannot be converted to a number
			table.remove (result, i);											-- delete
		end
	end
	
	if 0 == #result then														-- if we get here and the result table is empty
		return '';																-- abandon returning empty string
	end
	
	table.sort (result, comp);													-- sort what remains

	for i, v in ipairs (result) do
		result[i] = table.concat (result[i]);									-- make each table in result{} a string
	end

	result[1] = table.concat ({result[1], ref and ref or ''});					-- add reference(s) from |<list> ref= to first item in the list
	
	return frame:expandTemplate { title = 'Unbulleted list', args = result};	-- render the unbulleted list
end


--[[--------------------------< D I S R I B U T I O N _ S O R T >----------------------------------------------

{{#invoke:Infobox/utilities|distribution_sort|{{{percent urban|}}}|{{{percent rural|}}}|{{{distribution ref|}}} }}

]]

local function distribution_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in result[n][1]
		{args[1], '% urban'},
		{args[2], '% rural'},
	};
	
	return sort_common (result, args[#result+1], frame);
end


--[[--------------------------< E T H N I C I T Y _ S O R T >--------------------------------------------------

{{#invoke:Infobox/utilities|ethnicity_sort|{{{percent white|}}}|{{{percent black|}}}|{{{percent asian|}}}|{{{percent hispanic|}}}|{{{percent native american|}}}|{{{percent native hawaiian|}}}|{{{percent more than one race|}}}|{{{percent other race|}}}|{{{ethnicity ref|}}} }}

]]

local function ethnicity_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in result[n][1]
		{args[1], '% [[White people|white]]'},
		{args[2], '% [[Black people|black]]'},
		{args[3], '% [[Asian Americans|Asian]]'},
		{args[4], '% [[Hispanic]]'},
		{args[5], '% [[Native Americans in the United States|Native American]]'},
		{args[6], '% [[Pacific Islands Americans]]'},
		{args[7], '% [[Two or more races]]'},
		{args[8], '% other'},													-- TODO: make other always last?
	};
	
	return sort_common (result, args[#result+1], frame);
end


--[[--------------------------< O C C U P A T I O N _ S O R T >------------------------------------------------

{{#invoke:Infobox/utilities|distribution_sort|{{{percent blue collar|}}}|{{{percent white collar|}}}|{{{percent grey collar|}}}|{{{occupation ref|}}} }}

]]

local function occupation_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in result[n][1]
		{args[1], '% [[Blue-collar worker|Blue-collar]]'},
		{args[2], '% [[White-collar worker|White-collar]]'},
		{args[3], '% [[Gray-collar]]'},
	};
	
	return sort_common (result, args[#result+1], frame)
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------

]]

return {
	distribution_sort = distribution_sort,
	ethnicity_sort = ethnicity_sort,
	occupation_sort = occupation_sort,
	}