มอดูล:th-utilities

จาก วิกิพจนานุกรม พจนานุกรมเสรี
local export = {}

local gsub = mw.ustring.gsub
local find = mw.ustring.find
local match = mw.ustring.match
local thai_digits = {"๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"}
local thai_words = {"ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"}
local thai_words2 = {["."]="จุด",["-"]="ขีด",["+"]="บวก",["−"]="ลบ",["/"]="ทับ"}

function export.arabic_digit_to_thai(text)
	if type(text) == "number" then
		text = tostring(text) -- convert to string
	end
	if type(text) == "string" and find(text, "[0-9]") then
		for n = 0, 9 do
			text = gsub(text, tostring(n), thai_digits[n + 1])
		end
	end
	return text
end

function export.thai_digit_to_arabic(text)
	if type(text) == "string" and find(text, "[๐-๙]") then
		for n = 0, 9 do
			text = gsub(text, thai_digits[n + 1], tostring(n))
		end
	end
	return text
end

function export.thai_number_sequence(text)
	if type(text) == "number" then
		text = tostring(text) -- convert to string
	end
	if type(text) == "string" then
		text = export.thai_digit_to_arabic(text)
		for n = 0, 9 do
			text = gsub(text, tostring(n), thai_words[n + 1])
		end
		text = gsub(text, ".", thai_words2)
	end
	return text
end

--([\-+]?)([0-9,]*)((\.[0-9]+)?)

function export.thai_number_integer(text)
	if type(text) == "number" then
		text = tostring(text) -- convert to string
	end
	if type(text) == "string" and match(text, "^[-%+−]?[0-9๐-๙,]+$") then
		text = export.thai_digit_to_arabic(text)
		text = gsub(text, ",", "")
		local len = text:len() -- now only Arabic digits
		--TODO
	end
	return text
end

function export.thai_number_float(text)
	if type(text) == "number" then
		-- floating-point may be not accurate due to binary system
		text = tostring(text) -- convert to string
	end
	if type(text) == "string" then
		--TODO
	end
	return text
end

return export