Module:Ordinal: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Eliora (talk | contribs)
creating a much needed module
 
Remove sup argument (depended on yesno module which we chose not to import)
Line 9: Line 9:


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


--[[
--[[
Line 17: Line 15:
   
   
Usage:
Usage:
{{#invoke:Ordinal|ordinal|1=|2=|sup=}}
{{#invoke:Ordinal|ordinal|1=|2=}}
{{#invoke:Ordinal|ordinal}} - uses the caller's parameters
{{#invoke:Ordinal|ordinal}} - uses the caller's parameters
   
   
Line 23: Line 21:
     1: Any number or string.
     1: Any number or string.
     2: Set to "d" if the module should display "d" instead of "nd" and "rd".
     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)
function p.ordinal(frame)
Line 33: Line 30:
     args[1] = "{{{1}}}"
     args[1] = "{{{1}}}"
     end
     end
     return p._ordinal(args[1], (args[2] == 'd'), yesno(args.sup))
     return p._ordinal(args[1], (args[2] == 'd'))
end
end


function p._ordinal(n, d, sup)
function p._ordinal(n, d)
local x = tonumber(mw.ustring.match(n, "(%d*)%W*$"))
local x = tonumber(mw.ustring.match(n, "(%d*)%W*$"))
local suffix = "th"
local suffix = "th"
Line 50: Line 47:
if d then suffix = "d" else suffix = "rd" end
if d then suffix = "d" else suffix = "rd" end
end
end
end
if sup then
suffix = "<sup>" .. suffix .. "</sup>"
end
end
return n .. suffix
return n .. suffix

Revision as of 14:37, 4 July 2023

Module documentation[view] [edit] [history] [purge]
This module may be invoked by templates using its corresponding template Template:Ordinal, or used directly from other modules.

This module automatically determines the ordinal of a given number (i.e. 1 → first, 2 → second, 3 → third, etc.).

On templates, you can use {{Ordinal}}.

On modules, you can include local ordinal= require("Module:Ordinal")._ordinal.

Introspection summary for Module:Ordinal 
Functions provided (2)
Line Function Params
24 ordinal (invokable) (frame)
35 _ordinal (main) (n, d)
Lua modules required (0)
Variable Module Functions used

No function descriptions were provided. The Lua code may have further information.


--[[  
 
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 = {}

--[[
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=}}
{{#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".
]]
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'))
end

function p._ordinal(n, d)
	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
	return n .. suffix
end

return p