@datarailsshared/dr_renderer 1.2.103-dev → 1.2.105
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/.circleci/config.yml +41 -41
- package/README.md +18 -18
- package/package.json +28 -28
- package/src/dataformatter.js +1033 -1033
- package/src/dr_pivottable.js +2697 -2697
- package/src/highcharts_renderer.js +7749 -7733
- package/src/index.js +27 -27
- package/src/novix_renderer.js +901 -901
- package/src/pivot.css +410 -410
- package/src/pivottable.js +1874 -1874
- package/src/published_items_renderer.js +387 -387
- package/start_run.sh +3 -3
package/src/dataformatter.js
CHANGED
|
@@ -1,1034 +1,1034 @@
|
|
|
1
|
-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
2
|
-
|
|
3
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
-
|
|
5
|
-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
6
|
-
|
|
7
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
8
|
-
|
|
9
|
-
// from https://github.com/fakundo/Excel-Style-Javascript-DataFormatter
|
|
10
|
-
|
|
11
|
-
var defaultLocaleName = 'en-US';
|
|
12
|
-
|
|
13
|
-
var defaultLocaleData = {
|
|
14
|
-
name: 'en-US',
|
|
15
|
-
months: ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'],
|
|
16
|
-
monthsShort: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
|
|
17
|
-
days: ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'],
|
|
18
|
-
daysShort: ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'],
|
|
19
|
-
thousandSeparator: ',',
|
|
20
|
-
decimalSeparator: '.',
|
|
21
|
-
formats: {
|
|
22
|
-
'General Number': '#.#',
|
|
23
|
-
'Currency': '$ #,##0.00;[Red]$ -#,##0.00',
|
|
24
|
-
'Fixed': '0.00',
|
|
25
|
-
'Standard': '#,##0.00',
|
|
26
|
-
'Percent': '0.00%',
|
|
27
|
-
'Scientific': '0.00E+00',
|
|
28
|
-
'Yes/No': '"Yes";"Yes";"No"',
|
|
29
|
-
'True/False': '"True";"True";"False"',
|
|
30
|
-
'On/Off': '"On";"On";"Off"',
|
|
31
|
-
'Short Date': 'mm.dd.yyyy',
|
|
32
|
-
'Long Date': 'dd mmmm yyyy',
|
|
33
|
-
'General Date': 'mm.dd.yyyy h:mm',
|
|
34
|
-
'Medium Date': 'dd.mmm.yy',
|
|
35
|
-
'Long Time': 'hh:mm:ss AM/PM',
|
|
36
|
-
'Short Time': 'h:mm',
|
|
37
|
-
'Medium Time': 'hh:mm AM/PM',
|
|
38
|
-
'mm-dd-yy': 'mm.dd.yy',
|
|
39
|
-
'mm-dd-yyyy': 'mm.dd.yyyy',
|
|
40
|
-
'dd-mmmm-yyyy': 'dd mmmm yyyy',
|
|
41
|
-
'mm-dd-yyyy h:mm': 'mm.dd.yyyy h:mm',
|
|
42
|
-
'dd-mmm-yy': 'dd.mmm.yy'
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
var formatString = function formatString(s) {
|
|
47
|
-
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
48
|
-
args[_key - 1] = arguments[_key];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return s.replace(/{(\d+)}/g, function (match, number) {
|
|
52
|
-
return typeof args[number] != 'undefined' ? args[number] : match;
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
var Code = function () {
|
|
57
|
-
function Code() {
|
|
58
|
-
_classCallCheck(this, Code);
|
|
59
|
-
|
|
60
|
-
this.code = '';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
_createClass(Code, [{
|
|
64
|
-
key: 'makeString',
|
|
65
|
-
value: function makeString(s) {
|
|
66
|
-
for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
67
|
-
values[_key2 - 1] = arguments[_key2];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
values = values.map(JSON.stringify);
|
|
71
|
-
return formatString.apply(undefined, [s].concat(_toConsumableArray(values)));
|
|
72
|
-
}
|
|
73
|
-
}, {
|
|
74
|
-
key: 'append',
|
|
75
|
-
value: function append() {
|
|
76
|
-
this.code += this.makeString.apply(this, arguments);
|
|
77
|
-
}
|
|
78
|
-
}, {
|
|
79
|
-
key: 'toString',
|
|
80
|
-
value: function toString() {
|
|
81
|
-
return this.code;
|
|
82
|
-
}
|
|
83
|
-
}]);
|
|
84
|
-
|
|
85
|
-
return Code;
|
|
86
|
-
}();
|
|
87
|
-
|
|
88
|
-
var DataFormatterImpl = function () {
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Constructor
|
|
92
|
-
* Available options are:
|
|
93
|
-
* debug {boolean} - enable debug mode
|
|
94
|
-
* UTCOffset {number|null} - UTC offset for dates in minutes
|
|
95
|
-
* locale {string}
|
|
96
|
-
* transformCode {function} - code transformer
|
|
97
|
-
* @param {object} options
|
|
98
|
-
*/
|
|
99
|
-
function DataFormatterImpl() {
|
|
100
|
-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
101
|
-
_ref$debug = _ref.debug,
|
|
102
|
-
debug = _ref$debug === undefined ? false : _ref$debug,
|
|
103
|
-
_ref$UTCOffset = _ref.UTCOffset,
|
|
104
|
-
UTCOffset = _ref$UTCOffset === undefined ? null : _ref$UTCOffset,
|
|
105
|
-
_ref$locale = _ref.locale,
|
|
106
|
-
locale = _ref$locale === undefined ? defaultLocaleName : _ref$locale,
|
|
107
|
-
_ref$transformCode = _ref.transformCode,
|
|
108
|
-
transformCode = _ref$transformCode === undefined ? function (code) {
|
|
109
|
-
return code;
|
|
110
|
-
} : _ref$transformCode,
|
|
111
|
-
_ref$locales = _ref.locales,
|
|
112
|
-
locales = _ref$locales === undefined ? [] : _ref$locales;
|
|
113
|
-
|
|
114
|
-
_classCallCheck(this, DataFormatterImpl);
|
|
115
|
-
|
|
116
|
-
this.memoized = {};
|
|
117
|
-
this.debug = debug;
|
|
118
|
-
this.UTCOffset = UTCOffset;
|
|
119
|
-
this.transformCode = transformCode;
|
|
120
|
-
this.zeroDate = this.createDate('1899-12-31T00:00:00.000');
|
|
121
|
-
|
|
122
|
-
// Save defined locales
|
|
123
|
-
this.locales = _defineProperty({}, defaultLocaleName, defaultLocaleData);
|
|
124
|
-
this.defineLocales(locales);
|
|
125
|
-
|
|
126
|
-
// Set default locale
|
|
127
|
-
this.setLocale(locale);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Resets memoized pattern functions
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
_createClass(DataFormatterImpl, [{
|
|
136
|
-
key: 'clearMemoizedFunctions',
|
|
137
|
-
value: function clearMemoizedFunctions() {
|
|
138
|
-
this.memoized = {};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Defines locales
|
|
143
|
-
* @param {array} locales
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
}, {
|
|
147
|
-
key: 'defineLocales',
|
|
148
|
-
value: function defineLocales(locales) {
|
|
149
|
-
var _this = this;
|
|
150
|
-
|
|
151
|
-
locales.forEach(function (locale) {
|
|
152
|
-
return _this.locales[locale.name] = locale;
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Sets locale
|
|
158
|
-
* If locale doesn't exist, sets default
|
|
159
|
-
* @param {string} locale
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
}, {
|
|
163
|
-
key: 'setLocale',
|
|
164
|
-
value: function setLocale(locale) {
|
|
165
|
-
this.locale = this.locales[locale] || this.locales[defaultLocaleName];
|
|
166
|
-
this.clearMemoizedFunctions();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Sets UTC offset for dates
|
|
171
|
-
* @param {number|null} UTCOffset in minutes
|
|
172
|
-
*/
|
|
173
|
-
|
|
174
|
-
}, {
|
|
175
|
-
key: 'setUTCOffset',
|
|
176
|
-
value: function setUTCOffset(UTCOffset) {
|
|
177
|
-
this.UTCOffset = UTCOffset;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Creates new date instance
|
|
182
|
-
*/
|
|
183
|
-
|
|
184
|
-
}, {
|
|
185
|
-
key: 'createDate',
|
|
186
|
-
value: function createDate() {
|
|
187
|
-
var date = new (Function.prototype.bind.apply(Date, [null].concat(Array.prototype.slice.call(arguments))))();
|
|
188
|
-
|
|
189
|
-
if (this.UTCOffset !== null) {
|
|
190
|
-
var clientOffset = date.getTimezoneOffset();
|
|
191
|
-
var newOffset = this.UTCOffset + clientOffset;
|
|
192
|
-
var newOffsetMs = newOffset * 60 * 1000;
|
|
193
|
-
|
|
194
|
-
date.setTime(date.getTime() + newOffsetMs);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return date;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Logger
|
|
202
|
-
*/
|
|
203
|
-
|
|
204
|
-
}, {
|
|
205
|
-
key: 'log',
|
|
206
|
-
value: function log(message) {
|
|
207
|
-
if (this.debug) {
|
|
208
|
-
console.log(message);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Rounds value
|
|
214
|
-
* @param {[type]} n Value to be round
|
|
215
|
-
* @param {[type]} decimals Amount of decimal digits
|
|
216
|
-
* @return {number} Rounded value
|
|
217
|
-
*/
|
|
218
|
-
|
|
219
|
-
}, {
|
|
220
|
-
key: 'roundDecimals',
|
|
221
|
-
value: function roundDecimals(n, decimals) {
|
|
222
|
-
var pow = Math.pow(10, decimals);
|
|
223
|
-
return Math.round(n * pow) / pow;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Greatest common divisor
|
|
228
|
-
*/
|
|
229
|
-
|
|
230
|
-
}, {
|
|
231
|
-
key: 'gcd',
|
|
232
|
-
value: function gcd(a, b) {
|
|
233
|
-
var r = void 0;
|
|
234
|
-
while (b) {
|
|
235
|
-
r = a % b;
|
|
236
|
-
a = b;
|
|
237
|
-
b = r;
|
|
238
|
-
}
|
|
239
|
-
return Math.abs(a);
|
|
240
|
-
}
|
|
241
|
-
}, {
|
|
242
|
-
key: 'applyNumberPattern',
|
|
243
|
-
value: function applyNumberPattern(n, pattern, direction) {
|
|
244
|
-
n = n.toString();
|
|
245
|
-
var s = '';
|
|
246
|
-
|
|
247
|
-
if (direction === 'right') {
|
|
248
|
-
var j = 0;
|
|
249
|
-
var i = -1;
|
|
250
|
-
var patLen = pattern.length;
|
|
251
|
-
|
|
252
|
-
while (++i < patLen) {
|
|
253
|
-
switch (pattern[i]) {
|
|
254
|
-
case '0':
|
|
255
|
-
s += n[j] || '0';
|
|
256
|
-
j++;
|
|
257
|
-
break;
|
|
258
|
-
case '#':
|
|
259
|
-
s += n[j] || '';
|
|
260
|
-
j++;
|
|
261
|
-
break;
|
|
262
|
-
case '?':
|
|
263
|
-
s += n[j] || ' ';
|
|
264
|
-
j++;
|
|
265
|
-
break;
|
|
266
|
-
case '[':
|
|
267
|
-
while (i < pattern.length && pattern[i] !== ']') {
|
|
268
|
-
s += pattern[i];
|
|
269
|
-
i++;
|
|
270
|
-
}
|
|
271
|
-
i--;
|
|
272
|
-
break;
|
|
273
|
-
default:
|
|
274
|
-
s += pattern[i];
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
// Should separate thousands
|
|
279
|
-
var separateThousands = false;
|
|
280
|
-
var mostLeftDigit = void 0;
|
|
281
|
-
|
|
282
|
-
pattern = pattern.replace(/(0|#|\?)(,+?)(0|#|\?)/g, function (a, m1, m2, m3) {
|
|
283
|
-
separateThousands = true;
|
|
284
|
-
return m1 + m3;
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
// Add separation
|
|
288
|
-
if (separateThousands) {
|
|
289
|
-
var _j2 = n.length - 3;
|
|
290
|
-
while (n[0] === '-' ? _j2 > 1 : _j2 > 0) {
|
|
291
|
-
n = n.substr(0, _j2) + this.locale.thousandSeparator + n.substr(_j2);
|
|
292
|
-
_j2 -= 3;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
var _j = n.length - 1;
|
|
297
|
-
var _i = pattern.length;
|
|
298
|
-
|
|
299
|
-
while (_i--) {
|
|
300
|
-
switch (pattern[_i]) {
|
|
301
|
-
case '0':
|
|
302
|
-
if (_j < 0){
|
|
303
|
-
s = s + '0';
|
|
304
|
-
}
|
|
305
|
-
else{
|
|
306
|
-
s = (n[_j] || '0') + s;
|
|
307
|
-
}
|
|
308
|
-
mostLeftDigit = _i;
|
|
309
|
-
_j--;
|
|
310
|
-
break;
|
|
311
|
-
case '#':
|
|
312
|
-
s = (n[_j] || '') + s;
|
|
313
|
-
mostLeftDigit = _i;
|
|
314
|
-
_j--;
|
|
315
|
-
break;
|
|
316
|
-
case '?':
|
|
317
|
-
s = (n[_j] || ' ') + s;
|
|
318
|
-
mostLeftDigit = _i;
|
|
319
|
-
_j--;
|
|
320
|
-
break;
|
|
321
|
-
case ']':
|
|
322
|
-
while (_i > 0 && pattern[_i] !== '[') {
|
|
323
|
-
s = pattern[_i] + s;
|
|
324
|
-
_i--;
|
|
325
|
-
}
|
|
326
|
-
_i++;
|
|
327
|
-
break;
|
|
328
|
-
default:
|
|
329
|
-
s = pattern[_i] + s;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
// Add remaining digits, example: n=1234, ptrn=00, result must be 1234 instead of 34
|
|
333
|
-
if (_j >= 0 && mostLeftDigit !== null) {
|
|
334
|
-
s = s.substr(0, mostLeftDigit) + n.substr(0, _j + 1) + s.substr(mostLeftDigit);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return s;
|
|
339
|
-
}
|
|
340
|
-
}, {
|
|
341
|
-
key: 'restoreOrigins',
|
|
342
|
-
value: function restoreOrigins(value, origins) {
|
|
343
|
-
return value.toString().replace(/\[(?:(\$*?)|(.*?))\]/g, function (a, m1) {
|
|
344
|
-
return m1 && origins[m1.length - 1] || a;
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}, {
|
|
348
|
-
key: 'formatAsNumberDecimal',
|
|
349
|
-
value: function formatAsNumberDecimal(n, decimals, patternIntegerPart, patternDecimalPart) {
|
|
350
|
-
|
|
351
|
-
n = this.roundDecimals(n, decimals).toString().split('.');
|
|
352
|
-
var integerPart = n[0];
|
|
353
|
-
var decimalPart = n[1] || 0;
|
|
354
|
-
|
|
355
|
-
return this.applyNumberPattern(integerPart, patternIntegerPart) + this.locale.decimalSeparator + this.applyNumberPattern(decimalPart, patternDecimalPart, n[1] && n[1][0] == '0' ? '' : 'right');
|
|
356
|
-
}
|
|
357
|
-
}, {
|
|
358
|
-
key: 'formatAsNumberFractial',
|
|
359
|
-
value: function formatAsNumberFractial(n, patternNumeratorPart, patternDenominatorPart) {
|
|
360
|
-
var m = n.toString().split(".");
|
|
361
|
-
m = m[1] ? Math.pow(10, m[1].length) : 1;
|
|
362
|
-
n = Math.floor(n * m);
|
|
363
|
-
|
|
364
|
-
var factor = this.gcd(n, m);
|
|
365
|
-
|
|
366
|
-
return this.applyNumberPattern(n / factor, patternNumeratorPart) + '/' + this.applyNumberPattern(m / factor, patternDenominatorPart);
|
|
367
|
-
}
|
|
368
|
-
}, {
|
|
369
|
-
key: 'formatAsNumberFractialMixed',
|
|
370
|
-
value: function formatAsNumberFractialMixed(n, leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart) {
|
|
371
|
-
var f = 0;
|
|
372
|
-
var c = 1;
|
|
373
|
-
var factor = 1;
|
|
374
|
-
var m = n.toString().split('.');
|
|
375
|
-
|
|
376
|
-
if (m[1]) {
|
|
377
|
-
c = Math.pow(10, m[1].length);
|
|
378
|
-
f = parseInt(m[1]);
|
|
379
|
-
factor = this.gcd(f, c);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
return this.applyNumberPattern(parseInt(n), leftPatternNumeratorPart) + this.applyNumberPattern(f / factor, rightPatternNumeratorPart) + '/' + this.applyNumberPattern(c / factor, patternDenominatorPart);
|
|
383
|
-
}
|
|
384
|
-
}, {
|
|
385
|
-
key: 'formatAsNumberExponential',
|
|
386
|
-
value: function formatAsNumberExponential(n, integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart) {
|
|
387
|
-
|
|
388
|
-
var sign = n < 0 ? -1 : 1;
|
|
389
|
-
var pow = 0;
|
|
390
|
-
|
|
391
|
-
if (n !== 0) {
|
|
392
|
-
|
|
393
|
-
n = Math.abs(n);
|
|
394
|
-
|
|
395
|
-
var integerPartDivision = Math.pow(10, integerPartLength);
|
|
396
|
-
|
|
397
|
-
while (n < integerPartDivision || this.roundDecimals(n, decimalPartLength) < integerPartDivision) {
|
|
398
|
-
n *= 10;
|
|
399
|
-
pow++;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
while (n >= integerPartDivision || this.roundDecimals(n, decimalPartLength) >= integerPartDivision) {
|
|
403
|
-
n /= 10;
|
|
404
|
-
pow--;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
n = this.roundDecimals(n * sign, decimalPartLength).toString().split('.');
|
|
409
|
-
|
|
410
|
-
// Build res
|
|
411
|
-
var res = '';
|
|
412
|
-
|
|
413
|
-
// Integer part
|
|
414
|
-
res += this.applyNumberPattern(parseInt(n[0]), patternIntegerPart);
|
|
415
|
-
|
|
416
|
-
// Decimal part
|
|
417
|
-
if (patternDecimalPart) {
|
|
418
|
-
res += this.locale.decimalSeparator + this.applyNumberPattern(parseInt(n[1] || 0), patternDecimalPart, n[1] && n[1][0] == '0' ? '' : 'right');
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// Pow part
|
|
422
|
-
res += 'E' + (pow > 0 ? '-' : '+') + this.applyNumberPattern(Math.abs(pow), patternPowPart);
|
|
423
|
-
|
|
424
|
-
return res;
|
|
425
|
-
}
|
|
426
|
-
}, {
|
|
427
|
-
key: 'formatAsDateTimeElapsed',
|
|
428
|
-
value: function formatAsDateTimeElapsed(n, foundDays, foundHours, foundMinutes, pattern) {
|
|
429
|
-
var _this2 = this;
|
|
430
|
-
|
|
431
|
-
n = Math.abs(n.getTime() - this.zeroDate.getTime());
|
|
432
|
-
|
|
433
|
-
var seconds = parseInt(n / 1000);
|
|
434
|
-
var minutes = parseInt(seconds / 60);
|
|
435
|
-
var hours = parseInt(minutes / 60);
|
|
436
|
-
var days = parseInt(hours / 24);
|
|
437
|
-
|
|
438
|
-
hours = foundDays ? hours % 24 : hours;
|
|
439
|
-
minutes = foundHours ? minutes % 60 : minutes;
|
|
440
|
-
seconds = foundMinutes ? seconds % 60 : seconds;
|
|
441
|
-
|
|
442
|
-
return pattern.replace(/(dd)|(d)|(hh)|(h)|(mm)|(m)|(ss)|(s)/gi, function (a, dd, d, hh, h, mm, m, ss, s) {
|
|
443
|
-
|
|
444
|
-
if (dd) {
|
|
445
|
-
return _this2.applyNumberPattern(days, '0');
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
if (d) {
|
|
449
|
-
return days;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
if (hh) {
|
|
453
|
-
return _this2.applyNumberPattern(hours, '00');
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
if (h) {
|
|
457
|
-
return hours;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (mm) {
|
|
461
|
-
return _this2.applyNumberPattern(minutes, '0');
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
if (m) {
|
|
465
|
-
return minutes;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
if (ss) {
|
|
469
|
-
return _this2.applyNumberPattern(seconds, '00');
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
if (s) {
|
|
473
|
-
return seconds;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return '';
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
}, {
|
|
480
|
-
key: 'formatAsDateTimeNormal',
|
|
481
|
-
value: function formatAsDateTimeNormal(n, pattern) {
|
|
482
|
-
var _this3 = this;
|
|
483
|
-
|
|
484
|
-
var _locale = this.locale,
|
|
485
|
-
days = _locale.days,
|
|
486
|
-
daysShort = _locale.daysShort,
|
|
487
|
-
months = _locale.months,
|
|
488
|
-
monthsShort = _locale.monthsShort;
|
|
489
|
-
|
|
490
|
-
var foundAMPM = false;
|
|
491
|
-
|
|
492
|
-
var year = n.getFullYear();
|
|
493
|
-
var month = n.getMonth();
|
|
494
|
-
var date = n.getDate();
|
|
495
|
-
var weekDay = n.getDay();
|
|
496
|
-
var hours = n.getHours();
|
|
497
|
-
var minutes = n.getMinutes();
|
|
498
|
-
var seconds = n.getSeconds();
|
|
499
|
-
|
|
500
|
-
var padStartTime = function(time) {
|
|
501
|
-
if (time < 10) {
|
|
502
|
-
return '0' + time;
|
|
503
|
-
}
|
|
504
|
-
return time;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
// Build res
|
|
508
|
-
var res = pattern.replace(/((?:am\/pm)|(?:a\/p))|(?:(h[^ydsap]*?)mm)|(?:mm([^ydh]*?s))|(?:(h[^ydsap]*?)m)|(?:m([^ydh]*?s))/gi, function (a, ampm, fmin, fmin2, mmin, mmin2) {
|
|
509
|
-
|
|
510
|
-
if (ampm) {
|
|
511
|
-
foundAMPM = true;
|
|
512
|
-
return '[]';
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
if (fmin) {
|
|
516
|
-
return fmin + _this3.applyNumberPattern(padStartTime(minutes), '00');
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
if (fmin2) {
|
|
520
|
-
return _this3.applyNumberPattern(padStartTime(minutes), '00') + fmin2;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
if (mmin) {
|
|
524
|
-
return mmin + minutes;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
if (mmin2) {
|
|
528
|
-
return minutes + mmin2;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
return '';
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
return res.replace(/(ss)|(s)|(hh)|(h)|(dddd)|(ddd)|(dd)|(d)|(mmmmm)|(mmmm)|(mmm)|(mm)|(m)|(yyyy)|(yy)|(\[\])/gi, function (a, ss, s, hh, h, dddd, ddd, dd, d, mmmmm, mmmm, mmm, mm, m, yyyy, yy, ampm) {
|
|
535
|
-
|
|
536
|
-
if (ss) {
|
|
537
|
-
return _this3.applyNumberPattern(padStartTime(seconds), '00');
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
if (s) {
|
|
541
|
-
return seconds;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
if (hh) {
|
|
545
|
-
return _this3.applyNumberPattern(padStartTime(foundAMPM ? hours % 12 : hours), '00');
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
if (h) {
|
|
549
|
-
return foundAMPM ? hours % 12 : hours;
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
if (dddd) {
|
|
553
|
-
return days[weekDay];
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
if (ddd) {
|
|
557
|
-
return daysShort[weekDay];
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
if (dd) {
|
|
561
|
-
return _this3.applyNumberPattern(date, '0');
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
if (d) {
|
|
565
|
-
return date;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
if (mmmmm) {
|
|
569
|
-
return monthsShort[month][0];
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
if (mmmm) {
|
|
573
|
-
return months[month];
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
if (mmm) {
|
|
577
|
-
return monthsShort[month];
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
if (mm) {
|
|
581
|
-
return _this3.applyNumberPattern(month + 1, '0');
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
if (m) {
|
|
585
|
-
return month + 1;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
if (yyyy) {
|
|
589
|
-
return year;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
if (yy) {
|
|
593
|
-
return year.toString().substr(2);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
if (ampm) {
|
|
597
|
-
return hours < 12 ? 'AM' : 'PM';
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
return '';
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
}, {
|
|
604
|
-
key: 'createTextCode',
|
|
605
|
-
value: function createTextCode(section) {
|
|
606
|
-
var code = new Code();
|
|
607
|
-
|
|
608
|
-
code.append('\n result.value = {0}.replace(/@/, n);\n ', section);
|
|
609
|
-
|
|
610
|
-
return code.toString();
|
|
611
|
-
}
|
|
612
|
-
}, {
|
|
613
|
-
key: 'createGeneralCode',
|
|
614
|
-
value: function createGeneralCode() {
|
|
615
|
-
var code = new Code();
|
|
616
|
-
var numberCode = this.createNumberCode('#.00');
|
|
617
|
-
var dateTimeCode = this.createDateTimeCode('[d]');
|
|
618
|
-
|
|
619
|
-
code.append('\n if (type === "Number") {\n ' + numberCode + '\n }\n if (type === "DateTime") {\n ' + dateTimeCode + '\n }\n ');
|
|
620
|
-
|
|
621
|
-
return code.toString();
|
|
622
|
-
}
|
|
623
|
-
}, {
|
|
624
|
-
key: 'createNumberExponentialCode',
|
|
625
|
-
value: function createNumberExponentialCode(exponentialMatch) {
|
|
626
|
-
var patternIntegerPart = exponentialMatch[1];
|
|
627
|
-
var patternDecimalPart = exponentialMatch[2];
|
|
628
|
-
var patternPowPart = exponentialMatch[3];
|
|
629
|
-
var code = new Code();
|
|
630
|
-
var integerPartLength = void 0;
|
|
631
|
-
var decimalPartLength = void 0;
|
|
632
|
-
|
|
633
|
-
var zerosCount = function zerosCount(s) {
|
|
634
|
-
return s.match(/0|\?|#/g).length;
|
|
635
|
-
};
|
|
636
|
-
|
|
637
|
-
// Integer part
|
|
638
|
-
if (!patternIntegerPart) {
|
|
639
|
-
patternIntegerPart = '#';
|
|
640
|
-
integerPartLength = 1;
|
|
641
|
-
} else {
|
|
642
|
-
integerPartLength = zerosCount(patternIntegerPart);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// Decimal part
|
|
646
|
-
if (!patternDecimalPart) {
|
|
647
|
-
patternDecimalPart = '';
|
|
648
|
-
decimalPartLength = 0;
|
|
649
|
-
} else {
|
|
650
|
-
decimalPartLength = zerosCount(patternDecimalPart);
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
code.append('\n result.value = this.formatAsNumberExponential(n, {0}, {1}, {2}, {3}, {4});\n ', integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart);
|
|
654
|
-
|
|
655
|
-
return code.toString();
|
|
656
|
-
}
|
|
657
|
-
}, {
|
|
658
|
-
key: 'createNumberFractialCode',
|
|
659
|
-
value: function createNumberFractialCode(fractialMatch) {
|
|
660
|
-
var code = new Code();
|
|
661
|
-
var patternNumeratorPart = fractialMatch[1] || '#';
|
|
662
|
-
var patternDenominatorPart = fractialMatch[2] || '#';
|
|
663
|
-
// TODO watch here
|
|
664
|
-
var zeroPos = patternNumeratorPart.length - 1;
|
|
665
|
-
|
|
666
|
-
while (patternNumeratorPart[zeroPos] === '0' && patternNumeratorPart[zeroPos] !== '?' && patternNumeratorPart[zeroPos] !== '#' && patternNumeratorPart[zeroPos] !== ' ' && zeroPos > 0) {
|
|
667
|
-
zeroPos--;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
var leftPatternNumeratorPart = patternNumeratorPart.substr(0, zeroPos);
|
|
671
|
-
var rightPatternNumeratorPart = patternNumeratorPart.substr(zeroPos);
|
|
672
|
-
|
|
673
|
-
if (!leftPatternNumeratorPart) {
|
|
674
|
-
code.append('\n result.value = this.formatAsNumberFractial(n, {0}, {1});\n ', rightPatternNumeratorPart, patternDenominatorPart);
|
|
675
|
-
}
|
|
676
|
-
// Mixed fraction
|
|
677
|
-
else {
|
|
678
|
-
code.append('\n result.value = this.formatAsNumberFractialMixed(n, {0}, {1}, {2});\n ', leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
return code.toString();
|
|
682
|
-
}
|
|
683
|
-
}, {
|
|
684
|
-
key: 'createNumberDecimalCode',
|
|
685
|
-
value: function createNumberDecimalCode(decimalMatch) {
|
|
686
|
-
var code = new Code();
|
|
687
|
-
var patternIntegerPart = decimalMatch[1] || '0';
|
|
688
|
-
var patternDecimalPart = decimalMatch[2] || '';
|
|
689
|
-
var decimals = void 0;
|
|
690
|
-
var factor = 1;
|
|
691
|
-
|
|
692
|
-
var zerosCount = function zerosCount(s) {
|
|
693
|
-
return s.match(/0|\?|#/g).length;
|
|
694
|
-
};
|
|
695
|
-
|
|
696
|
-
if (!patternDecimalPart) {
|
|
697
|
-
decimals = 0;
|
|
698
|
-
} else {
|
|
699
|
-
decimals = zerosCount(patternDecimalPart);
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
// Spaces before .
|
|
703
|
-
patternIntegerPart = patternIntegerPart.replace(/(0|#|\?)(,+)([^0?#]*)$/, function (a, m1, m2, m3) {
|
|
704
|
-
factor *= Math.pow(1000, m2.length);
|
|
705
|
-
return m1 + m3;
|
|
706
|
-
});
|
|
707
|
-
|
|
708
|
-
if (factor !== 1) {
|
|
709
|
-
code.append('\n n /= {0};\n ', factor);
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
code.append('\n result.value = this.formatAsNumberDecimal(n, {0}, {1}, {2});\n ', decimals, patternIntegerPart, patternDecimalPart);
|
|
713
|
-
|
|
714
|
-
return code.toString();
|
|
715
|
-
}
|
|
716
|
-
}, {
|
|
717
|
-
key: 'createNumberIntegerCode',
|
|
718
|
-
value: function createNumberIntegerCode(section) {
|
|
719
|
-
var code = new Code();
|
|
720
|
-
|
|
721
|
-
code.append('\n n = Math.round(n);\n result.value = this.applyNumberPattern(n, {0});\n ', section);
|
|
722
|
-
|
|
723
|
-
return code.toString();
|
|
724
|
-
}
|
|
725
|
-
}, {
|
|
726
|
-
key: 'createNumberCode',
|
|
727
|
-
value: function createNumberCode(section, shouldAbsNumber) {
|
|
728
|
-
var numberCode = new Code();
|
|
729
|
-
|
|
730
|
-
// Abs
|
|
731
|
-
if (shouldAbsNumber) {
|
|
732
|
-
numberCode.append('\n n = Math.abs(n);\n ');
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
// Exponential form regexp
|
|
736
|
-
var exponentialMatch = section.match(/(.*?)(?:\.(.*?))?e(?:\+|\-)(.*)/i);
|
|
737
|
-
|
|
738
|
-
if (exponentialMatch) {
|
|
739
|
-
|
|
740
|
-
// Exponential form
|
|
741
|
-
numberCode.append(this.createNumberExponentialCode(exponentialMatch));
|
|
742
|
-
} else {
|
|
743
|
-
var factor = 1;
|
|
744
|
-
|
|
745
|
-
// Spaces before end and decimal separator (.)
|
|
746
|
-
//section = section.replace(/(0|#|\?)(\s+)([^0?#]*?)($|\.)/, (a, m1, m2, m3, m4)=> {
|
|
747
|
-
section = section.replace(/(0|#|\?)(,+)([^0?#]*?)($|\.)/, function (a, m1, m2, m3, m4) {
|
|
748
|
-
factor *= Math.pow(1000, m2.length);
|
|
749
|
-
return m1 + m3 + m4;
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
// Percents
|
|
753
|
-
var percentMatch = section.match(/%/g);
|
|
754
|
-
if (percentMatch) {
|
|
755
|
-
factor /= Math.pow(100, percentMatch.length);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
// Factor
|
|
759
|
-
if (factor !== 1) {
|
|
760
|
-
numberCode.append('\n n /= {0};\n ', factor);
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
var fractialMatch = void 0;
|
|
764
|
-
var decimalMatch = void 0;
|
|
765
|
-
|
|
766
|
-
switch (true) {
|
|
767
|
-
|
|
768
|
-
// Fractial form
|
|
769
|
-
case !!(fractialMatch = section.match(/(.*?)\/(.*)/)):
|
|
770
|
-
numberCode.append(this.createNumberFractialCode(fractialMatch));
|
|
771
|
-
break;
|
|
772
|
-
|
|
773
|
-
// Decimal form
|
|
774
|
-
case !!(decimalMatch = section.match(/(.*?)\.(.*)/)):
|
|
775
|
-
numberCode.append(this.createNumberDecimalCode(decimalMatch));
|
|
776
|
-
break;
|
|
777
|
-
|
|
778
|
-
// Integer form
|
|
779
|
-
default:
|
|
780
|
-
numberCode.append(this.createNumberIntegerCode(section));
|
|
781
|
-
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
// Final code
|
|
786
|
-
var code = new Code();
|
|
787
|
-
|
|
788
|
-
// Parse to float
|
|
789
|
-
code.append('\n n = parseFloat(n);\n ');
|
|
790
|
-
|
|
791
|
-
// Checks
|
|
792
|
-
code.append('\n if (!isNaN(n)) {\n if (n >= 1e21 || n <= -1e21) {\n result.value = n.toString().toUpperCase();\n }\n else {\n ' + numberCode + '\n }\n }\n ');
|
|
793
|
-
|
|
794
|
-
return code.toString();
|
|
795
|
-
}
|
|
796
|
-
}, {
|
|
797
|
-
key: 'createDateTimeElapsedCode',
|
|
798
|
-
value: function createDateTimeElapsedCode(section) {
|
|
799
|
-
var code = new Code();
|
|
800
|
-
|
|
801
|
-
var foundDays = /d/i.test(section);
|
|
802
|
-
var foundHours = /h/i.test(section);
|
|
803
|
-
var foundMinutes = /m/i.test(section);
|
|
804
|
-
|
|
805
|
-
code.append('\n result.value = this.formatAsDateTimeElapsed(n, {0}, {1}, {2}, {3});\n ', foundDays, foundHours, foundMinutes, section);
|
|
806
|
-
|
|
807
|
-
return code.toString();
|
|
808
|
-
}
|
|
809
|
-
}, {
|
|
810
|
-
key: 'createDateTimeNormalCode',
|
|
811
|
-
value: function createDateTimeNormalCode(section) {
|
|
812
|
-
var code = new Code();
|
|
813
|
-
|
|
814
|
-
code.append('\n result.value = this.formatAsDateTimeNormal(n, {0});\n ', section);
|
|
815
|
-
|
|
816
|
-
return code.toString();
|
|
817
|
-
}
|
|
818
|
-
}, {
|
|
819
|
-
key: 'createDateTimeCode',
|
|
820
|
-
value: function createDateTimeCode(section) {
|
|
821
|
-
var code = new Code();
|
|
822
|
-
var elapsed = false;
|
|
823
|
-
|
|
824
|
-
section = section.replace(/\[(h+?|m+?|s+?|y+?|d+?)]/ig, function (a, m1) {
|
|
825
|
-
elapsed = true;
|
|
826
|
-
return m1;
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
var dateTimeCode = elapsed ? this.createDateTimeElapsedCode(section) : this.createDateTimeNormalCode(section);
|
|
830
|
-
|
|
831
|
-
code.append('\n n = this.createDate(n);\n if (!isNaN(n.getTime())) {\n ' + dateTimeCode + '\n }\n ');
|
|
832
|
-
|
|
833
|
-
return code.toString();
|
|
834
|
-
}
|
|
835
|
-
}, {
|
|
836
|
-
key: 'createSectionCode',
|
|
837
|
-
value: function createSectionCode(section, sectionIndex, sectionsCount) {
|
|
838
|
-
// Start creating code for function
|
|
839
|
-
var code = new Code();
|
|
840
|
-
|
|
841
|
-
var condition = void 0;
|
|
842
|
-
var shouldAbsNumber = false;
|
|
843
|
-
|
|
844
|
-
// Find condition for sector or add standard sector condition (positive number, negative number, etc.)
|
|
845
|
-
var conditionMatch = section.match(/\[((?:>|>=|<|<=|=|<>)[0-9\.]+?)]/);
|
|
846
|
-
|
|
847
|
-
switch (true) {
|
|
848
|
-
|
|
849
|
-
// Found condition
|
|
850
|
-
case !!conditionMatch:
|
|
851
|
-
var cond = conditionMatch[1].replace(/<>/, '!=').replace('/=/', '==');
|
|
852
|
-
condition = 'type == "Number" && parseFloat(n)' + cond;
|
|
853
|
-
break;
|
|
854
|
-
|
|
855
|
-
// Standard condition for first section of 3+
|
|
856
|
-
case sectionIndex === 0 && sectionsCount > 2:
|
|
857
|
-
condition = 'type == "Number" && parseFloat(n) > 0';
|
|
858
|
-
break;
|
|
859
|
-
|
|
860
|
-
// Standard condition for first section of 2
|
|
861
|
-
case sectionIndex === 0 && sectionsCount === 2:
|
|
862
|
-
condition = 'type == "Number" && parseFloat(n) >= 0';
|
|
863
|
-
break;
|
|
864
|
-
|
|
865
|
-
// Standard condition for negative number
|
|
866
|
-
case sectionIndex === 1:
|
|
867
|
-
condition = 'type == "Number" && parseFloat(n) < 0';
|
|
868
|
-
shouldAbsNumber = true;
|
|
869
|
-
break;
|
|
870
|
-
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
// Find text color
|
|
874
|
-
section = section.replace(/\[(Red|Green|White|Blue|Magenta|Yellow|Cyan|Black)]/gi, function (a, m1) {
|
|
875
|
-
code.append('\n result.color = {0};\n ', m1);
|
|
876
|
-
return '';
|
|
877
|
-
});
|
|
878
|
-
|
|
879
|
-
// Remove all [], except our replacements and elapsed days, hours, minutes, seconds
|
|
880
|
-
section = section.replace(/(\[((?!((\$*?)|(d*?)|(h*?)|(m*?)|(s*?))]).*?)])/, '');
|
|
881
|
-
|
|
882
|
-
// Format code
|
|
883
|
-
var formatCode = new Code();
|
|
884
|
-
|
|
885
|
-
// Defaults
|
|
886
|
-
formatCode.append('\n result.value = {0};\n result.pattern = {0};\n ', section);
|
|
887
|
-
|
|
888
|
-
switch (true) {
|
|
889
|
-
|
|
890
|
-
// General format
|
|
891
|
-
case /General/i.test(section):
|
|
892
|
-
formatCode.append(this.createGeneralCode(section));
|
|
893
|
-
break;
|
|
894
|
-
|
|
895
|
-
// Text
|
|
896
|
-
case /@/.test(section):
|
|
897
|
-
formatCode.append(this.createTextCode(section));
|
|
898
|
-
break;
|
|
899
|
-
|
|
900
|
-
// Number
|
|
901
|
-
case /#|\?|0/.test(section):
|
|
902
|
-
if (!condition) {
|
|
903
|
-
condition = 'type === "Number"';
|
|
904
|
-
}
|
|
905
|
-
formatCode.append(this.createNumberCode(section, shouldAbsNumber));
|
|
906
|
-
break;
|
|
907
|
-
|
|
908
|
-
// DateTime
|
|
909
|
-
case /h|m|s|y|d/i.test(section):
|
|
910
|
-
if (!condition) {
|
|
911
|
-
condition = 'type === "DateTime"';
|
|
912
|
-
}
|
|
913
|
-
formatCode.append(this.createDateTimeCode(section));
|
|
914
|
-
break;
|
|
915
|
-
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
// Add return statement
|
|
919
|
-
formatCode.append('\n return makeResult.call(this);\n ');
|
|
920
|
-
|
|
921
|
-
// Build final section code
|
|
922
|
-
if (condition) {
|
|
923
|
-
code.append('\n // Section\n if (' + condition + ') {\n ' + formatCode + '\n }\n // End section\n ');
|
|
924
|
-
} else {
|
|
925
|
-
code.append('\n // Section\n ' + formatCode + '\n // End section\n ');
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
return code.toString();
|
|
929
|
-
}
|
|
930
|
-
}, {
|
|
931
|
-
key: 'createPatternCode',
|
|
932
|
-
value: function createPatternCode(pattern) {
|
|
933
|
-
var _this4 = this;
|
|
934
|
-
|
|
935
|
-
var origins = [];
|
|
936
|
-
var replaces = '';
|
|
937
|
-
|
|
938
|
-
// Find quotes, slash symbols
|
|
939
|
-
var patternReplaced = pattern.replace(/"([^"]+)"|\\(.?)|(_.?)|(\*.?)|(")/g, function (a, m1, m2, m3) {
|
|
940
|
-
// Quote found
|
|
941
|
-
if (m1) {
|
|
942
|
-
origins.push(m1.replace(/("|'|\\)/g, "\\$1"));
|
|
943
|
-
return '[' + (replaces += '$') + ']';
|
|
944
|
-
}
|
|
945
|
-
// Slash found
|
|
946
|
-
if (m2) {
|
|
947
|
-
origins.push(m2.replace(/("|'|\\)/g, "\\$1"));
|
|
948
|
-
return '[' + (replaces += '$') + ']';
|
|
949
|
-
}
|
|
950
|
-
// Space found
|
|
951
|
-
if (m3) {
|
|
952
|
-
origins.push(' ');
|
|
953
|
-
return '[' + (replaces += '$') + ']';
|
|
954
|
-
}
|
|
955
|
-
return '';
|
|
956
|
-
});
|
|
957
|
-
|
|
958
|
-
// Split pattern to sections
|
|
959
|
-
var sections = patternReplaced.split(/;/);
|
|
960
|
-
|
|
961
|
-
// Init code
|
|
962
|
-
var code = new Code();
|
|
963
|
-
|
|
964
|
-
// Start variables
|
|
965
|
-
code.append('\n var result = {\n value: "",\n align: type === "Number" || type === "DateTime" ? "right" : "",\n color: "",\n pattern: ""\n };\n function makeResult() {\n var origins = {0};\n result.value = this.restoreOrigins(result.value, origins);\n result.pattern = this.restoreOrigins(result.pattern, origins);\n return result;\n };\n ', origins);
|
|
966
|
-
|
|
967
|
-
// Remove unnesessary sections
|
|
968
|
-
sections = sections.slice(0, 4);
|
|
969
|
-
|
|
970
|
-
// Loop trough sections
|
|
971
|
-
sections.forEach(function (section, sectionIndex) {
|
|
972
|
-
return code.append(_this4.createSectionCode(section, sectionIndex, sections.length));
|
|
973
|
-
});
|
|
974
|
-
|
|
975
|
-
// Return statement
|
|
976
|
-
code.append('\n result.value = {0};\n result.pattern = {0};\n return makeResult.call(this);\n ', patternReplaced);
|
|
977
|
-
|
|
978
|
-
return code.toString();
|
|
979
|
-
}
|
|
980
|
-
}, {
|
|
981
|
-
key: 'format',
|
|
982
|
-
value: function format(n, type, pattern) {
|
|
983
|
-
this.log(`Input:`);
|
|
984
|
-
this.log(`n = ${n}, type = ${type}, pattern = ${pattern}`);
|
|
985
|
-
var result=n
|
|
986
|
-
n = n.toString();
|
|
987
|
-
pattern = pattern.toString();
|
|
988
|
-
//pattern=pattern.replace(/"/g, "'");
|
|
989
|
-
// Find predefined format
|
|
990
|
-
if (this.locale.formats[pattern]) {
|
|
991
|
-
pattern = this.locale.formats[pattern];
|
|
992
|
-
}
|
|
993
|
-
try{
|
|
994
|
-
// Create function
|
|
995
|
-
if (!this.memoized[pattern]) {
|
|
996
|
-
|
|
997
|
-
let code = this.createPatternCode(pattern);
|
|
998
|
-
|
|
999
|
-
// Transform code
|
|
1000
|
-
code = this.transformCode(code);
|
|
1001
|
-
|
|
1002
|
-
// Memoize function
|
|
1003
|
-
this.memoized[pattern] = Function('n', 'type', code);
|
|
1004
|
-
|
|
1005
|
-
// Log code
|
|
1006
|
-
this.log('Code:');
|
|
1007
|
-
this.log(code);
|
|
1008
|
-
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
// Call function
|
|
1012
|
-
result = this.memoized[pattern].call(this, n, type);
|
|
1013
|
-
|
|
1014
|
-
if (result.value === result.pattern && !(String(n) === '0' && result.value.includes('0'))) {
|
|
1015
|
-
result.value = n;
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
catch (e) {
|
|
1019
|
-
|
|
1020
|
-
this.log(`Input:`,e);
|
|
1021
|
-
|
|
1022
|
-
}
|
|
1023
|
-
// Log result
|
|
1024
|
-
this.log('Result:');
|
|
1025
|
-
this.log(result);
|
|
1026
|
-
|
|
1027
|
-
return result;
|
|
1028
|
-
}
|
|
1029
|
-
}]);
|
|
1030
|
-
|
|
1031
|
-
return DataFormatterImpl;
|
|
1032
|
-
}();
|
|
1033
|
-
|
|
1
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
2
|
+
|
|
3
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
+
|
|
5
|
+
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
6
|
+
|
|
7
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
8
|
+
|
|
9
|
+
// from https://github.com/fakundo/Excel-Style-Javascript-DataFormatter
|
|
10
|
+
|
|
11
|
+
var defaultLocaleName = 'en-US';
|
|
12
|
+
|
|
13
|
+
var defaultLocaleData = {
|
|
14
|
+
name: 'en-US',
|
|
15
|
+
months: ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'],
|
|
16
|
+
monthsShort: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
|
|
17
|
+
days: ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'],
|
|
18
|
+
daysShort: ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'],
|
|
19
|
+
thousandSeparator: ',',
|
|
20
|
+
decimalSeparator: '.',
|
|
21
|
+
formats: {
|
|
22
|
+
'General Number': '#.#',
|
|
23
|
+
'Currency': '$ #,##0.00;[Red]$ -#,##0.00',
|
|
24
|
+
'Fixed': '0.00',
|
|
25
|
+
'Standard': '#,##0.00',
|
|
26
|
+
'Percent': '0.00%',
|
|
27
|
+
'Scientific': '0.00E+00',
|
|
28
|
+
'Yes/No': '"Yes";"Yes";"No"',
|
|
29
|
+
'True/False': '"True";"True";"False"',
|
|
30
|
+
'On/Off': '"On";"On";"Off"',
|
|
31
|
+
'Short Date': 'mm.dd.yyyy',
|
|
32
|
+
'Long Date': 'dd mmmm yyyy',
|
|
33
|
+
'General Date': 'mm.dd.yyyy h:mm',
|
|
34
|
+
'Medium Date': 'dd.mmm.yy',
|
|
35
|
+
'Long Time': 'hh:mm:ss AM/PM',
|
|
36
|
+
'Short Time': 'h:mm',
|
|
37
|
+
'Medium Time': 'hh:mm AM/PM',
|
|
38
|
+
'mm-dd-yy': 'mm.dd.yy',
|
|
39
|
+
'mm-dd-yyyy': 'mm.dd.yyyy',
|
|
40
|
+
'dd-mmmm-yyyy': 'dd mmmm yyyy',
|
|
41
|
+
'mm-dd-yyyy h:mm': 'mm.dd.yyyy h:mm',
|
|
42
|
+
'dd-mmm-yy': 'dd.mmm.yy'
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
var formatString = function formatString(s) {
|
|
47
|
+
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
48
|
+
args[_key - 1] = arguments[_key];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return s.replace(/{(\d+)}/g, function (match, number) {
|
|
52
|
+
return typeof args[number] != 'undefined' ? args[number] : match;
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
var Code = function () {
|
|
57
|
+
function Code() {
|
|
58
|
+
_classCallCheck(this, Code);
|
|
59
|
+
|
|
60
|
+
this.code = '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_createClass(Code, [{
|
|
64
|
+
key: 'makeString',
|
|
65
|
+
value: function makeString(s) {
|
|
66
|
+
for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
67
|
+
values[_key2 - 1] = arguments[_key2];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
values = values.map(JSON.stringify);
|
|
71
|
+
return formatString.apply(undefined, [s].concat(_toConsumableArray(values)));
|
|
72
|
+
}
|
|
73
|
+
}, {
|
|
74
|
+
key: 'append',
|
|
75
|
+
value: function append() {
|
|
76
|
+
this.code += this.makeString.apply(this, arguments);
|
|
77
|
+
}
|
|
78
|
+
}, {
|
|
79
|
+
key: 'toString',
|
|
80
|
+
value: function toString() {
|
|
81
|
+
return this.code;
|
|
82
|
+
}
|
|
83
|
+
}]);
|
|
84
|
+
|
|
85
|
+
return Code;
|
|
86
|
+
}();
|
|
87
|
+
|
|
88
|
+
var DataFormatterImpl = function () {
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Constructor
|
|
92
|
+
* Available options are:
|
|
93
|
+
* debug {boolean} - enable debug mode
|
|
94
|
+
* UTCOffset {number|null} - UTC offset for dates in minutes
|
|
95
|
+
* locale {string}
|
|
96
|
+
* transformCode {function} - code transformer
|
|
97
|
+
* @param {object} options
|
|
98
|
+
*/
|
|
99
|
+
function DataFormatterImpl() {
|
|
100
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
101
|
+
_ref$debug = _ref.debug,
|
|
102
|
+
debug = _ref$debug === undefined ? false : _ref$debug,
|
|
103
|
+
_ref$UTCOffset = _ref.UTCOffset,
|
|
104
|
+
UTCOffset = _ref$UTCOffset === undefined ? null : _ref$UTCOffset,
|
|
105
|
+
_ref$locale = _ref.locale,
|
|
106
|
+
locale = _ref$locale === undefined ? defaultLocaleName : _ref$locale,
|
|
107
|
+
_ref$transformCode = _ref.transformCode,
|
|
108
|
+
transformCode = _ref$transformCode === undefined ? function (code) {
|
|
109
|
+
return code;
|
|
110
|
+
} : _ref$transformCode,
|
|
111
|
+
_ref$locales = _ref.locales,
|
|
112
|
+
locales = _ref$locales === undefined ? [] : _ref$locales;
|
|
113
|
+
|
|
114
|
+
_classCallCheck(this, DataFormatterImpl);
|
|
115
|
+
|
|
116
|
+
this.memoized = {};
|
|
117
|
+
this.debug = debug;
|
|
118
|
+
this.UTCOffset = UTCOffset;
|
|
119
|
+
this.transformCode = transformCode;
|
|
120
|
+
this.zeroDate = this.createDate('1899-12-31T00:00:00.000');
|
|
121
|
+
|
|
122
|
+
// Save defined locales
|
|
123
|
+
this.locales = _defineProperty({}, defaultLocaleName, defaultLocaleData);
|
|
124
|
+
this.defineLocales(locales);
|
|
125
|
+
|
|
126
|
+
// Set default locale
|
|
127
|
+
this.setLocale(locale);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Resets memoized pattern functions
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
_createClass(DataFormatterImpl, [{
|
|
136
|
+
key: 'clearMemoizedFunctions',
|
|
137
|
+
value: function clearMemoizedFunctions() {
|
|
138
|
+
this.memoized = {};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Defines locales
|
|
143
|
+
* @param {array} locales
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
}, {
|
|
147
|
+
key: 'defineLocales',
|
|
148
|
+
value: function defineLocales(locales) {
|
|
149
|
+
var _this = this;
|
|
150
|
+
|
|
151
|
+
locales.forEach(function (locale) {
|
|
152
|
+
return _this.locales[locale.name] = locale;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Sets locale
|
|
158
|
+
* If locale doesn't exist, sets default
|
|
159
|
+
* @param {string} locale
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
}, {
|
|
163
|
+
key: 'setLocale',
|
|
164
|
+
value: function setLocale(locale) {
|
|
165
|
+
this.locale = this.locales[locale] || this.locales[defaultLocaleName];
|
|
166
|
+
this.clearMemoizedFunctions();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sets UTC offset for dates
|
|
171
|
+
* @param {number|null} UTCOffset in minutes
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
}, {
|
|
175
|
+
key: 'setUTCOffset',
|
|
176
|
+
value: function setUTCOffset(UTCOffset) {
|
|
177
|
+
this.UTCOffset = UTCOffset;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates new date instance
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
}, {
|
|
185
|
+
key: 'createDate',
|
|
186
|
+
value: function createDate() {
|
|
187
|
+
var date = new (Function.prototype.bind.apply(Date, [null].concat(Array.prototype.slice.call(arguments))))();
|
|
188
|
+
|
|
189
|
+
if (this.UTCOffset !== null) {
|
|
190
|
+
var clientOffset = date.getTimezoneOffset();
|
|
191
|
+
var newOffset = this.UTCOffset + clientOffset;
|
|
192
|
+
var newOffsetMs = newOffset * 60 * 1000;
|
|
193
|
+
|
|
194
|
+
date.setTime(date.getTime() + newOffsetMs);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return date;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Logger
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
}, {
|
|
205
|
+
key: 'log',
|
|
206
|
+
value: function log(message) {
|
|
207
|
+
if (this.debug) {
|
|
208
|
+
console.log(message);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Rounds value
|
|
214
|
+
* @param {[type]} n Value to be round
|
|
215
|
+
* @param {[type]} decimals Amount of decimal digits
|
|
216
|
+
* @return {number} Rounded value
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
}, {
|
|
220
|
+
key: 'roundDecimals',
|
|
221
|
+
value: function roundDecimals(n, decimals) {
|
|
222
|
+
var pow = Math.pow(10, decimals);
|
|
223
|
+
return Math.round(n * pow) / pow;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Greatest common divisor
|
|
228
|
+
*/
|
|
229
|
+
|
|
230
|
+
}, {
|
|
231
|
+
key: 'gcd',
|
|
232
|
+
value: function gcd(a, b) {
|
|
233
|
+
var r = void 0;
|
|
234
|
+
while (b) {
|
|
235
|
+
r = a % b;
|
|
236
|
+
a = b;
|
|
237
|
+
b = r;
|
|
238
|
+
}
|
|
239
|
+
return Math.abs(a);
|
|
240
|
+
}
|
|
241
|
+
}, {
|
|
242
|
+
key: 'applyNumberPattern',
|
|
243
|
+
value: function applyNumberPattern(n, pattern, direction) {
|
|
244
|
+
n = n.toString();
|
|
245
|
+
var s = '';
|
|
246
|
+
|
|
247
|
+
if (direction === 'right') {
|
|
248
|
+
var j = 0;
|
|
249
|
+
var i = -1;
|
|
250
|
+
var patLen = pattern.length;
|
|
251
|
+
|
|
252
|
+
while (++i < patLen) {
|
|
253
|
+
switch (pattern[i]) {
|
|
254
|
+
case '0':
|
|
255
|
+
s += n[j] || '0';
|
|
256
|
+
j++;
|
|
257
|
+
break;
|
|
258
|
+
case '#':
|
|
259
|
+
s += n[j] || '';
|
|
260
|
+
j++;
|
|
261
|
+
break;
|
|
262
|
+
case '?':
|
|
263
|
+
s += n[j] || ' ';
|
|
264
|
+
j++;
|
|
265
|
+
break;
|
|
266
|
+
case '[':
|
|
267
|
+
while (i < pattern.length && pattern[i] !== ']') {
|
|
268
|
+
s += pattern[i];
|
|
269
|
+
i++;
|
|
270
|
+
}
|
|
271
|
+
i--;
|
|
272
|
+
break;
|
|
273
|
+
default:
|
|
274
|
+
s += pattern[i];
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
// Should separate thousands
|
|
279
|
+
var separateThousands = false;
|
|
280
|
+
var mostLeftDigit = void 0;
|
|
281
|
+
|
|
282
|
+
pattern = pattern.replace(/(0|#|\?)(,+?)(0|#|\?)/g, function (a, m1, m2, m3) {
|
|
283
|
+
separateThousands = true;
|
|
284
|
+
return m1 + m3;
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// Add separation
|
|
288
|
+
if (separateThousands) {
|
|
289
|
+
var _j2 = n.length - 3;
|
|
290
|
+
while (n[0] === '-' ? _j2 > 1 : _j2 > 0) {
|
|
291
|
+
n = n.substr(0, _j2) + this.locale.thousandSeparator + n.substr(_j2);
|
|
292
|
+
_j2 -= 3;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
var _j = n.length - 1;
|
|
297
|
+
var _i = pattern.length;
|
|
298
|
+
|
|
299
|
+
while (_i--) {
|
|
300
|
+
switch (pattern[_i]) {
|
|
301
|
+
case '0':
|
|
302
|
+
if (_j < 0){
|
|
303
|
+
s = s + '0';
|
|
304
|
+
}
|
|
305
|
+
else{
|
|
306
|
+
s = (n[_j] || '0') + s;
|
|
307
|
+
}
|
|
308
|
+
mostLeftDigit = _i;
|
|
309
|
+
_j--;
|
|
310
|
+
break;
|
|
311
|
+
case '#':
|
|
312
|
+
s = (n[_j] || '') + s;
|
|
313
|
+
mostLeftDigit = _i;
|
|
314
|
+
_j--;
|
|
315
|
+
break;
|
|
316
|
+
case '?':
|
|
317
|
+
s = (n[_j] || ' ') + s;
|
|
318
|
+
mostLeftDigit = _i;
|
|
319
|
+
_j--;
|
|
320
|
+
break;
|
|
321
|
+
case ']':
|
|
322
|
+
while (_i > 0 && pattern[_i] !== '[') {
|
|
323
|
+
s = pattern[_i] + s;
|
|
324
|
+
_i--;
|
|
325
|
+
}
|
|
326
|
+
_i++;
|
|
327
|
+
break;
|
|
328
|
+
default:
|
|
329
|
+
s = pattern[_i] + s;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// Add remaining digits, example: n=1234, ptrn=00, result must be 1234 instead of 34
|
|
333
|
+
if (_j >= 0 && mostLeftDigit !== null) {
|
|
334
|
+
s = s.substr(0, mostLeftDigit) + n.substr(0, _j + 1) + s.substr(mostLeftDigit);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return s;
|
|
339
|
+
}
|
|
340
|
+
}, {
|
|
341
|
+
key: 'restoreOrigins',
|
|
342
|
+
value: function restoreOrigins(value, origins) {
|
|
343
|
+
return value.toString().replace(/\[(?:(\$*?)|(.*?))\]/g, function (a, m1) {
|
|
344
|
+
return m1 && origins[m1.length - 1] || a;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}, {
|
|
348
|
+
key: 'formatAsNumberDecimal',
|
|
349
|
+
value: function formatAsNumberDecimal(n, decimals, patternIntegerPart, patternDecimalPart) {
|
|
350
|
+
|
|
351
|
+
n = this.roundDecimals(n, decimals).toString().split('.');
|
|
352
|
+
var integerPart = n[0];
|
|
353
|
+
var decimalPart = n[1] || 0;
|
|
354
|
+
|
|
355
|
+
return this.applyNumberPattern(integerPart, patternIntegerPart) + this.locale.decimalSeparator + this.applyNumberPattern(decimalPart, patternDecimalPart, n[1] && n[1][0] == '0' ? '' : 'right');
|
|
356
|
+
}
|
|
357
|
+
}, {
|
|
358
|
+
key: 'formatAsNumberFractial',
|
|
359
|
+
value: function formatAsNumberFractial(n, patternNumeratorPart, patternDenominatorPart) {
|
|
360
|
+
var m = n.toString().split(".");
|
|
361
|
+
m = m[1] ? Math.pow(10, m[1].length) : 1;
|
|
362
|
+
n = Math.floor(n * m);
|
|
363
|
+
|
|
364
|
+
var factor = this.gcd(n, m);
|
|
365
|
+
|
|
366
|
+
return this.applyNumberPattern(n / factor, patternNumeratorPart) + '/' + this.applyNumberPattern(m / factor, patternDenominatorPart);
|
|
367
|
+
}
|
|
368
|
+
}, {
|
|
369
|
+
key: 'formatAsNumberFractialMixed',
|
|
370
|
+
value: function formatAsNumberFractialMixed(n, leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart) {
|
|
371
|
+
var f = 0;
|
|
372
|
+
var c = 1;
|
|
373
|
+
var factor = 1;
|
|
374
|
+
var m = n.toString().split('.');
|
|
375
|
+
|
|
376
|
+
if (m[1]) {
|
|
377
|
+
c = Math.pow(10, m[1].length);
|
|
378
|
+
f = parseInt(m[1]);
|
|
379
|
+
factor = this.gcd(f, c);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return this.applyNumberPattern(parseInt(n), leftPatternNumeratorPart) + this.applyNumberPattern(f / factor, rightPatternNumeratorPart) + '/' + this.applyNumberPattern(c / factor, patternDenominatorPart);
|
|
383
|
+
}
|
|
384
|
+
}, {
|
|
385
|
+
key: 'formatAsNumberExponential',
|
|
386
|
+
value: function formatAsNumberExponential(n, integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart) {
|
|
387
|
+
|
|
388
|
+
var sign = n < 0 ? -1 : 1;
|
|
389
|
+
var pow = 0;
|
|
390
|
+
|
|
391
|
+
if (n !== 0) {
|
|
392
|
+
|
|
393
|
+
n = Math.abs(n);
|
|
394
|
+
|
|
395
|
+
var integerPartDivision = Math.pow(10, integerPartLength);
|
|
396
|
+
|
|
397
|
+
while (n < integerPartDivision || this.roundDecimals(n, decimalPartLength) < integerPartDivision) {
|
|
398
|
+
n *= 10;
|
|
399
|
+
pow++;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
while (n >= integerPartDivision || this.roundDecimals(n, decimalPartLength) >= integerPartDivision) {
|
|
403
|
+
n /= 10;
|
|
404
|
+
pow--;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
n = this.roundDecimals(n * sign, decimalPartLength).toString().split('.');
|
|
409
|
+
|
|
410
|
+
// Build res
|
|
411
|
+
var res = '';
|
|
412
|
+
|
|
413
|
+
// Integer part
|
|
414
|
+
res += this.applyNumberPattern(parseInt(n[0]), patternIntegerPart);
|
|
415
|
+
|
|
416
|
+
// Decimal part
|
|
417
|
+
if (patternDecimalPart) {
|
|
418
|
+
res += this.locale.decimalSeparator + this.applyNumberPattern(parseInt(n[1] || 0), patternDecimalPart, n[1] && n[1][0] == '0' ? '' : 'right');
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Pow part
|
|
422
|
+
res += 'E' + (pow > 0 ? '-' : '+') + this.applyNumberPattern(Math.abs(pow), patternPowPart);
|
|
423
|
+
|
|
424
|
+
return res;
|
|
425
|
+
}
|
|
426
|
+
}, {
|
|
427
|
+
key: 'formatAsDateTimeElapsed',
|
|
428
|
+
value: function formatAsDateTimeElapsed(n, foundDays, foundHours, foundMinutes, pattern) {
|
|
429
|
+
var _this2 = this;
|
|
430
|
+
|
|
431
|
+
n = Math.abs(n.getTime() - this.zeroDate.getTime());
|
|
432
|
+
|
|
433
|
+
var seconds = parseInt(n / 1000);
|
|
434
|
+
var minutes = parseInt(seconds / 60);
|
|
435
|
+
var hours = parseInt(minutes / 60);
|
|
436
|
+
var days = parseInt(hours / 24);
|
|
437
|
+
|
|
438
|
+
hours = foundDays ? hours % 24 : hours;
|
|
439
|
+
minutes = foundHours ? minutes % 60 : minutes;
|
|
440
|
+
seconds = foundMinutes ? seconds % 60 : seconds;
|
|
441
|
+
|
|
442
|
+
return pattern.replace(/(dd)|(d)|(hh)|(h)|(mm)|(m)|(ss)|(s)/gi, function (a, dd, d, hh, h, mm, m, ss, s) {
|
|
443
|
+
|
|
444
|
+
if (dd) {
|
|
445
|
+
return _this2.applyNumberPattern(days, '0');
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (d) {
|
|
449
|
+
return days;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (hh) {
|
|
453
|
+
return _this2.applyNumberPattern(hours, '00');
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (h) {
|
|
457
|
+
return hours;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (mm) {
|
|
461
|
+
return _this2.applyNumberPattern(minutes, '0');
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (m) {
|
|
465
|
+
return minutes;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (ss) {
|
|
469
|
+
return _this2.applyNumberPattern(seconds, '00');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (s) {
|
|
473
|
+
return seconds;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return '';
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}, {
|
|
480
|
+
key: 'formatAsDateTimeNormal',
|
|
481
|
+
value: function formatAsDateTimeNormal(n, pattern) {
|
|
482
|
+
var _this3 = this;
|
|
483
|
+
|
|
484
|
+
var _locale = this.locale,
|
|
485
|
+
days = _locale.days,
|
|
486
|
+
daysShort = _locale.daysShort,
|
|
487
|
+
months = _locale.months,
|
|
488
|
+
monthsShort = _locale.monthsShort;
|
|
489
|
+
|
|
490
|
+
var foundAMPM = false;
|
|
491
|
+
|
|
492
|
+
var year = n.getFullYear();
|
|
493
|
+
var month = n.getMonth();
|
|
494
|
+
var date = n.getDate();
|
|
495
|
+
var weekDay = n.getDay();
|
|
496
|
+
var hours = n.getHours();
|
|
497
|
+
var minutes = n.getMinutes();
|
|
498
|
+
var seconds = n.getSeconds();
|
|
499
|
+
|
|
500
|
+
var padStartTime = function(time) {
|
|
501
|
+
if (time < 10) {
|
|
502
|
+
return '0' + time;
|
|
503
|
+
}
|
|
504
|
+
return time;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// Build res
|
|
508
|
+
var res = pattern.replace(/((?:am\/pm)|(?:a\/p))|(?:(h[^ydsap]*?)mm)|(?:mm([^ydh]*?s))|(?:(h[^ydsap]*?)m)|(?:m([^ydh]*?s))/gi, function (a, ampm, fmin, fmin2, mmin, mmin2) {
|
|
509
|
+
|
|
510
|
+
if (ampm) {
|
|
511
|
+
foundAMPM = true;
|
|
512
|
+
return '[]';
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (fmin) {
|
|
516
|
+
return fmin + _this3.applyNumberPattern(padStartTime(minutes), '00');
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (fmin2) {
|
|
520
|
+
return _this3.applyNumberPattern(padStartTime(minutes), '00') + fmin2;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (mmin) {
|
|
524
|
+
return mmin + minutes;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (mmin2) {
|
|
528
|
+
return minutes + mmin2;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return '';
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
return res.replace(/(ss)|(s)|(hh)|(h)|(dddd)|(ddd)|(dd)|(d)|(mmmmm)|(mmmm)|(mmm)|(mm)|(m)|(yyyy)|(yy)|(\[\])/gi, function (a, ss, s, hh, h, dddd, ddd, dd, d, mmmmm, mmmm, mmm, mm, m, yyyy, yy, ampm) {
|
|
535
|
+
|
|
536
|
+
if (ss) {
|
|
537
|
+
return _this3.applyNumberPattern(padStartTime(seconds), '00');
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (s) {
|
|
541
|
+
return seconds;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (hh) {
|
|
545
|
+
return _this3.applyNumberPattern(padStartTime(foundAMPM ? hours % 12 : hours), '00');
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (h) {
|
|
549
|
+
return foundAMPM ? hours % 12 : hours;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (dddd) {
|
|
553
|
+
return days[weekDay];
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (ddd) {
|
|
557
|
+
return daysShort[weekDay];
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (dd) {
|
|
561
|
+
return _this3.applyNumberPattern(date, '0');
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (d) {
|
|
565
|
+
return date;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (mmmmm) {
|
|
569
|
+
return monthsShort[month][0];
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (mmmm) {
|
|
573
|
+
return months[month];
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (mmm) {
|
|
577
|
+
return monthsShort[month];
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (mm) {
|
|
581
|
+
return _this3.applyNumberPattern(month + 1, '0');
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (m) {
|
|
585
|
+
return month + 1;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (yyyy) {
|
|
589
|
+
return year;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (yy) {
|
|
593
|
+
return year.toString().substr(2);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (ampm) {
|
|
597
|
+
return hours < 12 ? 'AM' : 'PM';
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return '';
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
}, {
|
|
604
|
+
key: 'createTextCode',
|
|
605
|
+
value: function createTextCode(section) {
|
|
606
|
+
var code = new Code();
|
|
607
|
+
|
|
608
|
+
code.append('\n result.value = {0}.replace(/@/, n);\n ', section);
|
|
609
|
+
|
|
610
|
+
return code.toString();
|
|
611
|
+
}
|
|
612
|
+
}, {
|
|
613
|
+
key: 'createGeneralCode',
|
|
614
|
+
value: function createGeneralCode() {
|
|
615
|
+
var code = new Code();
|
|
616
|
+
var numberCode = this.createNumberCode('#.00');
|
|
617
|
+
var dateTimeCode = this.createDateTimeCode('[d]');
|
|
618
|
+
|
|
619
|
+
code.append('\n if (type === "Number") {\n ' + numberCode + '\n }\n if (type === "DateTime") {\n ' + dateTimeCode + '\n }\n ');
|
|
620
|
+
|
|
621
|
+
return code.toString();
|
|
622
|
+
}
|
|
623
|
+
}, {
|
|
624
|
+
key: 'createNumberExponentialCode',
|
|
625
|
+
value: function createNumberExponentialCode(exponentialMatch) {
|
|
626
|
+
var patternIntegerPart = exponentialMatch[1];
|
|
627
|
+
var patternDecimalPart = exponentialMatch[2];
|
|
628
|
+
var patternPowPart = exponentialMatch[3];
|
|
629
|
+
var code = new Code();
|
|
630
|
+
var integerPartLength = void 0;
|
|
631
|
+
var decimalPartLength = void 0;
|
|
632
|
+
|
|
633
|
+
var zerosCount = function zerosCount(s) {
|
|
634
|
+
return s.match(/0|\?|#/g).length;
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
// Integer part
|
|
638
|
+
if (!patternIntegerPart) {
|
|
639
|
+
patternIntegerPart = '#';
|
|
640
|
+
integerPartLength = 1;
|
|
641
|
+
} else {
|
|
642
|
+
integerPartLength = zerosCount(patternIntegerPart);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Decimal part
|
|
646
|
+
if (!patternDecimalPart) {
|
|
647
|
+
patternDecimalPart = '';
|
|
648
|
+
decimalPartLength = 0;
|
|
649
|
+
} else {
|
|
650
|
+
decimalPartLength = zerosCount(patternDecimalPart);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
code.append('\n result.value = this.formatAsNumberExponential(n, {0}, {1}, {2}, {3}, {4});\n ', integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart);
|
|
654
|
+
|
|
655
|
+
return code.toString();
|
|
656
|
+
}
|
|
657
|
+
}, {
|
|
658
|
+
key: 'createNumberFractialCode',
|
|
659
|
+
value: function createNumberFractialCode(fractialMatch) {
|
|
660
|
+
var code = new Code();
|
|
661
|
+
var patternNumeratorPart = fractialMatch[1] || '#';
|
|
662
|
+
var patternDenominatorPart = fractialMatch[2] || '#';
|
|
663
|
+
// TODO watch here
|
|
664
|
+
var zeroPos = patternNumeratorPart.length - 1;
|
|
665
|
+
|
|
666
|
+
while (patternNumeratorPart[zeroPos] === '0' && patternNumeratorPart[zeroPos] !== '?' && patternNumeratorPart[zeroPos] !== '#' && patternNumeratorPart[zeroPos] !== ' ' && zeroPos > 0) {
|
|
667
|
+
zeroPos--;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
var leftPatternNumeratorPart = patternNumeratorPart.substr(0, zeroPos);
|
|
671
|
+
var rightPatternNumeratorPart = patternNumeratorPart.substr(zeroPos);
|
|
672
|
+
|
|
673
|
+
if (!leftPatternNumeratorPart) {
|
|
674
|
+
code.append('\n result.value = this.formatAsNumberFractial(n, {0}, {1});\n ', rightPatternNumeratorPart, patternDenominatorPart);
|
|
675
|
+
}
|
|
676
|
+
// Mixed fraction
|
|
677
|
+
else {
|
|
678
|
+
code.append('\n result.value = this.formatAsNumberFractialMixed(n, {0}, {1}, {2});\n ', leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
return code.toString();
|
|
682
|
+
}
|
|
683
|
+
}, {
|
|
684
|
+
key: 'createNumberDecimalCode',
|
|
685
|
+
value: function createNumberDecimalCode(decimalMatch) {
|
|
686
|
+
var code = new Code();
|
|
687
|
+
var patternIntegerPart = decimalMatch[1] || '0';
|
|
688
|
+
var patternDecimalPart = decimalMatch[2] || '';
|
|
689
|
+
var decimals = void 0;
|
|
690
|
+
var factor = 1;
|
|
691
|
+
|
|
692
|
+
var zerosCount = function zerosCount(s) {
|
|
693
|
+
return s.match(/0|\?|#/g).length;
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
if (!patternDecimalPart) {
|
|
697
|
+
decimals = 0;
|
|
698
|
+
} else {
|
|
699
|
+
decimals = zerosCount(patternDecimalPart);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// Spaces before .
|
|
703
|
+
patternIntegerPart = patternIntegerPart.replace(/(0|#|\?)(,+)([^0?#]*)$/, function (a, m1, m2, m3) {
|
|
704
|
+
factor *= Math.pow(1000, m2.length);
|
|
705
|
+
return m1 + m3;
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
if (factor !== 1) {
|
|
709
|
+
code.append('\n n /= {0};\n ', factor);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
code.append('\n result.value = this.formatAsNumberDecimal(n, {0}, {1}, {2});\n ', decimals, patternIntegerPart, patternDecimalPart);
|
|
713
|
+
|
|
714
|
+
return code.toString();
|
|
715
|
+
}
|
|
716
|
+
}, {
|
|
717
|
+
key: 'createNumberIntegerCode',
|
|
718
|
+
value: function createNumberIntegerCode(section) {
|
|
719
|
+
var code = new Code();
|
|
720
|
+
|
|
721
|
+
code.append('\n n = Math.round(n);\n result.value = this.applyNumberPattern(n, {0});\n ', section);
|
|
722
|
+
|
|
723
|
+
return code.toString();
|
|
724
|
+
}
|
|
725
|
+
}, {
|
|
726
|
+
key: 'createNumberCode',
|
|
727
|
+
value: function createNumberCode(section, shouldAbsNumber) {
|
|
728
|
+
var numberCode = new Code();
|
|
729
|
+
|
|
730
|
+
// Abs
|
|
731
|
+
if (shouldAbsNumber) {
|
|
732
|
+
numberCode.append('\n n = Math.abs(n);\n ');
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Exponential form regexp
|
|
736
|
+
var exponentialMatch = section.match(/(.*?)(?:\.(.*?))?e(?:\+|\-)(.*)/i);
|
|
737
|
+
|
|
738
|
+
if (exponentialMatch) {
|
|
739
|
+
|
|
740
|
+
// Exponential form
|
|
741
|
+
numberCode.append(this.createNumberExponentialCode(exponentialMatch));
|
|
742
|
+
} else {
|
|
743
|
+
var factor = 1;
|
|
744
|
+
|
|
745
|
+
// Spaces before end and decimal separator (.)
|
|
746
|
+
//section = section.replace(/(0|#|\?)(\s+)([^0?#]*?)($|\.)/, (a, m1, m2, m3, m4)=> {
|
|
747
|
+
section = section.replace(/(0|#|\?)(,+)([^0?#]*?)($|\.)/, function (a, m1, m2, m3, m4) {
|
|
748
|
+
factor *= Math.pow(1000, m2.length);
|
|
749
|
+
return m1 + m3 + m4;
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
// Percents
|
|
753
|
+
var percentMatch = section.match(/%/g);
|
|
754
|
+
if (percentMatch) {
|
|
755
|
+
factor /= Math.pow(100, percentMatch.length);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// Factor
|
|
759
|
+
if (factor !== 1) {
|
|
760
|
+
numberCode.append('\n n /= {0};\n ', factor);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
var fractialMatch = void 0;
|
|
764
|
+
var decimalMatch = void 0;
|
|
765
|
+
|
|
766
|
+
switch (true) {
|
|
767
|
+
|
|
768
|
+
// Fractial form
|
|
769
|
+
case !!(fractialMatch = section.match(/(.*?)\/(.*)/)):
|
|
770
|
+
numberCode.append(this.createNumberFractialCode(fractialMatch));
|
|
771
|
+
break;
|
|
772
|
+
|
|
773
|
+
// Decimal form
|
|
774
|
+
case !!(decimalMatch = section.match(/(.*?)\.(.*)/)):
|
|
775
|
+
numberCode.append(this.createNumberDecimalCode(decimalMatch));
|
|
776
|
+
break;
|
|
777
|
+
|
|
778
|
+
// Integer form
|
|
779
|
+
default:
|
|
780
|
+
numberCode.append(this.createNumberIntegerCode(section));
|
|
781
|
+
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// Final code
|
|
786
|
+
var code = new Code();
|
|
787
|
+
|
|
788
|
+
// Parse to float
|
|
789
|
+
code.append('\n n = parseFloat(n);\n ');
|
|
790
|
+
|
|
791
|
+
// Checks
|
|
792
|
+
code.append('\n if (!isNaN(n)) {\n if (n >= 1e21 || n <= -1e21) {\n result.value = n.toString().toUpperCase();\n }\n else {\n ' + numberCode + '\n }\n }\n ');
|
|
793
|
+
|
|
794
|
+
return code.toString();
|
|
795
|
+
}
|
|
796
|
+
}, {
|
|
797
|
+
key: 'createDateTimeElapsedCode',
|
|
798
|
+
value: function createDateTimeElapsedCode(section) {
|
|
799
|
+
var code = new Code();
|
|
800
|
+
|
|
801
|
+
var foundDays = /d/i.test(section);
|
|
802
|
+
var foundHours = /h/i.test(section);
|
|
803
|
+
var foundMinutes = /m/i.test(section);
|
|
804
|
+
|
|
805
|
+
code.append('\n result.value = this.formatAsDateTimeElapsed(n, {0}, {1}, {2}, {3});\n ', foundDays, foundHours, foundMinutes, section);
|
|
806
|
+
|
|
807
|
+
return code.toString();
|
|
808
|
+
}
|
|
809
|
+
}, {
|
|
810
|
+
key: 'createDateTimeNormalCode',
|
|
811
|
+
value: function createDateTimeNormalCode(section) {
|
|
812
|
+
var code = new Code();
|
|
813
|
+
|
|
814
|
+
code.append('\n result.value = this.formatAsDateTimeNormal(n, {0});\n ', section);
|
|
815
|
+
|
|
816
|
+
return code.toString();
|
|
817
|
+
}
|
|
818
|
+
}, {
|
|
819
|
+
key: 'createDateTimeCode',
|
|
820
|
+
value: function createDateTimeCode(section) {
|
|
821
|
+
var code = new Code();
|
|
822
|
+
var elapsed = false;
|
|
823
|
+
|
|
824
|
+
section = section.replace(/\[(h+?|m+?|s+?|y+?|d+?)]/ig, function (a, m1) {
|
|
825
|
+
elapsed = true;
|
|
826
|
+
return m1;
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
var dateTimeCode = elapsed ? this.createDateTimeElapsedCode(section) : this.createDateTimeNormalCode(section);
|
|
830
|
+
|
|
831
|
+
code.append('\n n = this.createDate(n);\n if (!isNaN(n.getTime())) {\n ' + dateTimeCode + '\n }\n ');
|
|
832
|
+
|
|
833
|
+
return code.toString();
|
|
834
|
+
}
|
|
835
|
+
}, {
|
|
836
|
+
key: 'createSectionCode',
|
|
837
|
+
value: function createSectionCode(section, sectionIndex, sectionsCount) {
|
|
838
|
+
// Start creating code for function
|
|
839
|
+
var code = new Code();
|
|
840
|
+
|
|
841
|
+
var condition = void 0;
|
|
842
|
+
var shouldAbsNumber = false;
|
|
843
|
+
|
|
844
|
+
// Find condition for sector or add standard sector condition (positive number, negative number, etc.)
|
|
845
|
+
var conditionMatch = section.match(/\[((?:>|>=|<|<=|=|<>)[0-9\.]+?)]/);
|
|
846
|
+
|
|
847
|
+
switch (true) {
|
|
848
|
+
|
|
849
|
+
// Found condition
|
|
850
|
+
case !!conditionMatch:
|
|
851
|
+
var cond = conditionMatch[1].replace(/<>/, '!=').replace('/=/', '==');
|
|
852
|
+
condition = 'type == "Number" && parseFloat(n)' + cond;
|
|
853
|
+
break;
|
|
854
|
+
|
|
855
|
+
// Standard condition for first section of 3+
|
|
856
|
+
case sectionIndex === 0 && sectionsCount > 2:
|
|
857
|
+
condition = 'type == "Number" && parseFloat(n) > 0';
|
|
858
|
+
break;
|
|
859
|
+
|
|
860
|
+
// Standard condition for first section of 2
|
|
861
|
+
case sectionIndex === 0 && sectionsCount === 2:
|
|
862
|
+
condition = 'type == "Number" && parseFloat(n) >= 0';
|
|
863
|
+
break;
|
|
864
|
+
|
|
865
|
+
// Standard condition for negative number
|
|
866
|
+
case sectionIndex === 1:
|
|
867
|
+
condition = 'type == "Number" && parseFloat(n) < 0';
|
|
868
|
+
shouldAbsNumber = true;
|
|
869
|
+
break;
|
|
870
|
+
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// Find text color
|
|
874
|
+
section = section.replace(/\[(Red|Green|White|Blue|Magenta|Yellow|Cyan|Black)]/gi, function (a, m1) {
|
|
875
|
+
code.append('\n result.color = {0};\n ', m1);
|
|
876
|
+
return '';
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
// Remove all [], except our replacements and elapsed days, hours, minutes, seconds
|
|
880
|
+
section = section.replace(/(\[((?!((\$*?)|(d*?)|(h*?)|(m*?)|(s*?))]).*?)])/, '');
|
|
881
|
+
|
|
882
|
+
// Format code
|
|
883
|
+
var formatCode = new Code();
|
|
884
|
+
|
|
885
|
+
// Defaults
|
|
886
|
+
formatCode.append('\n result.value = {0};\n result.pattern = {0};\n ', section);
|
|
887
|
+
|
|
888
|
+
switch (true) {
|
|
889
|
+
|
|
890
|
+
// General format
|
|
891
|
+
case /General/i.test(section):
|
|
892
|
+
formatCode.append(this.createGeneralCode(section));
|
|
893
|
+
break;
|
|
894
|
+
|
|
895
|
+
// Text
|
|
896
|
+
case /@/.test(section):
|
|
897
|
+
formatCode.append(this.createTextCode(section));
|
|
898
|
+
break;
|
|
899
|
+
|
|
900
|
+
// Number
|
|
901
|
+
case /#|\?|0/.test(section):
|
|
902
|
+
if (!condition) {
|
|
903
|
+
condition = 'type === "Number"';
|
|
904
|
+
}
|
|
905
|
+
formatCode.append(this.createNumberCode(section, shouldAbsNumber));
|
|
906
|
+
break;
|
|
907
|
+
|
|
908
|
+
// DateTime
|
|
909
|
+
case /h|m|s|y|d/i.test(section):
|
|
910
|
+
if (!condition) {
|
|
911
|
+
condition = 'type === "DateTime"';
|
|
912
|
+
}
|
|
913
|
+
formatCode.append(this.createDateTimeCode(section));
|
|
914
|
+
break;
|
|
915
|
+
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
// Add return statement
|
|
919
|
+
formatCode.append('\n return makeResult.call(this);\n ');
|
|
920
|
+
|
|
921
|
+
// Build final section code
|
|
922
|
+
if (condition) {
|
|
923
|
+
code.append('\n // Section\n if (' + condition + ') {\n ' + formatCode + '\n }\n // End section\n ');
|
|
924
|
+
} else {
|
|
925
|
+
code.append('\n // Section\n ' + formatCode + '\n // End section\n ');
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
return code.toString();
|
|
929
|
+
}
|
|
930
|
+
}, {
|
|
931
|
+
key: 'createPatternCode',
|
|
932
|
+
value: function createPatternCode(pattern) {
|
|
933
|
+
var _this4 = this;
|
|
934
|
+
|
|
935
|
+
var origins = [];
|
|
936
|
+
var replaces = '';
|
|
937
|
+
|
|
938
|
+
// Find quotes, slash symbols
|
|
939
|
+
var patternReplaced = pattern.replace(/"([^"]+)"|\\(.?)|(_.?)|(\*.?)|(")/g, function (a, m1, m2, m3) {
|
|
940
|
+
// Quote found
|
|
941
|
+
if (m1) {
|
|
942
|
+
origins.push(m1.replace(/("|'|\\)/g, "\\$1"));
|
|
943
|
+
return '[' + (replaces += '$') + ']';
|
|
944
|
+
}
|
|
945
|
+
// Slash found
|
|
946
|
+
if (m2) {
|
|
947
|
+
origins.push(m2.replace(/("|'|\\)/g, "\\$1"));
|
|
948
|
+
return '[' + (replaces += '$') + ']';
|
|
949
|
+
}
|
|
950
|
+
// Space found
|
|
951
|
+
if (m3) {
|
|
952
|
+
origins.push(' ');
|
|
953
|
+
return '[' + (replaces += '$') + ']';
|
|
954
|
+
}
|
|
955
|
+
return '';
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
// Split pattern to sections
|
|
959
|
+
var sections = patternReplaced.split(/;/);
|
|
960
|
+
|
|
961
|
+
// Init code
|
|
962
|
+
var code = new Code();
|
|
963
|
+
|
|
964
|
+
// Start variables
|
|
965
|
+
code.append('\n var result = {\n value: "",\n align: type === "Number" || type === "DateTime" ? "right" : "",\n color: "",\n pattern: ""\n };\n function makeResult() {\n var origins = {0};\n result.value = this.restoreOrigins(result.value, origins);\n result.pattern = this.restoreOrigins(result.pattern, origins);\n return result;\n };\n ', origins);
|
|
966
|
+
|
|
967
|
+
// Remove unnesessary sections
|
|
968
|
+
sections = sections.slice(0, 4);
|
|
969
|
+
|
|
970
|
+
// Loop trough sections
|
|
971
|
+
sections.forEach(function (section, sectionIndex) {
|
|
972
|
+
return code.append(_this4.createSectionCode(section, sectionIndex, sections.length));
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
// Return statement
|
|
976
|
+
code.append('\n result.value = {0};\n result.pattern = {0};\n return makeResult.call(this);\n ', patternReplaced);
|
|
977
|
+
|
|
978
|
+
return code.toString();
|
|
979
|
+
}
|
|
980
|
+
}, {
|
|
981
|
+
key: 'format',
|
|
982
|
+
value: function format(n, type, pattern) {
|
|
983
|
+
this.log(`Input:`);
|
|
984
|
+
this.log(`n = ${n}, type = ${type}, pattern = ${pattern}`);
|
|
985
|
+
var result=n
|
|
986
|
+
n = n.toString();
|
|
987
|
+
pattern = pattern.toString();
|
|
988
|
+
//pattern=pattern.replace(/"/g, "'");
|
|
989
|
+
// Find predefined format
|
|
990
|
+
if (this.locale.formats[pattern]) {
|
|
991
|
+
pattern = this.locale.formats[pattern];
|
|
992
|
+
}
|
|
993
|
+
try{
|
|
994
|
+
// Create function
|
|
995
|
+
if (!this.memoized[pattern]) {
|
|
996
|
+
|
|
997
|
+
let code = this.createPatternCode(pattern);
|
|
998
|
+
|
|
999
|
+
// Transform code
|
|
1000
|
+
code = this.transformCode(code);
|
|
1001
|
+
|
|
1002
|
+
// Memoize function
|
|
1003
|
+
this.memoized[pattern] = Function('n', 'type', code);
|
|
1004
|
+
|
|
1005
|
+
// Log code
|
|
1006
|
+
this.log('Code:');
|
|
1007
|
+
this.log(code);
|
|
1008
|
+
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// Call function
|
|
1012
|
+
result = this.memoized[pattern].call(this, n, type);
|
|
1013
|
+
|
|
1014
|
+
if (result.value === result.pattern && !(String(n) === '0' && result.value.includes('0'))) {
|
|
1015
|
+
result.value = n;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
catch (e) {
|
|
1019
|
+
|
|
1020
|
+
this.log(`Input:`,e);
|
|
1021
|
+
|
|
1022
|
+
}
|
|
1023
|
+
// Log result
|
|
1024
|
+
this.log('Result:');
|
|
1025
|
+
this.log(result);
|
|
1026
|
+
|
|
1027
|
+
return result;
|
|
1028
|
+
}
|
|
1029
|
+
}]);
|
|
1030
|
+
|
|
1031
|
+
return DataFormatterImpl;
|
|
1032
|
+
}();
|
|
1033
|
+
|
|
1034
1034
|
module.exports = new DataFormatterImpl();
|