-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathmath.ts
240 lines (230 loc) · 9.77 KB
/
math.ts
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
import { isNullOrUndefined, Internationalization, IntlBase, cldrData } from '@syncfusion/ej2-base';
import { CellModel } from '../base';
/**
* @hidden
* @param {number} val - Specifies the val.
* @returns {string} - To get Fraction.
*/
export function toFraction(val: number): string {
const strVal: string = val.toString();
if (val === parseInt(strVal, 10)) {
return parseInt(strVal, 10) + ' ';
} else {
const top: string | number = strVal.indexOf('.') > -1 ? strVal.split('.')[1] : 0;
const bottom: number = Math.pow(10, top.toString().replace('-', '').length);
const abs: number = Math.abs(getGcd(top, bottom));
return (top as number / abs) + '/' + (bottom / abs);
}
}
/**
* @hidden
* @param {string | number} a - Specifies the a.
* @param {string | number} b - Specifies the b.
* @returns {number} - To get Gcd.
*/
export function getGcd(a: string | number, b: string | number): number {
a = Number(a);
b = Number(b);
return (b) ? getGcd(b, a % b) : a;
}
/**
* @hidden
* @param {number} val - Specifies the value.
* @returns {Date} - Returns Date.
*/
export function intToDate(val: number | string): Date {
val = Number(val);
val = (val > 0 && val < 1) ? (1 + val) : (val === 0) ? 1 : val;
if (val > 60) {
val -= 1; // Due to leap year issue of 1900 in MSExcel.
}
const startDate: Date = new Date('01/01/1900');
const startDateUTC: number = 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', ''));
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* @hidden
* @param {number} val - Specifies the value.
* @param {boolean} isTime - Specifies the boolean value.
* @param {boolean} isTimeOnly - Specifies the value is only a time without date.
* @returns {number} - Returns number.
*/
export function dateToInt(val: any, isTime?: boolean, isTimeOnly?: boolean): number {
const startDate: Date = new Date('01/01/1900');
const date: Date = isDateTime(val) ? val : new Date(val);
const startDateUTC: number = Date.UTC(
startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), startDate.getHours(),
startDate.getMinutes(), startDate.getSeconds(), startDate.getMilliseconds());
const dateUTC: number = Date.UTC(
date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(),
date.getSeconds(), date.getMilliseconds());
const diffDays: number = ((dateUTC - startDateUTC) / (1000 * 3600 * 24));
return (isTime ? diffDays : parseInt(diffDays.toString(), 10)) + (isTimeOnly ? 0 : (diffDays > 60 ? 2 : 1));
}
/**
* @hidden
* @param {any} date - Specifies the date.
* @returns {boolean} - Returns boolean value.
*/
export function isDateTime(date: any): boolean {
return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.valueOf());
}
/**
* @hidden
* @param {string} val - Specifies the value.
* @returns {boolean} - Returns boolean value.
*/
export function isNumber(val: string | number): boolean {
return val as number - parseFloat(val as string) >= 0;
}
/**
* @hidden
* @param {string | number} val - Specifies the value.
* @returns {string} - Returns converted value.
*/
export function evaluate(val: string | number): string {
return Function('"use strict";return (' + val + ')')();
}
/**
* @hidden
* @param {Date | string | number} text - Specifies the text.
* @param {Internationalization} intl - Specifies the Internationalization.
* @param {string} locale - Specifies the locale.
* @param {string} format - Specifies the string.
* @param {CellModel} cell - Specify the cell.
* @param {boolean} isDateTime -Specify is DateTime value or not.
* @returns {ToDateArgs} - Returns Date format.
*/
export function toDate(
text: Date | string | number, intl: Internationalization, locale: string, format?: string, cell?: CellModel,
isDateTime?: boolean): ToDateArgs {
const defaultDateFormats: Object = IntlBase.getDependables(cldrData, locale, null).dateObject;
const availabelDateTimeFormat: Object = (defaultDateFormats as any).dateTimeFormats.availableFormats;
const dObj: ToDateArgs = { dateObj: null, isCustom: false, type: '' }; let dateVal: Date;
const updateTime: Function = (): void => {
if (dObj.type === 'time') {
dObj.dateObj = new Date((dateVal ? dateVal.toDateString() : '01/01/1900') + ' ' + dObj.dateObj.toLocaleTimeString());
}
};
if (format) {
dObj.dateObj = intl.parseDate(text as string, { format: format });
if (dObj.dateObj) {
dObj.type = text.toString().indexOf(':') > -1 ? 'time' : 'datetime';
updateTime();
dObj.isCustom = true;
}
}
if (isNullOrUndefined(dObj.dateObj)) {
text = text.toString();
if (text && text.indexOf('/') > -1 || text.indexOf('-') > 0) {
let cFormat: string = (cell && cell.format) || format;
if (cFormat) {
const hyphenDate: boolean = 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 as string, { format: cFormat, skeleton: 'yMd' });
if (dObj.dateObj) {
dObj.type = 'date';
return dObj;
}
}
}
}
const parseDateTimeValue: Function = (text: string): void => {
if (text.indexOf(':') < 0) {
for (const key of Object.keys((defaultDateFormats as { dateFormats?: object }).dateFormats)) {
dObj.dateObj = intl.parseDate(
text, { format: (defaultDateFormats as { dateFormats?: object }).dateFormats[`${key}`], skeleton: key });
if (dObj.dateObj) {
dObj.type = 'date';
dObj.isCustom = false;
break;
}
}
}
if (isNullOrUndefined(dObj.dateObj)) {
let dateTimeFormat: string;
for (const key of Object.keys(availabelDateTimeFormat)) {
dateTimeFormat = availabelDateTimeFormat[`${key}`];
dObj.dateObj = intl.parseDate(text as string, { format: dateTimeFormat, skeleton: key });
if (!dObj.dateObj && text.indexOf(':') > -1 && dateTimeFormat.indexOf(':') > -1) { // parsing time format without am or pm
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 (isNullOrUndefined(dObj.dateObj)) {
for (const key of Object.keys((defaultDateFormats as { timeFormats?: object }).timeFormats)) {
dObj.dateObj = intl.parseDate(
text, { format: (defaultDateFormats as { timeFormats?: object }).timeFormats[`${key}`], skeleton: key });
if (dObj.dateObj) {
dObj.type = 'time';
updateTime();
dObj.isCustom = false;
break;
}
}
}
};
if (isDateTime) {
const dateTimeArr: string[] = text.split(' ');
if (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;
}
/**
* @hidden
* @param {string} value - Specifies the value.
* @param {boolean} isPaste - Optional flag indicating whether the value came from a paste action.
* @param {boolean} isFromExternalPaste - Optional flag indicating whether the value came from a getExternalCells method.
* @returns { string | number} - ReturnsparseIntValue.
*/
export function parseIntValue(value: string, isPaste?: boolean, isFromExternalPaste?: boolean): string | number {
if (value && value !== '.' && value !== '-') {
let val: string = value.toString();
const maxSafeIntegerLength: number = Number.MAX_SAFE_INTEGER.toString().length;
if (val.startsWith('-')) {
val = val.slice(1);
val = val.includes('-') ? value : val;
}
if (/^\d*\.?\d*$/.test(val)) {
// If the number is longer than the safe integer length.
if (isPaste && (val.length > maxSafeIntegerLength || (isFromExternalPaste && (val.startsWith('0') || val.endsWith('0'))))) {
return value; //skip parsefloat to get the precise value while pasting.
} else {
return parseFloat(value);
}
}
}
return value;
}
export interface ToDateArgs {
dateObj: Date;
type: string;
isCustom: boolean;
}