# All files calcMultiplier.js

100% Statements 43/43
100% Branches 20/20
100% Functions 1/1
100% Lines 43/43

    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    722x

    2x

    2x

    2x
    2x

    2x
    33x
    33x

    33x
    18x

    2x
    2x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    2x

    16x

    31x

     var pow = Math.pow

    var DEFAULT_UNIT = 'kib'

    // from kib to bytes
    var TO_BYTES = pow(2, 10)

    var multipliers = {}
    multipliers[DEFAULT_UNIT] = 1

    module.exports = function(aUnit) {
        var unit = (aUnit || DEFAULT_UNIT).toLowerCase()
        var multiplier = TO_BYTES

        if (!multipliers[unit]) {
            switch (unit) {
                case 'mib':
                    multiplier /= pow(2, 20)
                    break
                case 'gib':
                    multiplier /= pow(2, 30)
                    break
                case 'tib':
                    multiplier /= pow(2, 40)
                    break
                case 'pib':
                    multiplier /= pow(2, 50)
                    break
                case 'eib':
                    multiplier /= pow(2, 60)
                    break
                case 'zib':
                    multiplier /= pow(2, 70)
                    break
                case 'yib':
                    multiplier /= pow(2, 80)
                    break
                case 'kb':
                    multiplier /= pow(10, 3)
                    break
                case 'mb':
                    multiplier /= pow(10, 6)
                    break
                case 'gb':
                    multiplier /= pow(10, 9)
                    break
                case 'tb':
                    multiplier /= pow(10, 12)
                    break
                case 'pb':
                    multiplier /= pow(10, 15)
                    break
                case 'eb':
                    multiplier /= pow(10, 18)
                    break
                case 'zb':
                    multiplier /= pow(10, 21)
                    break
                case 'yb':
                    multiplier /= pow(10, 24)
                    break
                // TODO: default clsuse should be removed when `aUnit` is validated before invoking this function
                default:
                    return multipliers[DEFAULT_UNIT]
            }

            multipliers[unit] = multiplier
        }

        return multipliers[unit]
    }