dtable-ui-component 0.3.5 → 0.3.6-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,587 +0,0 @@
1
- /* eslint-disable no-case-declarations */
2
- import dayjs from 'dayjs';
3
- import NP from './number-precision';
4
- import { CellType, NUMBER_TYPES, DEFAULT_NUMBER_FORMAT, DURATION_FORMATS_MAP, DURATION_FORMATS, DURATION_ZERO_DISPLAY, DURATION_DECIMAL_DIGITS, FORMULA_RESULT_TYPE, COLLABORATOR_COLUMN_TYPES, ARRAY_FORMAL_COLUMNS_TYPES, DEFAULT_DATE_FORMAT } from '../constants';
5
- import { getFloatNumber } from './utils';
6
- NP.enableBoundaryChecking(false);
7
- var _separatorMap = {
8
- 'comma': ',',
9
- 'dot': '.',
10
- 'no': '',
11
- 'space': ' '
12
- };
13
- /**
14
- * @param {string} value
15
- * e.g. removeZerosFromEnd('0.0100') // '0.01'
16
- */
17
-
18
- var removeZerosFromEnd = function removeZerosFromEnd(value) {
19
- if (value.endsWith('0')) {
20
- return value.replace(/(?:\.0*|(\.\d+?)0+)$/, '$1');
21
- }
22
-
23
- return value;
24
- };
25
-
26
- var _toThousands = function _toThousands(num, isCurrency, formatData) {
27
- var _ref = formatData || {},
28
- _ref$decimal = _ref.decimal,
29
- decimal = _ref$decimal === void 0 ? 'dot' : _ref$decimal,
30
- _ref$thousands = _ref.thousands,
31
- thousands = _ref$thousands === void 0 ? 'no' : _ref$thousands,
32
- _ref$precision = _ref.precision,
33
- precision = _ref$precision === void 0 ? 2 : _ref$precision,
34
- _ref$enable_precision = _ref.enable_precision,
35
- enable_precision = _ref$enable_precision === void 0 ? false : _ref$enable_precision;
36
-
37
- var decimalString = _separatorMap[decimal];
38
- var thousandsString = _separatorMap[thousands];
39
-
40
- if ((num + '').indexOf('e') > -1) {
41
- if (num < 1 && num > -1) {
42
- // 1.convert to non-scientific number
43
- var numericString = num.toFixed(enable_precision ? precision : 8); // 2.remove 0 from end of the number which not set precision. e.g. 0.100000
44
-
45
- if (!enable_precision) {
46
- numericString = removeZerosFromEnd(numericString);
47
- } // 3.remove minus from number which equal to 0. e.g. '-0.00'
48
-
49
-
50
- if (parseFloat(numericString) === 0) {
51
- return numericString.startsWith('-') ? numericString.substring(1) : numericString;
52
- }
53
-
54
- return numericString;
55
- }
56
-
57
- return num;
58
- }
59
-
60
- var decimalDigits = enable_precision ? precision : _getDecimalDigits(num);
61
- var value = parseFloat(num.toFixed(decimalDigits));
62
- var isMinus = value < 0;
63
- var integer = Math.trunc(value); // format decimal value
64
-
65
- var decimalValue = String(Math.abs(NP.minus(value, integer)).toFixed(decimalDigits)).slice(1);
66
-
67
- if (!enable_precision) {
68
- decimalValue = removeZerosFromEnd(decimalValue);
69
- }
70
-
71
- if (isCurrency) {
72
- if (!enable_precision) {
73
- if (decimalValue.length === 2) {
74
- decimalValue = decimalValue.padEnd(3, '0');
75
- } else {
76
- decimalValue = (decimalValue.substring(0, 3) || '.').padEnd(3, '0');
77
- }
78
- }
79
- }
80
-
81
- decimalValue = decimalValue.replace(/./, decimalString); // format integer value
82
-
83
- var result = [],
84
- counter = 0;
85
- integer = Math.abs(integer).toString();
86
-
87
- for (var i = integer.length - 1; i >= 0; i--) {
88
- counter++;
89
- result.unshift(integer[i]);
90
-
91
- if (!(counter % 3) && i !== 0) {
92
- result.unshift(thousandsString);
93
- }
94
- }
95
-
96
- return (isMinus ? '-' : '') + result.join('') + decimalValue;
97
- };
98
-
99
- var _getDecimalDigits = function _getDecimalDigits(num) {
100
- if (Number.isInteger(num)) {
101
- return 0;
102
- }
103
-
104
- var valueArr = (num + '').split('.');
105
- var digitsLength = valueArr[1] ? valueArr[1].length : 8;
106
- return digitsLength > 8 ? 8 : digitsLength;
107
- };
108
-
109
- export var getNumberDisplayString = function getNumberDisplayString(value, formatData) {
110
- // formatData: old version maybe 'null'
111
- var type = Object.prototype.toString.call(value);
112
-
113
- if (type !== '[object Number]') {
114
- // return formula internal errors directly.
115
- if (type === '[object String]' && value.startsWith('#')) {
116
- return value;
117
- }
118
-
119
- return '';
120
- }
121
-
122
- if (isNaN(value) || value === Infinity || value === -Infinity) return value + '';
123
-
124
- var _ref2 = formatData || {},
125
- _ref2$format = _ref2.format,
126
- format = _ref2$format === void 0 ? DEFAULT_NUMBER_FORMAT : _ref2$format;
127
-
128
- switch (format) {
129
- case NUMBER_TYPES.NUMBER:
130
- {
131
- return _toThousands(value, false, formatData);
132
- }
133
-
134
- case NUMBER_TYPES.PERCENT:
135
- {
136
- return "".concat(_toThousands(Number.parseFloat((value * 100).toFixed(8)), false, formatData), "%");
137
- }
138
-
139
- case NUMBER_TYPES.YUAN:
140
- {
141
- return "\uFFE5".concat(_toThousands(value, true, formatData));
142
- }
143
-
144
- case NUMBER_TYPES.DOLLAR:
145
- {
146
- return "$".concat(_toThousands(value, true, formatData));
147
- }
148
-
149
- case NUMBER_TYPES.EURO:
150
- {
151
- return "\u20AC".concat(_toThousands(value, true, formatData));
152
- }
153
-
154
- case 'duration':
155
- {
156
- return getDurationDisplayString(value, formatData);
157
- }
158
-
159
- case NUMBER_TYPES.CUSTOM_CURRENCY:
160
- {
161
- if (formatData.currency_symbol_position === 'after') {
162
- return "".concat(_toThousands(value, true, formatData)).concat(formatData.currency_symbol || '');
163
- } else {
164
- return "".concat(formatData.currency_symbol || '').concat(_toThousands(value, true, formatData));
165
- }
166
- }
167
-
168
- default:
169
- return '' + value;
170
- }
171
- };
172
- export var formatStringToNumber = function formatStringToNumber(numberString, formatData) {
173
- var _ref3 = formatData || {},
174
- format = _ref3.format,
175
- decimal = _ref3.decimal,
176
- thousands = _ref3.thousands,
177
- enable_precision = _ref3.enable_precision,
178
- precision = _ref3.precision;
179
-
180
- var value = numberString;
181
-
182
- if (decimal && thousands && decimal === 'comma') {
183
- if (thousands === 'dot') {
184
- value = value.replace(/,/, '@');
185
- value = value.replace(/\./g, ',');
186
- value = value.replace(/@/, '.');
187
- } else {
188
- value = value.replace(/\./g, '');
189
- value = value.replace(/,/, '.');
190
- }
191
- }
192
-
193
- value = getFloatNumber(value, format);
194
-
195
- if (enable_precision && value) {
196
- if (format === 'percent') {
197
- precision += 2;
198
- }
199
-
200
- value = Number(parseFloat(value).toFixed(precision));
201
- }
202
-
203
- return value;
204
- };
205
- export var replaceNumberNotAllowInput = function replaceNumberNotAllowInput(value) {
206
- var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_NUMBER_FORMAT;
207
- var currency_symbol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
208
-
209
- if (!value) {
210
- return '';
211
- }
212
-
213
- value = value.replace(/。/g, '.');
214
-
215
- switch (format) {
216
- case NUMBER_TYPES.NUMBER:
217
- case NUMBER_TYPES.NUMBER_WITH_COMMAS:
218
- {
219
- return value.replace(/[^.-\d,]/g, '');
220
- }
221
-
222
- case NUMBER_TYPES.PERCENT:
223
- {
224
- return value.replace(/[^.-\d,%]/g, '');
225
- }
226
-
227
- case NUMBER_TYPES.YUAN:
228
- {
229
- return value.replace(/[^.-\d¥¥,]/g, '');
230
- }
231
-
232
- case NUMBER_TYPES.DOLLAR:
233
- {
234
- return value.replace(/[^.-\d$,]/g, '');
235
- }
236
-
237
- case NUMBER_TYPES.EURO:
238
- {
239
- return value.replace(/[^.-\d€,]/g, '');
240
- }
241
-
242
- case NUMBER_TYPES.CUSTOM_CURRENCY:
243
- {
244
- // eslint-disable-next-line
245
- var reg = new RegExp('[^.-\d' + currency_symbol + ',]', 'g');
246
- return value.replace(reg, '');
247
- }
248
-
249
- default:
250
- return value.replace(/[^.-\d,]/g, '');
251
- }
252
- };
253
- export var getDateDisplayString = function getDateDisplayString(value, format) {
254
- if (!value || typeof value !== 'string') {
255
- return '';
256
- }
257
-
258
- var date = dayjs(value);
259
- if (!date.isValid()) return value;
260
-
261
- switch (format) {
262
- case 'D/M/YYYY':
263
- case 'DD/MM/YYYY':
264
- // no-case-declarations
265
- var formatValue = date.format('YYYY-MM-DD');
266
- var formatValueList = formatValue.split('-');
267
- return "".concat(formatValueList[2], "/").concat(formatValueList[1], "/").concat(formatValueList[0]);
268
-
269
- case 'D/M/YYYY HH:mm':
270
- case 'DD/MM/YYYY HH:mm':
271
- var formatValues = date.format('YYYY-MM-DD HH:mm');
272
- var formatValuesList = formatValues.split(' ');
273
- var formatDateList = formatValuesList[0].split('-');
274
- return "".concat(formatDateList[2], "/").concat(formatDateList[1], "/").concat(formatDateList[0], " ").concat(formatValuesList[1]);
275
-
276
- case 'M/D/YYYY':
277
- case 'M/D/YYYY HH:mm':
278
- case 'YYYY-MM-DD':
279
- case 'YYYY-MM-DD HH:mm':
280
- case 'DD.MM.YYYY':
281
- case 'DD.MM.YYYY HH:mm':
282
- return date.format(format);
283
-
284
- default:
285
- return date.format('YYYY-MM-DD');
286
- }
287
- };
288
-
289
- var _getMathRoundedDuration = function _getMathRoundedDuration(num, duration_format) {
290
- var decimalDigits = DURATION_DECIMAL_DIGITS[duration_format];
291
-
292
- if (decimalDigits < 1) {
293
- return num;
294
- }
295
-
296
- var ratio = Math.pow(10, decimalDigits);
297
- return Math.round(num * ratio) / ratio;
298
- };
299
-
300
- var _getDurationDecimalSuffix = function _getDurationDecimalSuffix(duration_format, decimal) {
301
- if (duration_format === DURATION_FORMATS_MAP.H_MM_SS_S) {
302
- return decimal === 0 ? '.0' : '';
303
- } else if (duration_format === DURATION_FORMATS_MAP.H_MM_SS_SS) {
304
- if (decimal === 0) {
305
- return '.00';
306
- } else if (decimal < 10) {
307
- return '0';
308
- }
309
- } else if (duration_format === DURATION_FORMATS_MAP.H_MM_SS_SSS) {
310
- if (decimal === 0) {
311
- return '.000';
312
- } else if (decimal < 10) {
313
- return '00';
314
- } else if (decimal < 100) {
315
- return '0';
316
- }
317
- }
318
-
319
- return '';
320
- };
321
-
322
- export var getDurationDisplayString = function getDurationDisplayString(value, data) {
323
- if (!value && value !== 0) return '';
324
-
325
- var _ref4 = data || {},
326
- duration_format = _ref4.duration_format;
327
-
328
- duration_format = duration_format || DURATION_FORMATS_MAP.H_MM;
329
-
330
- if (DURATION_FORMATS.findIndex(function (format) {
331
- return format.type === duration_format;
332
- }) < 0) {
333
- return '';
334
- }
335
-
336
- if (value === 0) {
337
- return DURATION_ZERO_DISPLAY[duration_format];
338
- }
339
-
340
- var includeDecimal = duration_format.indexOf('.') > -1;
341
- var positiveValue = Math.abs(value);
342
-
343
- if (!includeDecimal) {
344
- positiveValue = Math.round(positiveValue);
345
- }
346
-
347
- positiveValue = _getMathRoundedDuration(positiveValue, duration_format);
348
- var decimalParts = (positiveValue + '').split('.');
349
- var decimalPartsLen = decimalParts.length;
350
- var decimal = 0;
351
-
352
- if (decimalPartsLen > 1) {
353
- decimal = decimalParts[decimalPartsLen - 1];
354
- decimal = decimal ? decimal - 0 : 0;
355
- }
356
-
357
- var decimalDigits = DURATION_DECIMAL_DIGITS[duration_format];
358
-
359
- var decimalSuffix = _getDurationDecimalSuffix(duration_format, decimal);
360
-
361
- var displayString = value < 0 ? '-' : '';
362
- var hours = parseInt(positiveValue / 3600);
363
- var minutes = parseInt((positiveValue - hours * 3600) / 60);
364
-
365
- if (duration_format === DURATION_FORMATS_MAP.H_MM) {
366
- displayString += "".concat(hours, ":").concat(minutes > 9 ? minutes : '0' + minutes);
367
- return displayString;
368
- }
369
-
370
- var seconds = Number.parseFloat((positiveValue - hours * 3600 - minutes * 60).toFixed(decimalDigits));
371
- minutes = minutes > 9 ? minutes : "0".concat(minutes);
372
- seconds = seconds > 9 ? seconds : "0".concat(seconds);
373
- displayString += "".concat(hours, ":").concat(minutes, ":").concat(seconds).concat(decimalSuffix);
374
- return displayString;
375
- };
376
- export var getOptionName = function getOptionName(options, targetOptionID) {
377
- if (!targetOptionID || !options || !Array.isArray(options)) return null;
378
- var option = options.find(function (option) {
379
- return option.id === targetOptionID;
380
- });
381
- return option ? option.name : null;
382
- };
383
- export var getMultipleOptionName = function getMultipleOptionName(options, cellVal) {
384
- if (!cellVal || !options || !Array.isArray(options)) return null;
385
- var selectedOptions = options.filter(function (option) {
386
- return cellVal.includes(option.id);
387
- });
388
- if (selectedOptions.length === 0) return null;
389
- return selectedOptions.map(function (option) {
390
- return option.name;
391
- }).join(', ');
392
- };
393
- export var getLongtextDisplayString = function getLongtextDisplayString(value) {
394
- var _ref5 = value || {},
395
- text = _ref5.text;
396
-
397
- if (!text) {
398
- return null;
399
- }
400
-
401
- return text;
402
- };
403
- export var getCollaboratorsName = function getCollaboratorsName(collaborators, cellVal) {
404
- if (cellVal) {
405
- var collaboratorsName = [];
406
- cellVal.forEach(function (v) {
407
- var collaborator = collaborators.find(function (c) {
408
- return c.email === v;
409
- });
410
-
411
- if (collaborator) {
412
- collaboratorsName.push(collaborator.name);
413
- }
414
- });
415
-
416
- if (collaboratorsName.length === 0) {
417
- return null;
418
- }
419
-
420
- return collaboratorsName.join(', ');
421
- }
422
-
423
- return null;
424
- };
425
- export var getGeolocationDisplayString = function getGeolocationDisplayString(value, columnData) {
426
- var _ref6 = columnData || {},
427
- geo_format = _ref6.geo_format;
428
-
429
- var cellValue = value || {};
430
-
431
- if (!value) {
432
- return '';
433
- }
434
-
435
- if (geo_format === 'lng_lat' && value.lng && value.lat) {
436
- return "".concat(cellValue.lng, ", ").concat(cellValue.lat);
437
- }
438
-
439
- if (geo_format === 'country_region' && cellValue.country_region) {
440
- return value.country_region || '';
441
- }
442
-
443
- var province = cellValue.province,
444
- city = cellValue.city,
445
- district = cellValue.district,
446
- detail = cellValue.detail;
447
-
448
- if (geo_format === 'province') {
449
- return "".concat(province || '');
450
- }
451
-
452
- if (geo_format === 'province_city') {
453
- return "".concat(province || '').concat(city || '');
454
- }
455
-
456
- return "".concat(province || '').concat(city || '').concat(district || '').concat(detail || '');
457
- };
458
- export var getFormulaDisplayString = function getFormulaDisplayString(cellValue, columnData) {
459
- var _ref7 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
460
- _ref7$collaborators = _ref7.collaborators,
461
- collaborators = _ref7$collaborators === void 0 ? [] : _ref7$collaborators;
462
-
463
- if (!columnData) {
464
- return null;
465
- }
466
-
467
- var result_type = columnData.result_type;
468
-
469
- if (result_type === FORMULA_RESULT_TYPE.NUMBER) {
470
- return getNumberDisplayString(cellValue, columnData);
471
- }
472
-
473
- if (result_type === FORMULA_RESULT_TYPE.DATE) {
474
- var format = columnData.format;
475
- return getDateDisplayString(cellValue, format);
476
- }
477
-
478
- if (result_type === FORMULA_RESULT_TYPE.ARRAY) {
479
- var array_type = columnData.array_type,
480
- array_data = columnData.array_data;
481
-
482
- if (!array_type) {
483
- return null;
484
- }
485
-
486
- if (COLLABORATOR_COLUMN_TYPES.includes(array_type)) {
487
- return cellValue;
488
- }
489
-
490
- if (ARRAY_FORMAL_COLUMNS_TYPES.indexOf(array_type) < 0 && Array.isArray(cellValue)) {
491
- return cellValue.map(function (val) {
492
- return getCellValueDisplayString(val, array_type, {
493
- data: array_data,
494
- collaborators: collaborators
495
- });
496
- }).join(', ');
497
- }
498
-
499
- return getCellValueDisplayString(cellValue, array_type, {
500
- data: array_data,
501
- collaborators: collaborators
502
- });
503
- }
504
-
505
- if (Object.prototype.toString.call(cellValue) === '[object Boolean]') {
506
- return cellValue + '';
507
- }
508
-
509
- return cellValue;
510
- };
511
- export function getCellValueDisplayString(cellValue, type) {
512
- var _ref8 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
513
- data = _ref8.data,
514
- _ref8$collaborators = _ref8.collaborators,
515
- collaborators = _ref8$collaborators === void 0 ? [] : _ref8$collaborators;
516
-
517
- var newData = data || {};
518
-
519
- switch (type) {
520
- case CellType.GEOLOCATION:
521
- {
522
- return getGeolocationDisplayString(cellValue, data);
523
- }
524
-
525
- case CellType.SINGLE_SELECT:
526
- {
527
- if (!data) return '';
528
- var options = newData.options;
529
- return getOptionName(options, cellValue);
530
- }
531
-
532
- case CellType.MULTIPLE_SELECT:
533
- {
534
- if (!data) return '';
535
- var _options = newData.options;
536
- return getMultipleOptionName(_options, cellValue);
537
- }
538
-
539
- case CellType.FORMULA:
540
- case CellType.LINK_FORMULA:
541
- {
542
- return getFormulaDisplayString(cellValue, newData, {
543
- collaborators: collaborators
544
- });
545
- }
546
-
547
- case CellType.LONG_TEXT:
548
- {
549
- return getLongtextDisplayString(cellValue);
550
- }
551
-
552
- case CellType.NUMBER:
553
- {
554
- return getNumberDisplayString(cellValue, newData);
555
- }
556
-
557
- case CellType.DATE:
558
- {
559
- var _ref9 = newData || {},
560
- _ref9$format = _ref9.format,
561
- format = _ref9$format === void 0 ? DEFAULT_DATE_FORMAT : _ref9$format;
562
-
563
- return getDateDisplayString(cellValue, format);
564
- }
565
-
566
- case CellType.CREATOR:
567
- case CellType.LAST_MODIFIER:
568
- {
569
- return cellValue === 'anonymous' ? cellValue : getCollaboratorsName(collaborators, [cellValue]);
570
- }
571
-
572
- case CellType.COLLABORATOR:
573
- {
574
- return getCollaboratorsName(collaborators, cellValue);
575
- }
576
-
577
- case CellType.DURATION:
578
- {
579
- return getDurationDisplayString(cellValue, data);
580
- }
581
-
582
- default:
583
- {
584
- return cellValue ? cellValue + '' : '';
585
- }
586
- }
587
- }