Code coverage report for app/utils/number_utils.js

Statements: 100% (46 / 46)      Branches: 100% (48 / 48)      Functions: 100% (6 / 6)      Lines: 100% (45 / 45)      Ignored: none     

All files » app/utils/ » number_utils.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 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    1                                 1                                       102 34   68 3   68 35   68 68 68 68 53 53   68 3   68 68                             171 1   170 170 170 2   168   1   167 93   74 1       73               445 406 406 41   365   26     39         17   17 17   17   17               15 15      
'use strict';
 
;require.register("utils/number_utils", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements. See the NOTICE file distributed with this
   * work for additional information regarding copyright ownership. The ASF
   * licenses this file to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations under
   * the License.
   */
  module.exports = {
 
    /**
     * @const
     */
    BYTES_IN_MB: 1024 * 1024,
 
    /**
     * Convert byte size to other metrics.
     *
     * @param {Number} bytes to convert to string
     * @param {Number} precision Number to adjust precision of return value. Default is 0.
     * @param {String} parseType
     *           JS method name for parse string to number. Default is "parseInt".
     * @param {Number} multiplyBy bytes by this number if given. This is needed
     *          as <code>null * 1024 = 0</null>
     * @remarks The parseType argument can be "parseInt" or "parseFloat".
     * @return {String} Returns converted value with abbreviation.
     */
    bytesToSize: function bytesToSize(bytes, precision, parseType, multiplyBy) {
      if (bytes === null || bytes === undefined) {
        return 'n/a';
      } else {
        if (arguments[2] === undefined) {
          parseType = 'parseInt';
        }
        if (arguments[3] === undefined) {
          multiplyBy = 1;
        }
        var value = bytes * multiplyBy;
        var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
        var posttxt = 0;
        while (value >= 1024) {
          posttxt++;
          value = value / 1024;
        }
        if (value === 0) {
          precision = 0;
        }
        var parsedValue = window[parseType](value);
        return parsedValue.toFixed(precision) + " " + sizes[posttxt];
      }
    },
 
    /**
     * Validates if the given string or number is an integer between the
     * values of min and max (inclusive). The minimum and maximum
     * checks are ignored if their valid is NaN.
     *
     * @method validateInteger
     * @param {string|number} str - input string
     * @param {string|number} [min]
     * @param {string|number} [max]
     */
    validateInteger: function validateInteger(str, min, max) {
      if (str == null || str == undefined || (str + "").trim().length < 1) {
        return Em.I18n.t('number.validate.empty');
      } else {
        str = (str + "").trim();
        var number = parseInt(str);
        if (isNaN(number)) {
          return Em.I18n.t('number.validate.notValidNumber');
        } else {
          if (str.length != (number + "").length) {
            // parseInt("1abc") returns 1 as integer
            return Em.I18n.t('number.validate.notValidNumber');
          }
          if (!isNaN(min) && number < min) {
            return Em.I18n.t('number.validate.lessThanMinimum').format(min);
          }
          if (!isNaN(max) && number > max) {
            return Em.I18n.t('number.validate.moreThanMaximum').format(max);
          }
        }
      }
      return null;
    },
    /**
     * @param {String|Number} cardinality - value to parse
     * @param {Boolean} isMax - return maximum count
     * @return {Number}
     **/
    getCardinalityValue: function getCardinalityValue(cardinality, isMax) {
      if (cardinality) {
        var isOptional = cardinality.toString().split('-').length > 1;
        if (isOptional) {
          return parseInt(cardinality.split('-')[isMax ? 1 : 0]);
        } else {
          if (isMax) return (/^\d+\+/.test(cardinality) || cardinality == 'ALL' ? Infinity : parseInt(cardinality)
          );
          return cardinality == 'ALL' ? Infinity : parseInt(cardinality.toString().replace('+', ''));
        }
      } else {
        return 0;
      }
    },
 
    getFloatDecimals: function getFloatDecimals(number, separator) {
      separator = separator || '.';
 
      var value = '' + number;
      var decimals = value.split(separator);
 
      decimals = decimals[1] ? decimals[1].length : 0;
 
      return decimals;
    },
 
    /**
     * @param n
     * @return {boolean}
     */
    isPositiveNumber: function isPositiveNumber(n) {
      var number = Number(n);
      return !isNaN(number) && isFinite(number) && number > 0;
    }
  };
});