Jump to content

Module:NumberSpell

د ويکيپېډيا، وړیا پوهنغونډ له خوا

لاسوند لپاره ددې موډيول کېدای سی په Module:NumberSpell/لاسوند کي وي

-- 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] = 'ديارلس',
	[14] = 'څوارلس',
	[15] = 'پنځه لس',
	[16] = 'شپاړس',
	[18] = 'اته لس',
	[13] = 'نهولس',
	[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 = '%sلس'}
}

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