Acciones

Módulo

Diferencia entre revisiones de «Ordinal»

De La Venciclopedia

mSin resumen de edición
mSin resumen de edición
Línea 38: Línea 38:
function p._ordinal(n, d, sup)
function p._ordinal(n, d, sup)
local x = tonumber(mw.ustring.match(n, "(%d*)%W*$"))
local x = tonumber(mw.ustring.match(n, "(%d*)%W*$"))
local suffix = "vo"
local suffix = "th"
-- If tonumber(n) worked:
-- If tonumber(n) worked:
if x then
if x then
Línea 44: Línea 44:
local mod100 = math.abs(x) % 100
local mod100 = math.abs(x) % 100
if    mod10 == 1 and mod100 ~= 11 then
if    mod10 == 1 and mod100 ~= 11 then
suffix = "er"
suffix = "st"
elseif mod10 == 2 and mod100 ~= 12 then
elseif mod10 == 2 and mod100 ~= 12 then
if d then suffix = "d" else suffix = "do" end
if d then suffix = "d" else suffix = "nd" end
elseif mod10 == 3 and mod100 ~= 13 then
elseif mod10 == 3 and mod100 ~= 13 then
if d then suffix = "d" else suffix = "er" end
if d then suffix = "d" else suffix = "rd" end
elseif mod10 == 4 and mod100 ~= 14 then
if d then suffix = "d" else suffix = "to" end
elseif mod10 == 5 and mod100 ~= 15 then
if d then suffix = "d" else suffix = "to" end
elseif mod10 == 6 and mod100 ~= 16 then
if d then suffix = "d" else suffix = "to" end
elseif mod10 == 7 and mod100 ~= 17 then
if d then suffix = "d" else suffix = "mo" end
elseif mod10 == 8 and mod100 ~= 18 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 9 and mod100 ~= 19 then
if d then suffix = "d" else suffix = "no" end
elseif mod10 == 10 and mod100 ~= 20 then
if d then suffix = "d" else suffix = "mo" end
elseif mod10 == 11 and mod100 ~= 21 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 12 and mod100 ~= 22 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 13 and mod100 ~= 23 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 14 and mod100 ~= 24 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 15 and mod100 ~= 25 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 16 and mod100 ~= 26 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 17 and mod100 ~= 27 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 18 and mod100 ~= 28 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 19 and mod100 ~= 29 then
if d then suffix = "d" else suffix = "vo" end
elseif mod10 == 20 and mod100 ~= 30 then
if d then suffix = "d" else suffix = "vo" end
end
end
end
end

Revisión del 14:33 8 oct 2020

La documentación para este módulo puede ser creada en Módulo:Ordinal/doc

--[[  
 
This template will add the appropriate ordinal suffix to a given integer.
 
Please do not modify this code without applying the changes first at
Module:Ordinal/sandbox and testing.
 
]]

local p = {}

local yesno     = require('Module:Yesno') -- boolean value interpretation

--[[
This function converts an integer value into a numeral followed by ordinal indicator.
The output string might contain HTML tags.
 
Usage:
{{#invoke:Ordinal|ordinal|1=|2=|sup=}}
{{#invoke:Ordinal|ordinal}} - uses the caller's parameters
 
Parameters
    1: Any number or string.
    2: Set to "d" if the module should display "d" instead of "nd" and "rd".
    sup: Set to yes/no to toggle superscript ordinal suffix.
]]
function p.ordinal(frame)
	local args = frame.args
    if args[1] == nil then
        args = frame:getParent().args
    end
    if args[1] == nil then
    	args[1] = "{{{1}}}"
    end
    return p._ordinal(args[1], (args[2] == 'd'), yesno(args.sup))
end

function p._ordinal(n, d, sup)
	local x = tonumber(mw.ustring.match(n, "(%d*)%W*$"))
	local suffix = "th"
	-- If tonumber(n) worked:
	if x then
		local mod10 = math.abs(x) % 10
		local mod100 = math.abs(x) % 100
		if     mod10 == 1 and mod100 ~= 11 then
			suffix = "st"
		elseif mod10 == 2 and mod100 ~= 12 then
			if d then suffix = "d" else suffix = "nd" end
		elseif mod10 == 3 and mod100 ~= 13 then
			if d then suffix = "d" else suffix = "rd" end
		end
	end
	if sup then
		suffix = "<sup>" .. suffix .. "</sup>"
	end
	return n .. suffix
end

return p