Documentation icon မော်ဂျူးမှတ်တမ်းလက်စွဲ[ဖန်တီး]
-- This module converts a number into its written English form.
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".

local getArgs = require('Module: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 = 'ကိုးဆယ့်-%s'},
	{num = 80, rule = 'ရှစ်ဆယ့်-%s'},
	{num = 70, rule = 'ခုနစ်ဆယ့်-%s'},
	{num = 60, rule = 'ခြောက်ဆယ့်-%s'},
	{num = 50, rule = 'ငါးဆယ့်-%s'},
	{num = 40, rule = 'လေးဆယ့်-%s'},
	{num = 30, rule = 'သုံးဆယ့်-%s'},
	{num = 20, rule = 'နှစ်ဆယ့်-%s'},
	{num = 10, rule = '%ဆယ်'}
}

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