Module:Currency short

Documentation for this module may be created at Module:Currency short/doc

-- <pre>
local YesNo = require( 'Module:Yesno' )
local libraryUtil = require( 'libraryUtil' )
local p = {}

local function amount( a, coinType, precise )
    -- convert used globals to locals where possible to improve performance
    local math = math
    local string = string
    local table = table
    local mw = mw

    local ret = { '<span class="coins ', 'inventory-image ', true, '', true, true, '', '', '</span>' }
    -- add class for CSS to add the correct image with
    -- see [[MediaWiki:Common.less/coins.less]] for more details
    local coinClasses = {
        coins = 'coins-',
        rusty = 'rusty-coins-',
        chimes = 'chimes-',
        zemomark = 'zemomark-',
        nocoins = '',
        nocoinsc = ''
    }
    local bins
    if coinType == 'chimes' then
    	bins = {1000, 100, 50, 20, 1}
    else
    	bins = {10000, 1000, 250, 100, 25, 5, 4, 3, 2, 1}
    end

    ret[3] = coinClasses[coinType] or error( "bad argument #2 to 'Module:Currency_short._amount' (unsupported coin type '" .. coinType .. "')", 4 )

    -- strip commas from input
    -- @example {{GEPrice|Foo}} -> '1,000'
    if type( a ) == 'string' then
        a = string.gsub( a, ',', '' )
        local a2 = tonumber( a )
        if a2 == nil then -- only do this if required so as not to impact performance too much
            a = mw.ext.ParserFunctions.expr( a )
            a = tonumber( a ) or 0
        else
               a = a2
        end
    end

    -- select which image class to use for css to hook off
    local num = math.abs( a )
    for i = 1, #bins do
        if a >= bins[i] then
            num = bins[i]
            break
        end
    end
    
    if coinType == 'nocoins' then
        ret[2] = ''
    elseif coinType == 'nocoinsc' then
    	ret[2] = ''
    	ret[8] = ' coins'
    else
    	ret[4] = tostring( num )
    end

    -- set a class to denote positive or negative (css sets the colour)
    if a > 0 then
        ret[5] = ' coins-pos">'
    elseif a < 0 then
        ret[5] = ' coins-neg">'
    else
        ret[5] = '">'
    end

    -- shorten the number k or M
    if precise then
    	-- round to 2 d.p.
    	a = math.floor(a * 100 + 0.5) / 100
    else
        if math.abs( a ) >= 10000000 then
            a = math.floor( a / 100000 + 0.5 ) / 10
            ret[7] = 'M'
        elseif math.abs( a ) >= 100000 then
            a = math.floor( a / 1000 + 0.5 )
            ret[7] = 'k'
        else
            a = math.floor( a + 0.5 )
        end
    end

    -- format number with commas
    ret[6] = mw.language.getContentLanguage():formatNum( a ) -- This is so slow almost nothing else matters

    return table.concat( ret )
end

--
-- {{Coins short}}
--
function p.coins( frame )
    local args = frame:getParent().args
    local a = args[1] or 0
    local pr = YesNo( args[2] ) or false
    return amount( a, 'coins', pr )
end

--
-- {{Rusty coins short}}
--
function p.rusty( frame )
    local args = frame:getParent().args
    local a = args[1] or 0
    local pr = YesNo( args[2] ) or false
    return amount( a, 'rusty', pr )
end

--
-- {{Chimes short}}
--
function p.chimes( frame )
    local args = frame:getParent().args
    local a = args[1] or 0
    local pr = YesNo( args[2] ) or false
    return amount( a, 'chimes', pr )
end

--
-- {{Zemomark short}}
--
function p.zemomark( frame )
    local args = frame:getParent().args
    local a = args[1] or 0
    local pr = YesNo( args[2] ) or false
    return amount( a, 'zemomark', pr )
end

--
-- Module access point
--
function p._amount( a, coinType, precise )
    libraryUtil.checkTypeMulti( 'Module:Currency_short._amount', 1, a, { 'number', 'string' } )
    libraryUtil.checkType( 'Module:Currency_short._amount', 2, coinType, 'string' )
    libraryUtil.checkTypeMulti( 'Module:Currency_short._amount', 3, precise, { 'boolean', 'string', 'nil' } )
    return amount( a, coinType, precise )
end

return p
-- </pre>