Jump to content

Module:Jct/city/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chinissai (talk | contribs) at 13:36, 23 April 2016 (Avoid using string concat with table insert.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local concat = table.concat
local insert = table.insert
local format = mw.ustring.format

-- ON DEPLOY: Change to main module
local statenameModule = "Module:Jct/statename/sandbox"

local state

local function stateName(args)
	local data = mw.loadData(statenameModule) 
	local abbr = args.state or args.province
	return data[args.country][abbr]
end

local function location(args, num)
	local city = args["city" .. num]
	local location = args["location" .. num]
	local areadab = args["areadab" .. num]
	local countydab = args["countydab" .. num]
	
	if location then
		return location
	end
	
	local parts = {}
	
	insert(parts, "[[")
	insert(parts, city)
	if areadab then
		insert(parts, " (")
		insert(parts, areadab)
		insert(parts, ")")
	end
	if countydab then
		insert(parts, ", ")
		insert(parts, countydab)
		insert(parts, " County")
	end
	if state then
		insert(parts, ", ")
		insert(parts, state)
	end
	
	insert(parts, "|")
	insert(parts, city)
	insert(parts, "]]")
	return concat(parts)
end

function p.city(args)
	state = stateName(args)
	local cities = {}
	local locationCount = 1
	while args['city' .. locationCount] or args['location' .. locationCount] do
		insert(cities, location(args, locationCount))
		locationCount = locationCount + 1
	end
	return concat(cities, ', ')
end

return p