All files / js/util NumberUtil.js

92.16% Statements 94/102
70.83% Branches 34/48
92.86% Functions 13/14
93.48% Lines 86/92
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 2961x 1x 1x                               1x                             1x 1x                           1x 1x                           1x 1x                             1x 1x                             1x 5x 5x 5x 5x 5x   5x 2x     3x   5x 5x 5x                                       1x 15x 7x   8x                                       1x   6x   6x 6x 6x   2x       4x 4x   4x   6x                                                 1x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x   5x 7x 7x   5x 5x 3x   2x 2x           5x                               1x 2x 2x 2x 2x 2x       2x 2x 4x 4x     2x 2x                               1x 2x 2x 2x 2x 2x       2x 2x 4x 4x     2x 2x   1x   1x 1x    
(function (factory) {
    Eif (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    /**
     * The NumberUtil class has many helper methods to work with number data.
     *
     * @class NumberUtil
     * @module StructureJS
     * @submodule util
     * @author Robert S. (www.codeBelt.com)
     * @static
     */
    var NumberUtil = (function () {
        function NumberUtil() {
            throw new Error('[NumberUtil] Do not instantiate the NumberUtil class because it is a static class.');
        }
        /**
         * Converts bytes into megabytes.
         *
         * @method bytesToMegabytes
         * @param bytes {number}
         * @returns {number}
         * @public
         * @static
         * @example
         *
         */
        NumberUtil.bytesToMegabytes = function (bytes) {
            return bytes / 1048576;
        };
        /**
         * Converts centimeters into inches.
         *
         * @method centimeterToInch
         * @param cm {number}
         * @public
         * @static
         * @returns {number}
         * @example
         *     NumberUtil.centimeterToInch(1);
         *     // 0.3937
         */
        NumberUtil.centimeterToInch = function (cm) {
            return cm * 0.39370;
        };
        /**
         * Converts inches into centimeters.
         *
         * @method inchToCentimeter
         * @param inch {number}
         * @public
         * @static
         * @returns {number}
         * @example
         *     NumberUtil.inchToCentimeter(1);
         *     // 2.54
         */
        NumberUtil.inchToCentimeter = function (inch) {
            return inch * 2.54;
        };
        /**
         * Converts feet into meters.
         *
         * @method feetToMeter
         * @param feet {number}
         * @public
         * @static
         * @returns {number}
         * @example
         *     NumberUtil.feetToMeter(1);
         *     // 0.3048
         *
         */
        NumberUtil.feetToMeter = function (feet) {
            return feet / 3.2808;
        };
        /**
         * Converts seconds into hour, minutes, seconds.
         *
         * @method convertToHHMMSS
         * @param seconds {number}
         * @param showHours [boolean=true] By default if the time does not pass one hour it will show 00:05:34. Pass false to display the time as 05:34 until it gets pass one hour and then it will show 01:00:00
         * @returns {string}
         * @public
         * @static
         * @example
         *     NumberUtil.convertToHHMMSS(33333);
         *     // '09:15:33'
         */
        NumberUtil.convertToHHMMSS = function (seconds, showHours) {
            if (showHours === void 0) { showHours = true; }
            var sec = isNaN(seconds) ? 0 : seconds; //Changes NaN to 0
            var s = sec % 60;
            var m = Math.floor((sec % 3600) / 60);
            var h = Math.floor(sec / (60 * 60));
            var hourStr;
            if (showHours === false) {
                hourStr = (h == 0) ? '' : NumberUtil.doubleDigitFormat(h) + ':';
            }
            else {
                hourStr = NumberUtil.doubleDigitFormat(h) + ':';
            }
            var minuteStr = NumberUtil.doubleDigitFormat(m) + ':';
            var secondsStr = NumberUtil.doubleDigitFormat(s);
            return hourStr + minuteStr + secondsStr;
        };
        /**
         * Formats a number from 0-9 to display with 2 digits.
         *
         * @method doubleDigitFormat
         * @param num {number}
         * @returns {string}
         * @public
         * @static
         * @example
         *     NumberUtil.doubleDigitFormat(0);
         *     // '00'
         *
         *     NumberUtil.doubleDigitFormat(5);
         *     // '05'
         *
         *     NumberUtil.doubleDigitFormat(9);
         *     // '09'
         */
        NumberUtil.doubleDigitFormat = function (num) {
            if (num < 10) {
                return ('0' + num);
            }
            return String(num);
        };
        /**
         * Formats a currency string as a number.
         *
         * @method unformatUnit
         * @param value {string} The string currency that you want converted into a number.
         * @returns {number} Returns the number value of the currency string.
         * @public
         * @static
         * @example
         *     NumberUtil.unformatUnit('$1,234,567.89');
         *     // 1234567.89
         *
         *     NumberUtil.unformatUnit('1.234.567,89 €');
         *     // 1234567.89
         *
         *     NumberUtil.unformatUnit('$-123,456,789.99');
         *     // -123456789.99
         */
        NumberUtil.unformatUnit = function (value) {
            // Removes all characters and spaces except the period (.), comma (,) and the negative symbol (-).
            var withoutSpecialCharacters = value.replace(/[^\d.,-]/g, '');
            // Gets the index where the decimal placement is located.
            var decimalIndex = withoutSpecialCharacters.length - 3;
            var decimalSeparator = withoutSpecialCharacters.charAt(decimalIndex);
            if (decimalSeparator === '.') {
                // Removes all comma (,) characters and leaves the period (.) and the negative symbol (-).
                withoutSpecialCharacters = value.replace(/[^\d.-]/g, '');
            }
            else {
                // Removes all period (.) characters and leaves the comma (,) and the negative symbol (-).
                withoutSpecialCharacters = value.replace(/[^\d,-]/g, '');
                decimalIndex = withoutSpecialCharacters.length - 3;
                //Replaces the comma (,) to a period (.).
                withoutSpecialCharacters = withoutSpecialCharacters.replace(',', '.');
            }
            return parseFloat(withoutSpecialCharacters);
        };
        /**
         * Formats a number as a currency string.
         *
         * @method formatUnit
         * @param value {number} The number value you want formatted.
         * @param [decimalPlacement=2] {number} How many decimal placements you want to show.
         * @param [decimalSeparator='.'] {string} The character you want to use as the thousands separator.
         * @param [thousandsSeparator=','] {string} The character you want to use as the thousands separator.
         * @param [currencySymbol=''] {string} The symbol you would like to add.
         * @param [currencySymbolPlacement=0] {number} The placement of the symbol. Use 0 to place in front or 1 to place at the end.
         * @returns {string} Returns the formatted currency.
         * @public
         * @static
         * @example
         *     NumberUtil.formatUnit(1234567.89, 2, ".", ",", "$", 0);
         *     // '$1,234,567.89'
         *
         *     NumberUtil.formatUnit(12341234.56, 2, "*", ",", " €", 1);
         *     // '12,341,234*56 €'
         *
         *     NumberUtil.formatUnit(-1900.24, 1);
         *     // '-1,900.2'
         */
        NumberUtil.formatUnit = function (value, decimalPlacement, decimalSeparator, thousandsSeparator, currencySymbol, currencySymbolPlacement) {
            Iif (decimalPlacement === void 0) { decimalPlacement = 2; }
            if (decimalSeparator === void 0) { decimalSeparator = '.'; }
            if (thousandsSeparator === void 0) { thousandsSeparator = ','; }
            if (currencySymbol === void 0) { currencySymbol = ''; }
            if (currencySymbolPlacement === void 0) { currencySymbolPlacement = 0; }
            var str = String(Number(value).toFixed(decimalPlacement));
            var result = '';
            if (decimalPlacement != 0) {
                result = str.slice(-1 - decimalPlacement);
                result = result.replace('.', decimalSeparator);
                str = str.slice(0, str.length - 1 - decimalPlacement);
            }
            while (str.length > 3) {
                result = thousandsSeparator + str.slice(-3) + result;
                str = str.slice(0, str.length - 3);
            }
            Eif (str.length > 0) {
                if (currencySymbolPlacement === 0) {
                    result = currencySymbol + str + result;
                }
                else Eif (currencySymbolPlacement === 1) {
                    result = str + result + currencySymbol;
                }
                else {
                    result = str + result;
                }
            }
            return result;
        };
        /**
         * Convert Fahrenheit to Celsius.
         *
         * @method fahrenheitToCelsius
         * @param fahrenheit {number} The fahrenheit value.
         * @param decimals {number} The number of decimals.
         * @return {number}
         * @example
         *      MathUtil.fahrenheitToCelsius(32);
         *      // 0
         *
         *      MathUtil.fahrenheitToCelsius(212);
         *      // 100
         */
        NumberUtil.fahrenheitToCelsius = function (fahrenheit, decimals) {
            Eif (decimals === void 0) { decimals = 2; }
            var d = '';
            var r = (5 / 9) * (fahrenheit - 32);
            var s = r.toString().split('.');
            Iif (s[1] != undefined) {
                d = s[1].substr(0, decimals);
            }
            else {
                var i = decimals;
                while (i > 0) {
                    d += '0';
                    i--;
                }
            }
            var c = s[0] + '.' + d;
            return Number(c);
        };
        /**
         * Convert Celsius to Fahrenheit.
         *
         * @method celsiusToFahrenheit
         * @param celsius {number} The celsius value.
         * @param decimals {number} The number of decimals.
         * @return {number}
         * @example
         *      MathUtil.celsiusToFahrenheit(0);
         *      // 32
         *
         *      MathUtil.celsiusToFahrenheit(100);
         *      // 212
         */
        NumberUtil.celsiusToFahrenheit = function (celsius, decimals) {
            Eif (decimals === void 0) { decimals = 2; }
            var d = '';
            var r = (celsius / (5 / 9)) + 32;
            var s = r.toString().split('.');
            Iif (s[1] != undefined) {
                d = s[1].substr(0, decimals);
            }
            else {
                var i = decimals;
                while (i > 0) {
                    d += '0';
                    i--;
                }
            }
            var f = s[0] + '.' + d;
            return Number(f);
        };
        return NumberUtil;
    }());
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = NumberUtil;
});