dtable-ui-component 0.1.98 → 0.1.99
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.
- package/lib/utils/value-format-utils.js +75 -19
- package/package.json +1 -1
|
@@ -9,6 +9,18 @@ var _separatorMap = {
|
|
|
9
9
|
'no': '',
|
|
10
10
|
'space': ' '
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} value
|
|
14
|
+
* e.g. removeZerosFromEnd('0.0100') // '0.01'
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
var removeZerosFromEnd = function removeZerosFromEnd(value) {
|
|
18
|
+
if (value.endsWith('0')) {
|
|
19
|
+
return value.replace(/(?:\.0*|(\.\d+?)0+)$/, '$1');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return value;
|
|
23
|
+
};
|
|
12
24
|
|
|
13
25
|
var _toThousands = function _toThousands(num, isCurrency, formatData) {
|
|
14
26
|
var _ref = formatData || {},
|
|
@@ -23,27 +35,53 @@ var _toThousands = function _toThousands(num, isCurrency, formatData) {
|
|
|
23
35
|
|
|
24
36
|
var decimalString = _separatorMap[decimal];
|
|
25
37
|
var thousandsString = _separatorMap[thousands];
|
|
38
|
+
|
|
39
|
+
if ((num + '').indexOf('e') > -1) {
|
|
40
|
+
if (num < 1 && num > -1) {
|
|
41
|
+
// 1.convert to non-scientific number
|
|
42
|
+
var numericString = num.toFixed(enable_precision ? precision : 8); // 2.remove 0 from end of the number which not set precision. e.g. 0.100000
|
|
43
|
+
|
|
44
|
+
if (!enable_precision) {
|
|
45
|
+
numericString = removeZerosFromEnd(numericString);
|
|
46
|
+
} // 3.remove minus from number which equal to 0. e.g. '-0.00'
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if (parseFloat(numericString) === 0) {
|
|
50
|
+
return numericString.startsWith('-') ? numericString.substring(1) : numericString;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return numericString;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return num;
|
|
57
|
+
}
|
|
58
|
+
|
|
26
59
|
var decimalDigits = enable_precision ? precision : _getDecimalDigits(num);
|
|
27
60
|
var value = parseFloat(num.toFixed(decimalDigits));
|
|
28
|
-
var
|
|
61
|
+
var isMinus = value < 0;
|
|
62
|
+
var integer = Math.trunc(value); // format decimal value
|
|
63
|
+
|
|
29
64
|
var decimalValue = String(Math.abs(NP.minus(value, integer)).toFixed(decimalDigits)).slice(1);
|
|
30
65
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
} else {
|
|
35
|
-
decimalValue = (decimalValue.substring(0, 3) || '.').padEnd(3, '0');
|
|
36
|
-
}
|
|
66
|
+
if (!enable_precision) {
|
|
67
|
+
decimalValue = removeZerosFromEnd(decimalValue);
|
|
68
|
+
}
|
|
37
69
|
|
|
38
|
-
|
|
39
|
-
|
|
70
|
+
if (isCurrency) {
|
|
71
|
+
if (!enable_precision) {
|
|
72
|
+
if (decimalValue.length === 2) {
|
|
73
|
+
decimalValue = decimalValue.padEnd(3, '0');
|
|
74
|
+
} else {
|
|
75
|
+
decimalValue = (decimalValue.substring(0, 3) || '.').padEnd(3, '0');
|
|
76
|
+
}
|
|
40
77
|
}
|
|
41
78
|
}
|
|
42
79
|
|
|
43
|
-
decimalValue = decimalValue.replace(/./, decimalString);
|
|
80
|
+
decimalValue = decimalValue.replace(/./, decimalString); // format integer value
|
|
81
|
+
|
|
44
82
|
var result = [],
|
|
45
83
|
counter = 0;
|
|
46
|
-
integer =
|
|
84
|
+
integer = Math.abs(integer).toString();
|
|
47
85
|
|
|
48
86
|
for (var i = integer.length - 1; i >= 0; i--) {
|
|
49
87
|
counter++;
|
|
@@ -54,7 +92,7 @@ var _toThousands = function _toThousands(num, isCurrency, formatData) {
|
|
|
54
92
|
}
|
|
55
93
|
}
|
|
56
94
|
|
|
57
|
-
return result.join('') + decimalValue;
|
|
95
|
+
return (isMinus ? '-' : '') + result.join('') + decimalValue;
|
|
58
96
|
};
|
|
59
97
|
|
|
60
98
|
var _getDecimalDigits = function _getDecimalDigits(num) {
|
|
@@ -72,14 +110,15 @@ export var getNumberDisplayString = function getNumberDisplayString(value, forma
|
|
|
72
110
|
var type = Object.prototype.toString.call(value);
|
|
73
111
|
|
|
74
112
|
if (type !== '[object Number]') {
|
|
113
|
+
// return formula internal errors directly.
|
|
75
114
|
if (type === '[object String]' && value.startsWith('#')) {
|
|
76
115
|
return value;
|
|
77
116
|
}
|
|
78
117
|
|
|
79
|
-
return
|
|
118
|
+
return '';
|
|
80
119
|
}
|
|
81
120
|
|
|
82
|
-
if (isNaN(value) || value === Infinity || value === -Infinity
|
|
121
|
+
if (isNaN(value) || value === Infinity || value === -Infinity) return value + '';
|
|
83
122
|
|
|
84
123
|
var _ref2 = formatData || {},
|
|
85
124
|
_ref2$format = _ref2.format,
|
|
@@ -87,7 +126,9 @@ export var getNumberDisplayString = function getNumberDisplayString(value, forma
|
|
|
87
126
|
|
|
88
127
|
switch (format) {
|
|
89
128
|
case 'number':
|
|
90
|
-
|
|
129
|
+
{
|
|
130
|
+
return _toThousands(value, false, formatData);
|
|
131
|
+
}
|
|
91
132
|
|
|
92
133
|
case 'percent':
|
|
93
134
|
{
|
|
@@ -95,17 +136,32 @@ export var getNumberDisplayString = function getNumberDisplayString(value, forma
|
|
|
95
136
|
}
|
|
96
137
|
|
|
97
138
|
case 'yuan':
|
|
98
|
-
|
|
139
|
+
{
|
|
140
|
+
return "\uFFE5".concat(_toThousands(value, true, formatData));
|
|
141
|
+
}
|
|
99
142
|
|
|
100
143
|
case 'dollar':
|
|
101
|
-
|
|
144
|
+
{
|
|
145
|
+
return "$".concat(_toThousands(value, true, formatData));
|
|
146
|
+
}
|
|
102
147
|
|
|
103
148
|
case 'euro':
|
|
104
|
-
|
|
149
|
+
{
|
|
150
|
+
return "\u20AC".concat(_toThousands(value, true, formatData));
|
|
151
|
+
}
|
|
105
152
|
|
|
106
153
|
case 'duration':
|
|
107
154
|
{
|
|
108
|
-
return getDurationDisplayString(value, formatData
|
|
155
|
+
return getDurationDisplayString(value, formatData);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
case 'custom_currency':
|
|
159
|
+
{
|
|
160
|
+
if (formatData.currency_symbol_position === 'after') {
|
|
161
|
+
return "".concat(_toThousands(value, true, formatData)).concat(formatData.currency_symbol || '');
|
|
162
|
+
} else {
|
|
163
|
+
return "".concat(formatData.currency_symbol || '').concat(_toThousands(value, true, formatData));
|
|
164
|
+
}
|
|
109
165
|
}
|
|
110
166
|
|
|
111
167
|
default:
|