all files / workbook/common/ math.js

95.9% Statements 117/122
95.88% Branches 93/97
100% Functions 11/11
95.9% Lines 117/122
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   11× 11×       11× 11× 11× 11×     86× 86× 86×   27238× 27238× 27238× 14553×   27238× 27238× 27238×   12513× 12513× 12513× 12513× 12513× 12513×   13424×   1285982×   12442× 12442× 12442× 12442× 12442× 12194× 6002×     12442× 6047× 6047× 116× 116× 116×     12442× 12326× 12326× 6289× 6289× 38× 38×         12324× 12339× 6299× 25186× 25186× 25186× 156× 156× 156×       12339× 12183× 12183× 294709× 294709× 294709× 294709× 36649×   294709× 12078× 12078× 12078× 12078×       12339× 105× 420× 420× 420×                 12324× 16× 16× 16× 16× 15× 15× 15× 15× 13×           12308×     12440× 39× 39×   12440×   11262× 3474× 3474× 55× 55×   3474× 1667×     9595×      
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 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(' '));
                        if (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) {
        if (value && value !== '.' && value !== '-') {
            var val = value.toString();
            if (val.startsWith('-')) {
                val = val.slice(1);
                val = val.includes('-') ? value : val;
            }
            if (/^\d*\.?\d*$/.test(val)) {
                return parseFloat(value);
            }
        }
        return value;
    }
    exports.parseIntValue = parseIntValue;
});