Module:NumberScripts

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Module to assist with the conversion of numbers in different scripts. Used e.g. in sortkeys and necessary for the WLM-bot (Erfgoedbot).

Code

-- Convert between numbers in different scripts
-- Only supports conversion between base 10 scripts where each number is
-- represented by one character
local p = {}

-- list supported scripts with the same number order
local persian_numbers = {"۱","۲","۳","۴","۵","۶","۷","۸","۹","۰"} -- note that order is ltr
local arabic_numbers  = {"١","٢","٣","٤","٥","٦","٧","٨","٩","٠"} -- note that order is rtl
local european_numbers = {"1","2","3","4","5","6","7","8","9","0"}

local convert_char = require('Module:ConvertChar')

function p.convert( text, from_set, to_set )
	return convert_char.new(from_set, to_set):convert(text)
end

function p.persian_to_european( frame )
	return p.convert(frame.args[1], persian_numbers, european_numbers)
end

function p.european_to_persian( frame )
	return p.convert(frame.args[1], european_numbers, persian_numbers)
end

function p.arabic_to_european( frame )
	return p.convert(frame.args[1], arabic_numbers, european_numbers)
end

function p.european_to_arabic( frame )
	return p.convert(frame.args[1], european_numbers, arabic_numbers)
end

return p