Module:Currency

From Call of Duty Esports Wiki
Revision as of 02:49, 24 June 2019 by RheingoldRiver (talk | contribs) (Syncing content across wikis, if something seems broken as a result let me know)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_args = require('Module:ArgsUtil')
local util_math = require('Module:MathUtil')
local util_text = require('Module:TextUtil')
local LOOKUP = mw.loadData('Module:Currencynames')
local lang = mw.getLanguage('en')

local h = {}

function h.getInfo(currency, settings)
	if currency == '' or not currency then return end
	local data = util_args.lookupVars(currency, LOOKUP, true)
	return data
end

function h.padDecimal(str, places)
	local tbl = util_text.split(str,'.',true)
	if tbl[2] then
		local zeroes = {}
		local i = #tbl[2]
		while i < places do
			zeroes[#zeroes+1] = 0
			i = i + 1
		end
		tbl[2] = tbl[2] .. table.concat(zeroes,'')
		return table.concat(tbl,'.')
	end
	return str
end

local p = {}
function p.main(frame)
	local args = util_args.overwrite(true)
	local style = args[3] or 'short'
	return p[style](args[1], args[2], args)
end

function p.singular(amount, currency, settings)
	local vars = h.getInfo(currency, settings)
	if not vars or not amount then return end
	return vars.singular:format(util_math.formatNumber(amount))
end

function p.plural(amount, currency, settings)
	local vars = h.getInfo(currency, settings)
	if not vars or not amount then return end
	return vars.plural:format(util_math.formatNumber(amount))
end

function p.long(amount, currency, settings)
	local vars = h.getInfo(currency, settings)
	if not vars or not amount then return end
	if tonumber(amount) == 1 then
		return vars.singular:format(util_math.formatNumber(amount))
	else
		return vars.plural:format(util_math.formatNumber(amount))
	end
end

function p.short(amount, currency, settings)
	local vars = h.getInfo(currency, settings)
	if not vars or not amount then return end
	local unpadded = vars.short:format(util_math.formatNumber(amount))
	if vars.decimals then
		return h.padDecimal(unpadded, vars.decimals)
	else
		return unpadded
	end
end

return p