Call of Duty Esports Wiki
[checked revision][checked revision]
(Syncing content across wikis, if something seems broken as a result let me know)
 
No edit summary
Line 2: Line 2:
 
local util_math = require('Module:MathUtil')
 
local util_math = require('Module:MathUtil')
 
local util_text = require('Module:TextUtil')
 
local util_text = require('Module:TextUtil')
local LOOKUP = mw.loadData('Module:Currencynames')
 
 
local lang = mw.getLanguage('en')
 
local lang = mw.getLanguage('en')
   
 
local h = {}
 
local h = {}
  +
local p = require('Module:EntityAbstract'):extends()
  +
p.objectType = 'Currency'
   
function h.getInfo(currency, settings)
+
function p:long(amount)
if currency == '' or not currency then return end
+
if self.is_nil or not amount then return end
 
if tonumber(amount) == 1 then
local data = util_args.lookupVars(currency, LOOKUP, true)
 
 
return self.vars.singular:format(util_math.formatNumber(amount))
return data
 
 
end
 
return self.vars.plural:format(util_math.formatNumber(amount))
 
end
  +
 
function p:short(amount)
 
if self.is_nil or not amount then return end
 
local unpadded = self.vars.short:format(util_math.formatNumber(amount))
 
return h.padDecimal(unpadded, self.vars.decimals)
 
end
 
end
   
 
function h.padDecimal(str, places)
 
function h.padDecimal(str, places)
 
if not places then return str end
 
local tbl = util_text.split(str,'.',true)
 
local tbl = util_text.split(str,'.',true)
 
if tbl[2] then
 
if tbl[2] then
Line 26: Line 36:
 
end
 
end
 
return str
 
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
 
end
   

Revision as of 17:29, 22 August 2020

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 lang = mw.getLanguage('en')

local h = {}
local p = require('Module:EntityAbstract'):extends()
p.objectType = 'Currency'

function p:long(amount)
	if self.is_nil or not amount then return end
	if tonumber(amount) == 1 then
		return self.vars.singular:format(util_math.formatNumber(amount))
	end
	return self.vars.plural:format(util_math.formatNumber(amount))
end

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

function h.padDecimal(str, places)
	if not places then return str end
	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

return p