Code coverage report for js/util/MathUtil.js

Statements: 74.04% (77 / 104)      Branches: 51.56% (33 / 64)      Functions: 84.21% (16 / 19)      Lines: 75.53% (71 / 94)      Ignored: none     

All files » js/util/ » MathUtil.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 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 3401 1 1                             1 1                                         1 3 3 3 1   2 1   1                         1                                             1 1 1 1     1     1                             1 1                                 1 1 1 1 1     1     1 1 1 1                                 1 2                             1 2                             1 2 1   1                             1 2                             1 2                             1 2 2 2                             1 2 2 2                             1 2 1   1 1 1         1                             1 2     2 2 2 12 12   2                     1                       1   1 1    
(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) {
    /**
     * A helper class to do calculations and conversions.
     *
     * @class MathUtil
     * @module StructureJS
     * @submodule util
     * @author Robert S. (www.codeBelt.com)
     * @static
     */
    var MathUtil = (function () {
        function MathUtil() {
            throw new Error('[MathUtil] Do not instantiate the MathUtil class because it is a static class.');
        }
        /**
         * Returns a number constrained between min and max.
         *
         * @method constrain
         * @param num {number}
         * @param min {number}
         * @param  max {number}
         * @return  {number}
         * @example
         *      MathUtil.constrain(12, 3, 20);
         *      // 12
         *
         *      MathUtil.constrain(22, 3, 20);
         *      // 20
         *
         *      MathUtil.constrain(0, 3, 20);
         *      // 3
         */
        MathUtil.constrain = function (num, min, max) {
            Iif (min === void 0) { min = 0; }
            Iif (max === void 0) { max = 1; }
            if (num < min) {
                return min;
            }
            if (num > max) {
                return max;
            }
            return num;
        };
        /**
         * Returns a random number between min and max.
         *
         * @method randomRange
         * @param min {number}
         * @param max {number}
         * @param [wholeNumber=true] {number}
         * @return {number}
         * @example
         *
         */
        MathUtil.randomRange = function (min, max, wholeNumber) {
            if (wholeNumber === void 0) { wholeNumber = true; }
            var num = (min + Math.random() * (max - min));
            if (wholeNumber) {
                return Math.round(num);
            }
            return num;
        };
        /**
         * Returns the percentage of a number in a given range.
         * Example: num = 15 range 10 to 20 // outputs 0.5
         *
         * @method rangeToPercent
         * @param num {number}
         * @param min {number}
         * @param max {number}
         * @param constrainMin {boolean}        Returns 0 if num < min.
         * @param constrainMax {boolean}        Returns 1 if num > max.
         * @return {number}
         * @example
         *      MathUtil.rangeToPercent(15, 10, 20);
         *      // 0.5
         */
        MathUtil.rangeToPercent = function (num, min, max, constrainMin, constrainMax) {
            Eif (constrainMin === void 0) { constrainMin = false; }
            Eif (constrainMax === void 0) { constrainMax = false; }
            Iif (constrainMin && num < min) {
                return 0;
            }
            Iif (constrainMax && num > max) {
                return 1;
            }
            return (num - min) / (max - min);
        };
        /**
         * Returns the number that corresponds to the percentage in a given range.
         * Example: percent = 0.5 range 10 to 20 // outputs 15
         *
         * @method percentToRange
         * @param percent {number}
         * @param min {number}
         * @param max {number}
         * @return {number}
         * @example
         *      MathUtil.percentToRange(0.5, 10, 20);
         *      // 15
         */
        MathUtil.percentToRange = function (percent, min, max) {
            return (percent * (max - min)) + min;
        };
        /**
         * Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() numbero percentToRange().
         * Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5
         *
         * @method map
         * @param num {number}
         * @param min1 {number}
         * @param max1 {number}
         * @param min2 {number}
         * @param max2 {number}
         * @return {number}
         * @example
         *      MathUtil.map(10, 0, 100, 0, 50);
         *      // 5
         */
        MathUtil.map = function (num, min1, max1, min2, max2, round, constrainMin, constrainMax) {
            Eif (round === void 0) { round = true; }
            Eif (constrainMin === void 0) { constrainMin = true; }
            Eif (constrainMax === void 0) { constrainMax = true; }
            Iif (constrainMin && num < min1) {
                return min2;
            }
            Iif (constrainMax && num > max1) {
                return max2;
            }
            var num1 = (num - min1) / (max1 - min1);
            var num2 = (num1 * (max2 - min2)) + min2;
            Eif (round) {
                return Math.round(num2);
            }
            return num2;
        };
        /**
         * Converts radians to degrees.
         *
         * @method radiansToDegrees
         * @param radians {number}
         * @return {number}
         * @example
         *      MathUtil.radiansToDegrees(1.5707963267948966);
         *      // 90
         *
         *      MathUtil.radiansToDegrees(3.141592653589793);
         *      // 180
         */
        MathUtil.radiansToDegrees = function (radians) {
            return radians * (180 / Math.PI);
        };
        /**
         * Converts degrees to radians.
         *
         * @method degreesToRadians
         * @param degrees {number}
         * @return {number}
         * @example
         *      MathUtil.degreesToRadians(90);
         *      // 1.5707963267948966
         *
         *      MathUtil.degreesToRadians(180);
         *      // 3.141592653589793
         */
        MathUtil.degreesToRadians = function (degrees) {
            return (degrees * Math.PI / 180);
        };
        /**
         * Returns 1 if the value is >= 0. Returns -1 if the value is < 0.
         *
         * @method sign
         * @param num {number}
         * @return {number}
         * @example
         *      MathUtil.sign(23);
         *      // 1
         *
         *      MathUtil.sign(-23);
         *      // -1
         */
        MathUtil.sign = function (num) {
            if (num < 0) {
                return -1;
            }
            return 1;
        };
        /**
         * Check if number is positive (zero is positive).
         *
         * @method isPositive
         * @param num {number} The number.
         * @return {boolean}
         * @example
         *      MathUtil.isPositive(23);
         *      // true
         *
         *      MathUtil.isPositive(-23);
         *      // false
         */
        MathUtil.isPositive = function (num) {
            return (num >= 0);
        };
        /**
         * Check if number is negative.
         *
         * @method isNegative
         * @param num {number} The
         * @return {boolean}
         * @example
         *      MathUtil.isNegative(23);
         *      // false
         *
         *      MathUtil.isNegative(-23);
         *      // true
         */
        MathUtil.isNegative = function (num) {
            return (num < 0);
        };
        /**
         * Check if number is odd (convert to Integer if necessary).
         *
         * @method isOdd
         * @param num {number} The number.
         * @return {boolean}
         * @example
         *      MathUtil.isOdd(2);
         *      // false
         *
         *      MathUtil.isOdd(3);
         *      // true
         */
        MathUtil.isOdd = function (num) {
            var i = num;
            var e = 2;
            return Boolean(i % e);
        };
        /**
         * Check if number is even (convert to Integer if necessary).
         *
         * @method isEven
         * @param num {number} The number.
         * @return {boolean}
         * @example
         *      MathUtil.isEven(2);
         *      // true
         *
         *      MathUtil.isEven(3);
         *      // false
         */
        MathUtil.isEven = function (num) {
            var int = num;
            var e = 2;
            return (int % e == 0);
        };
        /**
         * Check if number is Prime (divisible only by itself and one).
         *
         * @method isPrime
         * @param num {number} The number.
         * @return {boolean}
         * @example
         *      MathUtil.isPrime(4);
         *      // false
         *
         *      MathUtil.isPrime(5);
         *      // true
         */
        MathUtil.isPrime = function (num) {
            if (num > 2 && num % 2 == 0) {
                return false;
            }
            var l = Math.sqrt(num);
            var i = 3;
            for (i; i <= l; i += 2) {
                if (num % i == 0) {
                    return false;
                }
            }
            return true;
        };
        /**
         * Calculate the factorial of the integer.
         *
         * @method factorial
         * @param num {number} The number.
         * @return {number}
         * @example
         *      MathUtil.factorial(5);
         *      // 120
         *
         *      MathUtil.factorial(9);
         *      // 362880
         */
        MathUtil.factorial = function (num) {
            Iif (num == 0) {
                return 1;
            }
            var d = num.valueOf();
            var i = d - 1;
            while (i) {
                d = d * i;
                i--;
            }
            return d;
        };
        /**
         * Return an array of divisors of the integer.
         *
         * @method getDivisors
         * @param num {number} The number.
         * @return {Array.<number>}
         * @example
         *
         */
        MathUtil.getDivisors = function (num) {
            var r = [];
            for (var i_1 = 1, e = num / 2; i_1 <= e; i_1++) {
                if (num % i_1 == 0) {
                    r.push(i_1);
                }
            }
            if (num != 0) {
                r.push(num.valueOf());
            }
            return r;
        };
        return MathUtil;
    })();
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = MathUtil;
});