all files / workbook/common/ math.js

96.09% Statements 123/128
95.19% Branches 99/104
100% Functions 12/12
96.09% Lines 123/128
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             128× 128× 128×   34592× 34592× 34592× 18255×   34592× 34592× 34592×   15683× 15683× 15683× 15683× 15683× 15683×   16562×   1452894×   11×   15583× 15583× 15583× 15583× 15583× 15305× 7562×     15583× 7607× 7607× 136× 136× 136×     15583× 15447× 15447× 7833× 7833× 41× 41×         15445× 15461× 7874× 31413× 31413× 31413× 183× 183× 183×       15461× 15278× 15278× 369047× 369047× 369047× 369047× 45918×   369047× 15169× 15169× 15169× 15169×       15461× 109× 436× 436× 436×                 15445× 17× 17× 17× 17× 16× 16× 16× 16× 16×           15428×     15581× 43× 43×   15581×   44721× 4852× 4852× 4852× 67× 67×   4852× 2757× 108×     2649×       41964×      
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function toFraction(val) {
        var strVal = val.toString();
        Iif (val === parseInt(strVal, 10)) {
            return parseInt(strVal, 10) + '  ';
        }
        else {
            var top_1 = strVal.indexOf('.') > -1 ? strVal.split('.')[1] : 0;
            var bottom = Math.pow(10, top_1.toString().replace('-', '').length);
            var abs = Math.abs(getGcd(top_1, bottom));
            return (top_1 / abs) + '/' + (bottom / abs);
        }
    }
    exports.toFraction = toFraction;
    function getGcd(a, b) {
        a = Number(a);
        b = Number(b);
        return (b) ? getGcd(b, a % b) : a;
    }
    exports.getGcd = getGcd;
    function intToDate(val) {
        val = Number(val);
        val = (val > 0 && val < 1) ? (1 + val) : (val === 0) ? 1 : val;
        if (val > 60) {
            val -= 1;
        }
        var startDate = new Date('01/01/1900');
        var startDateUTC = Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), startDate.getHours(), startDate.getMinutes(), startDate.getSeconds(), startDate.getMilliseconds());
        return new Date(new Date(((val - 1) * (1000 * 3600 * 24)) + startDateUTC).toUTCString().replace(' GMT', ''));
    }
    exports.intToDate = intToDate;
    function dateToInt(val, isTime, isTimeOnly) {
        var startDate = new Date('01/01/1900');
        var date = isDateTime(val) ? val : new Date(val);
        var startDateUTC = Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), startDate.getHours(), startDate.getMinutes(), startDate.getSeconds(), startDate.getMilliseconds());
        var dateUTC = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
        var diffDays = ((dateUTC - startDateUTC) / (1000 * 3600 * 24));
        return (isTime ? diffDays : parseInt(diffDays.toString(), 10)) + (isTimeOnly ? 0 : (diffDays > 60 ? 2 : 1));
    }
    exports.dateToInt = dateToInt;
    function isDateTime(date) {
        return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.valueOf());
    }
    exports.isDateTime = isDateTime;
    function isNumber(val) {
        return val - parseFloat(val) >= 0;
    }
    exports.isNumber = isNumber;
    function evaluate(val) {
        return Function('"use strict";return (' + val + ')')();
    }
    exports.evaluate = evaluate;
    function toDate(text, intl, locale, format, cell, isDateTime) {
        var defaultDateFormats = ej2_base_1.IntlBase.getDependables(ej2_base_1.cldrData, locale, null).dateObject;
        var availabelDateTimeFormat = defaultDateFormats.dateTimeFormats.availableFormats;
        var dObj = { dateObj: null, isCustom: false, type: '' };
        var dateVal;
        var updateTime = function () {
            if (dObj.type === 'time') {
                dObj.dateObj = new Date((dateVal ? dateVal.toDateString() : '01/01/1900') + ' ' + dObj.dateObj.toLocaleTimeString());
            }
        };
        if (format) {
            dObj.dateObj = intl.parseDate(text, { format: format });
            if (dObj.dateObj) {
                dObj.type = text.toString().indexOf(':') > -1 ? 'time' : 'datetime';
                updateTime();
                dObj.isCustom = true;
            }
        }
        if (ej2_base_1.isNullOrUndefined(dObj.dateObj)) {
            text = text.toString();
            if (text && text.indexOf('/') > -1 || text.indexOf('-') > 0) {
                var cFormat = (cell && cell.format) || format;
                if (cFormat) {
                    var hyphenDate = cFormat.toLowerCase().includes('dd-mm-yy');
                    if (hyphenDate || cFormat.toLowerCase().includes('dd/mm/yy')) {
                        cFormat = hyphenDate ? 'd-M-y' : 'd/M/y';
                        dObj.dateObj = intl.parseDate(text, { format: cFormat, skeleton: 'yMd' });
                        if (dObj.dateObj) {
                            dObj.type = 'date';
                            return dObj;
                        }
                    }
                }
            }
            var parseDateTimeValue = function (text) {
                if (text.indexOf(':') < 0) {
                    for (var _i = 0, _a = Object.keys(defaultDateFormats.dateFormats); _i < _a.length; _i++) {
                        var key = _a[_i];
                        dObj.dateObj = intl.parseDate(text, { format: defaultDateFormats.dateFormats["" + key], skeleton: key });
                        if (dObj.dateObj) {
                            dObj.type = 'date';
                            dObj.isCustom = false;
                            break;
                        }
                    }
                }
                if (ej2_base_1.isNullOrUndefined(dObj.dateObj)) {
                    var dateTimeFormat = void 0;
                    for (var _b = 0, _c = Object.keys(availabelDateTimeFormat); _b < _c.length; _b++) {
                        var key = _c[_b];
                        dateTimeFormat = availabelDateTimeFormat["" + key];
                        dObj.dateObj = intl.parseDate(text, { format: dateTimeFormat, skeleton: key });
                        if (!dObj.dateObj && text.indexOf(':') > -1 && dateTimeFormat.indexOf(':') > -1) {
                            dObj.dateObj = intl.parseDate(text, { format: dateTimeFormat.split(' ')[0] });
                        }
                        if (dObj.dateObj) {
                            dObj.type = text.toString().indexOf(':') > -1 ? 'time' : 'datetime';
                            updateTime();
                            dObj.isCustom = true;
                            break;
                        }
                    }
                }
                if (ej2_base_1.isNullOrUndefined(dObj.dateObj)) {
                    for (var _d = 0, _e = Object.keys(defaultDateFormats.timeFormats); _d < _e.length; _d++) {
                        var key = _e[_d];
                        dObj.dateObj = intl.parseDate(text, { format: defaultDateFormats.timeFormats["" + key], skeleton: key });
                        Iif (dObj.dateObj) {
                            dObj.type = 'time';
                            updateTime();
                            dObj.isCustom = false;
                            break;
                        }
                    }
                }
            };
            if (isDateTime) {
                var dateTimeArr = text.split(' ');
                Eif (dateTimeArr.length >= 2) {
                    parseDateTimeValue(dateTimeArr.shift());
                    if (dObj.dateObj) {
                        dateVal = dObj.dateObj;
                        dObj.dateObj = null;
                        parseDateTimeValue(dateTimeArr.join(' '));
                        Eif (dObj.dateObj) {
                            dObj.type = 'datetime';
                        }
                    }
                }
            }
            else {
                parseDateTimeValue(text);
            }
        }
        if (text !== '#DIV/0!' && !dObj.dateObj && new Date(text).toString() !== 'Invalid Date') {
            dObj.dateObj = new Date(text);
            dObj.type = 'date';
        }
        return dObj;
    }
    exports.toDate = toDate;
    function parseIntValue(value, isPaste, isFromExternalPaste) {
        if (value && value !== '.' && value !== '-') {
            var val = value.toString();
            var maxSafeIntegerLength = Number.MAX_SAFE_INTEGER.toString().length;
            if (val.startsWith('-')) {
                val = val.slice(1);
                val = val.includes('-') ? value : val;
            }
            if (/^\d*\.?\d*$/.test(val)) {
                if (isPaste && (val.length > maxSafeIntegerLength || (isFromExternalPaste && (val.startsWith('0') || val.endsWith('0'))))) {
                    return value;
                }
                else {
                    return parseFloat(value);
                }
            }
        }
        return value;
    }
    exports.parseIntValue = parseIntValue;
});