มอดูล:ConvertChar

จาก วิกิซอร์ซ

Example[แก้ไข]

  1. {{#invoke:ConvertChar|main|test321test987|0123456789|๐๑๒๓๔๕๖๗๘๙}} --> test๓๒๑test๙๘๗

p = {}

function p.new(from, to)
	local self = { map = {} }
	--for i = 1, mw.ustring.len(from) do
	--	self.map[mw.ustring.sub(from, i, i)] = mw.ustring.sub(to, i, i)
	--end
	if type(from) ~= 'table' then from = mw.text.split(from, '', true) end
	if type(to) ~= 'table' then to = mw.text.split(to, '', true) end
	for i = 1, #from do
		self.map[from[i]] = to[i]
	end
	function self:convert(input)
		local result = ""
		for i = 1, mw.ustring.len(input) do
			local c = mw.ustring.sub(input, i, i)
			result = result .. (self.map[c] or c)
		end
		return result
	end
	return self
end

function p.main(frame, ...)
	local args =
		type(frame) ~= 'table' and {frame, ...} or
		type(frame.args) ~= 'table' and frame or
		frame.args[1] and frame.args or
		frame:getParent().args
	return p.new(args[2] or "", args[3] or ""):convert(args[1] or "")
end

return p