Module:Ordinal: Difference between revisions

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