Code coverage report for number/abbreviate.js

Statements: 100% (17 / 17)      Branches: 100% (12 / 12)      Functions: 100% (3 / 3)      Lines: 100% (16 / 16)     

All files » number/ » abbreviate.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361   1                 1 67 67 67   67   67 33   33 34 21 21   13     67     1      
define(['./enforcePrecision'], function (enforcePrecision) {
 
    var _defaultDict = {
        thousand : 'K',
        million : 'M',
        billion : 'B'
    };
 
    /**
     * Abbreviate number if bigger than 1000. (eg: 2.5K, 17.5M, 3.4B, ...)
     */
    function abbreviateNumber(val, nDecimals, dict){
        nDecimals = nDecimals != null? nDecimals : 1;
        dict = dict || _defaultDict;
        val = enforcePrecision(val, nDecimals);
 
        var str, mod;
 
        if (val < 1000000) {
            mod = enforcePrecision(val / 1000, nDecimals);
            // might overflow to next scale during rounding
            str = mod < 1000? mod + dict.thousand : 1 + dict.million;
        } else if (val < 1000000000) {
            mod = enforcePrecision(val / 1000000, nDecimals);
            str = mod < 1000? mod + dict.million : 1 + dict.billion;
        } else {
            str = enforcePrecision(val / 1000000000, nDecimals) + dict.billion;
        }
 
        return str;
    }
 
    return abbreviateNumber;
 
});