Module:Year sequence: Difference between revisions

From stencil.wiki
(Created page with "local p = {} function p.consolidate(frame) local args = frame.args or frame:getParent().args local source = args[1] --[[ local terminus = tonumber(args[2]) local suffix = args[3] ]]-- local a = {} for year in string.gmatch(source, "%d%d%d%d") do table.insert(a, tonumber(year)) end table.sort(a) local result = "" local prev = 0 local range = false for year in a do if year == prev + 1 then --YEAR IS ONE ABOVE PREVIOUS YEAR-- if not range then...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.consolidate(frame)
function p.consolidate(frame)
local args = frame.args or frame:getParent().args
local args = frame.args or frame:getParent().args
local source = args[1]
--[[ local terminus = tonumber(args[2])
local suffix = args[3] ]]--
local a = {}
local a = {}
for year in string.gmatch(source, "%d%d%d%d") do
for year in string.gmatch(args[1], "%d%d%d%d") do
table.insert(a, tonumber(year))
table.insert(a, tonumber(year))
end
end
Line 14: Line 11:
local prev = 0
local prev = 0
local range = false
local range = false
for year in a do
for i = 1, #a do
local year = a[i]
if year == prev + 1 then
if year == prev + 1 then
--[[YEAR IS ONE ABOVE PREVIOUS YEAR]]--
if not range then
if not range then
--[[START A RANGE]]--
result = result .. "–"
result = result .. "–"
range = true
range = true
Line 24: Line 20:
else
else
if range then
if range then
--[[RANGE ENDED WITH PREV]]--
result = result .. prev
result = result .. prev
range = false
range = false
Line 34: Line 29:
end
end
if range then
if range then
--[[RANGE ENDED WITH PREV]]--
result = result .. prev
result = result .. prev
range = false
range = false

Latest revision as of 21:39, 21 December 2024

Documentation for this module may be created at Module:Year sequence/doc

local p = {}

function p.consolidate(frame)
	local args = frame.args or frame:getParent().args
	local a = {}
	for year in string.gmatch(args[1], "%d%d%d%d") do
		table.insert(a, tonumber(year))
	end
	table.sort(a)
	local result = ""
	local prev = 0
	local range = false
	for i = 1, #a do
		local year = a[i]
		if year == prev + 1 then
			if not range then
				result = result .. "–"
				range = true
			end
		else
			if range then
				result = result .. prev
				range = false
			end
			if result ~= "" then result = result .. ", " end
			result = result .. year
		end
		prev = year
	end
	if range then
		result = result .. prev
		range = false
	end
	return result
end

return p