Module:Wq/syl/NumberSpell

Documentation for this module may be created at Module:Wq/syl/NumberSpell/doc

-- ꠟꠝ꠆ꠛꠞꠕꠘꠦ ꠀꠍ꠆ꠔꠣ ꠟꠦꠈꠣꠔ
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".

local getArgs = require('Module:Wn/syl/Arguments').getArgs

local p = {}

local max = 100 -- The maximum number that can be parsed.

local ones = {
	[0] = 'ꠡꠥꠁꠘ꠆ꠘ',
	[1] = 'ꠄꠇ',
	[2] = 'ꠖꠥꠁ',
	[3] = 'ꠔꠤꠘ',
	[4] = 'ꠌꠣꠁꠞ',
	[5] = 'ꠙꠣꠌ',
	[6] = 'ꠍꠄ',
	[7] = 'ꠢꠣꠔ',
	[8] = 'ꠀꠐ',
	[9] = 'ꠘꠄ'
}

local specials = {
	[10] = 'ꠖꠡ',
	[11] = 'ꠄꠉꠣꠞꠅ',
	[12] = 'ꠛꠣꠞꠅ',
	[13] = 'ꠔꠦꠞꠅ',
	[15] = 'ꠙꠘ꠆ꠞꠅ',
	[18] = 'ꠀꠐꠣꠞꠅ',
	[20] = 'ꠛꠤꠡ',
	[30] = 'ꠔꠤꠞꠤꠡ',
	[40] = 'ꠌꠟ꠆ꠟꠤꠡ',
	[50] = 'ꠙꠘ꠆ꠌꠣꠁꠡ',
	[60] = 'ꠡꠣꠁꠐ',
	[70] = 'ꠡꠔ꠆ꠔꠁꠞ',
	[80] = 'ꠀꠡꠤ',
	[90] = 'ꠘꠛ꠆ꠛꠁ',
	[100] = 'ꠄꠇ ꠡ'
}

local formatRules = {
	{num = 90, rule = 'ninety-%s'},
	{num = 80, rule = 'eighty-%s'},
	{num = 70, rule = 'seventy-%s'},
	{num = 60, rule = 'sixty-%s'},
	{num = 50, rule = 'fifty-%s'},
	{num = 40, rule = 'forty-%s'},
	{num = 30, rule = 'thirty-%s'},
	{num = 20, rule = 'twenty-%s'},
	{num = 10, rule = '%steen'}
}

function p.main(frame)
	local args = getArgs(frame)
	local num = tonumber(args[1])
	local success, result = pcall(p._main, num)
	if success then
		return result
	else
		return string.format('<strong class="error">Error: %s</strong>', result) -- "result" is the error message.
	end
	return p._main(num)
end

function p._main(num)
	if type(num) ~= 'number' or math.floor(num) ~= num or num < 0 or num > max then
		error('input must be an integer between 0 and ' .. tostring(max), 2)
	end
	-- Check for numbers from 0 to 9.
	local onesVal = ones[num]
	if onesVal then
		return onesVal
	end
	-- Check for special numbers.
	local specialVal = specials[num]
	if specialVal then
		return specialVal
	end
	-- Construct the number from its format rule.
	onesVal = ones[num % 10]
	if not onesVal then
		error('Unexpected error parsing input ' .. tostring(num))
	end
	for i, t in ipairs(formatRules) do
		if num >= t.num then
			return string.format(t.rule, onesVal)
		end
	end
	error('No format rule found for input ' .. tostring(num))
end

return p