@datarailsshared/dr_renderer 1.2.37-beta → 1.2.37

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,1023 +1,1023 @@
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
- // Build res
501
- 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) {
502
-
503
- if (ampm) {
504
- foundAMPM = true;
505
- return '[]';
506
- }
507
-
508
- if (fmin) {
509
- return fmin + _this3.applyNumberPattern(minutes, '00');
510
- }
511
-
512
- if (fmin2) {
513
- return _this3.applyNumberPattern(minutes, '00') + fmin2;
514
- }
515
-
516
- if (mmin) {
517
- return mmin + minutes;
518
- }
519
-
520
- if (mmin2) {
521
- return minutes + mmin2;
522
- }
523
-
524
- return '';
525
- });
526
-
527
- 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) {
528
-
529
- if (ss) {
530
- return _this3.applyNumberPattern(seconds, '00');
531
- }
532
-
533
- if (s) {
534
- return seconds;
535
- }
536
-
537
- if (hh) {
538
- return _this3.applyNumberPattern(foundAMPM ? hours % 12 : hours, '00');
539
- }
540
-
541
- if (h) {
542
- return foundAMPM ? hours % 12 : hours;
543
- }
544
-
545
- if (dddd) {
546
- return days[weekDay];
547
- }
548
-
549
- if (ddd) {
550
- return daysShort[weekDay];
551
- }
552
-
553
- if (dd) {
554
- return _this3.applyNumberPattern(date, '0');
555
- }
556
-
557
- if (d) {
558
- return date;
559
- }
560
-
561
- if (mmmmm) {
562
- return monthsShort[month][0];
563
- }
564
-
565
- if (mmmm) {
566
- return months[month];
567
- }
568
-
569
- if (mmm) {
570
- return monthsShort[month];
571
- }
572
-
573
- if (mm) {
574
- return _this3.applyNumberPattern(month + 1, '0');
575
- }
576
-
577
- if (m) {
578
- return month + 1;
579
- }
580
-
581
- if (yyyy) {
582
- return year;
583
- }
584
-
585
- if (yy) {
586
- return year.toString().substr(2);
587
- }
588
-
589
- if (ampm) {
590
- return hours < 12 ? 'AM' : 'PM';
591
- }
592
-
593
- return '';
594
- });
595
- }
596
- }, {
597
- key: 'createTextCode',
598
- value: function createTextCode(section) {
599
- var code = new Code();
600
-
601
- code.append('\n result.value = {0}.replace(/@/, n);\n ', section);
602
-
603
- return code.toString();
604
- }
605
- }, {
606
- key: 'createGeneralCode',
607
- value: function createGeneralCode() {
608
- var code = new Code();
609
- var numberCode = this.createNumberCode('#.00');
610
- var dateTimeCode = this.createDateTimeCode('[d]');
611
-
612
- code.append('\n if (type === "Number") {\n ' + numberCode + '\n }\n if (type === "DateTime") {\n ' + dateTimeCode + '\n }\n ');
613
-
614
- return code.toString();
615
- }
616
- }, {
617
- key: 'createNumberExponentialCode',
618
- value: function createNumberExponentialCode(exponentialMatch) {
619
- var patternIntegerPart = exponentialMatch[1];
620
- var patternDecimalPart = exponentialMatch[2];
621
- var patternPowPart = exponentialMatch[3];
622
- var code = new Code();
623
- var integerPartLength = void 0;
624
- var decimalPartLength = void 0;
625
-
626
- var zerosCount = function zerosCount(s) {
627
- return s.match(/0|\?|#/g).length;
628
- };
629
-
630
- // Integer part
631
- if (!patternIntegerPart) {
632
- patternIntegerPart = '#';
633
- integerPartLength = 1;
634
- } else {
635
- integerPartLength = zerosCount(patternIntegerPart);
636
- }
637
-
638
- // Decimal part
639
- if (!patternDecimalPart) {
640
- patternDecimalPart = '';
641
- decimalPartLength = 0;
642
- } else {
643
- decimalPartLength = zerosCount(patternDecimalPart);
644
- }
645
-
646
- code.append('\n result.value = this.formatAsNumberExponential(n, {0}, {1}, {2}, {3}, {4});\n ', integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart);
647
-
648
- return code.toString();
649
- }
650
- }, {
651
- key: 'createNumberFractialCode',
652
- value: function createNumberFractialCode(fractialMatch) {
653
- var code = new Code();
654
- var patternNumeratorPart = fractialMatch[1] || '#';
655
- var patternDenominatorPart = fractialMatch[2] || '#';
656
- // TODO watch here
657
- var zeroPos = patternNumeratorPart.length - 1;
658
-
659
- while (patternNumeratorPart[zeroPos] === '0' && patternNumeratorPart[zeroPos] !== '?' && patternNumeratorPart[zeroPos] !== '#' && patternNumeratorPart[zeroPos] !== ' ' && zeroPos > 0) {
660
- zeroPos--;
661
- }
662
-
663
- var leftPatternNumeratorPart = patternNumeratorPart.substr(0, zeroPos);
664
- var rightPatternNumeratorPart = patternNumeratorPart.substr(zeroPos);
665
-
666
- if (!leftPatternNumeratorPart) {
667
- code.append('\n result.value = this.formatAsNumberFractial(n, {0}, {1});\n ', rightPatternNumeratorPart, patternDenominatorPart);
668
- }
669
- // Mixed fraction
670
- else {
671
- code.append('\n result.value = this.formatAsNumberFractialMixed(n, {0}, {1}, {2});\n ', leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart);
672
- }
673
-
674
- return code.toString();
675
- }
676
- }, {
677
- key: 'createNumberDecimalCode',
678
- value: function createNumberDecimalCode(decimalMatch) {
679
- var code = new Code();
680
- var patternIntegerPart = decimalMatch[1] || '0';
681
- var patternDecimalPart = decimalMatch[2] || '';
682
- var decimals = void 0;
683
- var factor = 1;
684
-
685
- var zerosCount = function zerosCount(s) {
686
- return s.match(/0|\?|#/g).length;
687
- };
688
-
689
- if (!patternDecimalPart) {
690
- decimals = 0;
691
- } else {
692
- decimals = zerosCount(patternDecimalPart);
693
- }
694
-
695
- // Spaces before .
696
- patternIntegerPart = patternIntegerPart.replace(/(0|#|\?)(,+)([^0?#]*)$/, function (a, m1, m2, m3) {
697
- factor *= Math.pow(1000, m2.length);
698
- return m1 + m3;
699
- });
700
-
701
- if (factor !== 1) {
702
- code.append('\n n /= {0};\n ', factor);
703
- }
704
-
705
- code.append('\n result.value = this.formatAsNumberDecimal(n, {0}, {1}, {2});\n ', decimals, patternIntegerPart, patternDecimalPart);
706
-
707
- return code.toString();
708
- }
709
- }, {
710
- key: 'createNumberIntegerCode',
711
- value: function createNumberIntegerCode(section) {
712
- var code = new Code();
713
-
714
- code.append('\n n = Math.round(n);\n result.value = this.applyNumberPattern(n, {0});\n ', section);
715
-
716
- return code.toString();
717
- }
718
- }, {
719
- key: 'createNumberCode',
720
- value: function createNumberCode(section, shouldAbsNumber) {
721
- var numberCode = new Code();
722
-
723
- // Abs
724
- if (shouldAbsNumber) {
725
- numberCode.append('\n n = Math.abs(n);\n ');
726
- }
727
-
728
- // Exponential form regexp
729
- var exponentialMatch = section.match(/(.*?)(?:\.(.*?))?e(?:\+|\-)(.*)/i);
730
-
731
- if (exponentialMatch) {
732
-
733
- // Exponential form
734
- numberCode.append(this.createNumberExponentialCode(exponentialMatch));
735
- } else {
736
- var factor = 1;
737
-
738
- // Spaces before end and decimal separator (.)
739
- //section = section.replace(/(0|#|\?)(\s+)([^0?#]*?)($|\.)/, (a, m1, m2, m3, m4)=> {
740
- section = section.replace(/(0|#|\?)(,+)([^0?#]*?)($|\.)/, function (a, m1, m2, m3, m4) {
741
- factor *= Math.pow(1000, m2.length);
742
- return m1 + m3 + m4;
743
- });
744
-
745
- // Percents
746
- var percentMatch = section.match(/%/g);
747
- if (percentMatch) {
748
- factor /= Math.pow(100, percentMatch.length);
749
- }
750
-
751
- // Factor
752
- if (factor !== 1) {
753
- numberCode.append('\n n /= {0};\n ', factor);
754
- }
755
-
756
- var fractialMatch = void 0;
757
- var decimalMatch = void 0;
758
-
759
- switch (true) {
760
-
761
- // Fractial form
762
- case !!(fractialMatch = section.match(/(.*?)\/(.*)/)):
763
- numberCode.append(this.createNumberFractialCode(fractialMatch));
764
- break;
765
-
766
- // Decimal form
767
- case !!(decimalMatch = section.match(/(.*?)\.(.*)/)):
768
- numberCode.append(this.createNumberDecimalCode(decimalMatch));
769
- break;
770
-
771
- // Integer form
772
- default:
773
- numberCode.append(this.createNumberIntegerCode(section));
774
-
775
- }
776
- }
777
-
778
- // Final code
779
- var code = new Code();
780
-
781
- // Parse to float
782
- code.append('\n n = parseFloat(n);\n ');
783
-
784
- // Checks
785
- 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 ');
786
-
787
- return code.toString();
788
- }
789
- }, {
790
- key: 'createDateTimeElapsedCode',
791
- value: function createDateTimeElapsedCode(section) {
792
- var code = new Code();
793
-
794
- var foundDays = /d/i.test(section);
795
- var foundHours = /h/i.test(section);
796
- var foundMinutes = /m/i.test(section);
797
-
798
- code.append('\n result.value = this.formatAsDateTimeElapsed(n, {0}, {1}, {2}, {3});\n ', foundDays, foundHours, foundMinutes, section);
799
-
800
- return code.toString();
801
- }
802
- }, {
803
- key: 'createDateTimeNormalCode',
804
- value: function createDateTimeNormalCode(section) {
805
- var code = new Code();
806
-
807
- code.append('\n result.value = this.formatAsDateTimeNormal(n, {0});\n ', section);
808
-
809
- return code.toString();
810
- }
811
- }, {
812
- key: 'createDateTimeCode',
813
- value: function createDateTimeCode(section) {
814
- var code = new Code();
815
- var elapsed = false;
816
-
817
- section = section.replace(/\[(h+?|m+?|s+?|y+?|d+?)]/ig, function (a, m1) {
818
- elapsed = true;
819
- return m1;
820
- });
821
-
822
- var dateTimeCode = elapsed ? this.createDateTimeElapsedCode(section) : this.createDateTimeNormalCode(section);
823
-
824
- code.append('\n n = this.createDate(n);\n if (!isNaN(n.getTime())) {\n ' + dateTimeCode + '\n }\n ');
825
-
826
- return code.toString();
827
- }
828
- }, {
829
- key: 'createSectionCode',
830
- value: function createSectionCode(section, sectionIndex, sectionsCount) {
831
- // Start creating code for function
832
- var code = new Code();
833
-
834
- var condition = void 0;
835
- var shouldAbsNumber = false;
836
-
837
- // Find condition for sector or add standard sector condition (positive number, negative number, etc.)
838
- var conditionMatch = section.match(/\[((?:>|>=|<|<=|=|<>)[0-9\.]+?)]/);
839
-
840
- switch (true) {
841
-
842
- // Found condition
843
- case !!conditionMatch:
844
- var cond = conditionMatch[1].replace(/<>/, '!=').replace('/=/', '==');
845
- condition = 'type == "Number" && parseFloat(n)' + cond;
846
- break;
847
-
848
- // Standard condition for first section of 3+
849
- case sectionIndex === 0 && sectionsCount > 2:
850
- condition = 'type == "Number" && parseFloat(n) > 0';
851
- break;
852
-
853
- // Standard condition for first section of 2
854
- case sectionIndex === 0 && sectionsCount === 2:
855
- condition = 'type == "Number" && parseFloat(n) >= 0';
856
- break;
857
-
858
- // Standard condition for negative number
859
- case sectionIndex === 1:
860
- condition = 'type == "Number" && parseFloat(n) < 0';
861
- shouldAbsNumber = true;
862
- break;
863
-
864
- }
865
-
866
- // Find text color
867
- section = section.replace(/\[(Red|Green|White|Blue|Magenta|Yellow|Cyan|Black)]/gi, function (a, m1) {
868
- code.append('\n result.color = {0};\n ', m1);
869
- return '';
870
- });
871
-
872
- // Remove all [], except our replacements and elapsed days, hours, minutes, seconds
873
- section = section.replace(/(\[((?!((\$*?)|(d*?)|(h*?)|(m*?)|(s*?))]).*?)])/, '');
874
-
875
- // Format code
876
- var formatCode = new Code();
877
-
878
- // Defaults
879
- formatCode.append('\n result.value = {0};\n result.pattern = {0};\n ', section);
880
-
881
- switch (true) {
882
-
883
- // General format
884
- case /General/i.test(section):
885
- formatCode.append(this.createGeneralCode(section));
886
- break;
887
-
888
- // Text
889
- case /@/.test(section):
890
- formatCode.append(this.createTextCode(section));
891
- break;
892
-
893
- // Number
894
- case /#|\?|0/.test(section):
895
- if (!condition) {
896
- condition = 'type === "Number"';
897
- }
898
- formatCode.append(this.createNumberCode(section, shouldAbsNumber));
899
- break;
900
-
901
- // DateTime
902
- case /h|m|s|y|d/i.test(section):
903
- if (!condition) {
904
- condition = 'type === "DateTime"';
905
- }
906
- formatCode.append(this.createDateTimeCode(section));
907
- break;
908
-
909
- }
910
-
911
- // Add return statement
912
- formatCode.append('\n return makeResult.call(this);\n ');
913
-
914
- // Build final section code
915
- if (condition) {
916
- code.append('\n // Section\n if (' + condition + ') {\n ' + formatCode + '\n }\n // End section\n ');
917
- } else {
918
- code.append('\n // Section\n ' + formatCode + '\n // End section\n ');
919
- }
920
-
921
- return code.toString();
922
- }
923
- }, {
924
- key: 'createPatternCode',
925
- value: function createPatternCode(pattern) {
926
- var _this4 = this;
927
-
928
- var origins = [];
929
- var replaces = '';
930
-
931
- // Find quotes, slash symbols
932
- var patternReplaced = pattern.replace(/"([^"]+)"|\\(.?)|(_.?)|(\*.?)|(")/g, function (a, m1, m2, m3) {
933
- // Quote found
934
- if (m1) {
935
- origins.push(m1.replace(/("|'|\\)/g, "\\$1"));
936
- return '[' + (replaces += '$') + ']';
937
- }
938
- // Slash found
939
- if (m2) {
940
- origins.push(m2.replace(/("|'|\\)/g, "\\$1"));
941
- return '[' + (replaces += '$') + ']';
942
- }
943
- // Space found
944
- if (m3) {
945
- origins.push(' ');
946
- return '[' + (replaces += '$') + ']';
947
- }
948
- return '';
949
- });
950
-
951
- // Split pattern to sections
952
- var sections = patternReplaced.split(/;/);
953
-
954
- // Init code
955
- var code = new Code();
956
-
957
- // Start variables
958
- 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);
959
-
960
- // Remove unnesessary sections
961
- sections = sections.slice(0, 4);
962
-
963
- // Loop trough sections
964
- sections.forEach(function (section, sectionIndex) {
965
- return code.append(_this4.createSectionCode(section, sectionIndex, sections.length));
966
- });
967
-
968
- // Return statement
969
- code.append('\n result.value = {0};\n result.pattern = {0};\n return makeResult.call(this);\n ', patternReplaced);
970
-
971
- return code.toString();
972
- }
973
- }, {
974
- key: 'format',
975
- value: function format(n, type, pattern) {
976
- this.log(`Input:`);
977
- this.log(`n = ${n}, type = ${type}, pattern = ${pattern}`);
978
- var result=n
979
- n = n.toString();
980
- pattern = pattern.toString();
981
- //pattern=pattern.replace(/"/g, "'");
982
- // Find predefined format
983
- if (this.locale.formats[pattern]) {
984
- pattern = this.locale.formats[pattern];
985
- }
986
- try{
987
- // Create function
988
- if (!this.memoized[pattern]) {
989
-
990
- let code = this.createPatternCode(pattern);
991
-
992
- // Transform code
993
- code = this.transformCode(code);
994
-
995
- // Memoize function
996
- this.memoized[pattern] = Function('n', 'type', code);
997
-
998
- // Log code
999
- this.log('Code:');
1000
- this.log(code);
1001
-
1002
- }
1003
-
1004
- // Call function
1005
- result = this.memoized[pattern].call(this, n, type);
1006
- }
1007
- catch (e) {
1008
-
1009
- this.log(`Input:`,e);
1010
-
1011
- }
1012
- // Log result
1013
- this.log('Result:');
1014
- this.log(result);
1015
-
1016
- return result;
1017
- }
1018
- }]);
1019
-
1020
- return DataFormatterImpl;
1021
- }();
1022
-
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
+ // Build res
501
+ 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) {
502
+
503
+ if (ampm) {
504
+ foundAMPM = true;
505
+ return '[]';
506
+ }
507
+
508
+ if (fmin) {
509
+ return fmin + _this3.applyNumberPattern(minutes, '00');
510
+ }
511
+
512
+ if (fmin2) {
513
+ return _this3.applyNumberPattern(minutes, '00') + fmin2;
514
+ }
515
+
516
+ if (mmin) {
517
+ return mmin + minutes;
518
+ }
519
+
520
+ if (mmin2) {
521
+ return minutes + mmin2;
522
+ }
523
+
524
+ return '';
525
+ });
526
+
527
+ 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) {
528
+
529
+ if (ss) {
530
+ return _this3.applyNumberPattern(seconds, '00');
531
+ }
532
+
533
+ if (s) {
534
+ return seconds;
535
+ }
536
+
537
+ if (hh) {
538
+ return _this3.applyNumberPattern(foundAMPM ? hours % 12 : hours, '00');
539
+ }
540
+
541
+ if (h) {
542
+ return foundAMPM ? hours % 12 : hours;
543
+ }
544
+
545
+ if (dddd) {
546
+ return days[weekDay];
547
+ }
548
+
549
+ if (ddd) {
550
+ return daysShort[weekDay];
551
+ }
552
+
553
+ if (dd) {
554
+ return _this3.applyNumberPattern(date, '0');
555
+ }
556
+
557
+ if (d) {
558
+ return date;
559
+ }
560
+
561
+ if (mmmmm) {
562
+ return monthsShort[month][0];
563
+ }
564
+
565
+ if (mmmm) {
566
+ return months[month];
567
+ }
568
+
569
+ if (mmm) {
570
+ return monthsShort[month];
571
+ }
572
+
573
+ if (mm) {
574
+ return _this3.applyNumberPattern(month + 1, '0');
575
+ }
576
+
577
+ if (m) {
578
+ return month + 1;
579
+ }
580
+
581
+ if (yyyy) {
582
+ return year;
583
+ }
584
+
585
+ if (yy) {
586
+ return year.toString().substr(2);
587
+ }
588
+
589
+ if (ampm) {
590
+ return hours < 12 ? 'AM' : 'PM';
591
+ }
592
+
593
+ return '';
594
+ });
595
+ }
596
+ }, {
597
+ key: 'createTextCode',
598
+ value: function createTextCode(section) {
599
+ var code = new Code();
600
+
601
+ code.append('\n result.value = {0}.replace(/@/, n);\n ', section);
602
+
603
+ return code.toString();
604
+ }
605
+ }, {
606
+ key: 'createGeneralCode',
607
+ value: function createGeneralCode() {
608
+ var code = new Code();
609
+ var numberCode = this.createNumberCode('#.00');
610
+ var dateTimeCode = this.createDateTimeCode('[d]');
611
+
612
+ code.append('\n if (type === "Number") {\n ' + numberCode + '\n }\n if (type === "DateTime") {\n ' + dateTimeCode + '\n }\n ');
613
+
614
+ return code.toString();
615
+ }
616
+ }, {
617
+ key: 'createNumberExponentialCode',
618
+ value: function createNumberExponentialCode(exponentialMatch) {
619
+ var patternIntegerPart = exponentialMatch[1];
620
+ var patternDecimalPart = exponentialMatch[2];
621
+ var patternPowPart = exponentialMatch[3];
622
+ var code = new Code();
623
+ var integerPartLength = void 0;
624
+ var decimalPartLength = void 0;
625
+
626
+ var zerosCount = function zerosCount(s) {
627
+ return s.match(/0|\?|#/g).length;
628
+ };
629
+
630
+ // Integer part
631
+ if (!patternIntegerPart) {
632
+ patternIntegerPart = '#';
633
+ integerPartLength = 1;
634
+ } else {
635
+ integerPartLength = zerosCount(patternIntegerPart);
636
+ }
637
+
638
+ // Decimal part
639
+ if (!patternDecimalPart) {
640
+ patternDecimalPart = '';
641
+ decimalPartLength = 0;
642
+ } else {
643
+ decimalPartLength = zerosCount(patternDecimalPart);
644
+ }
645
+
646
+ code.append('\n result.value = this.formatAsNumberExponential(n, {0}, {1}, {2}, {3}, {4});\n ', integerPartLength, decimalPartLength, patternIntegerPart, patternDecimalPart, patternPowPart);
647
+
648
+ return code.toString();
649
+ }
650
+ }, {
651
+ key: 'createNumberFractialCode',
652
+ value: function createNumberFractialCode(fractialMatch) {
653
+ var code = new Code();
654
+ var patternNumeratorPart = fractialMatch[1] || '#';
655
+ var patternDenominatorPart = fractialMatch[2] || '#';
656
+ // TODO watch here
657
+ var zeroPos = patternNumeratorPart.length - 1;
658
+
659
+ while (patternNumeratorPart[zeroPos] === '0' && patternNumeratorPart[zeroPos] !== '?' && patternNumeratorPart[zeroPos] !== '#' && patternNumeratorPart[zeroPos] !== ' ' && zeroPos > 0) {
660
+ zeroPos--;
661
+ }
662
+
663
+ var leftPatternNumeratorPart = patternNumeratorPart.substr(0, zeroPos);
664
+ var rightPatternNumeratorPart = patternNumeratorPart.substr(zeroPos);
665
+
666
+ if (!leftPatternNumeratorPart) {
667
+ code.append('\n result.value = this.formatAsNumberFractial(n, {0}, {1});\n ', rightPatternNumeratorPart, patternDenominatorPart);
668
+ }
669
+ // Mixed fraction
670
+ else {
671
+ code.append('\n result.value = this.formatAsNumberFractialMixed(n, {0}, {1}, {2});\n ', leftPatternNumeratorPart, rightPatternNumeratorPart, patternDenominatorPart);
672
+ }
673
+
674
+ return code.toString();
675
+ }
676
+ }, {
677
+ key: 'createNumberDecimalCode',
678
+ value: function createNumberDecimalCode(decimalMatch) {
679
+ var code = new Code();
680
+ var patternIntegerPart = decimalMatch[1] || '0';
681
+ var patternDecimalPart = decimalMatch[2] || '';
682
+ var decimals = void 0;
683
+ var factor = 1;
684
+
685
+ var zerosCount = function zerosCount(s) {
686
+ return s.match(/0|\?|#/g).length;
687
+ };
688
+
689
+ if (!patternDecimalPart) {
690
+ decimals = 0;
691
+ } else {
692
+ decimals = zerosCount(patternDecimalPart);
693
+ }
694
+
695
+ // Spaces before .
696
+ patternIntegerPart = patternIntegerPart.replace(/(0|#|\?)(,+)([^0?#]*)$/, function (a, m1, m2, m3) {
697
+ factor *= Math.pow(1000, m2.length);
698
+ return m1 + m3;
699
+ });
700
+
701
+ if (factor !== 1) {
702
+ code.append('\n n /= {0};\n ', factor);
703
+ }
704
+
705
+ code.append('\n result.value = this.formatAsNumberDecimal(n, {0}, {1}, {2});\n ', decimals, patternIntegerPart, patternDecimalPart);
706
+
707
+ return code.toString();
708
+ }
709
+ }, {
710
+ key: 'createNumberIntegerCode',
711
+ value: function createNumberIntegerCode(section) {
712
+ var code = new Code();
713
+
714
+ code.append('\n n = Math.round(n);\n result.value = this.applyNumberPattern(n, {0});\n ', section);
715
+
716
+ return code.toString();
717
+ }
718
+ }, {
719
+ key: 'createNumberCode',
720
+ value: function createNumberCode(section, shouldAbsNumber) {
721
+ var numberCode = new Code();
722
+
723
+ // Abs
724
+ if (shouldAbsNumber) {
725
+ numberCode.append('\n n = Math.abs(n);\n ');
726
+ }
727
+
728
+ // Exponential form regexp
729
+ var exponentialMatch = section.match(/(.*?)(?:\.(.*?))?e(?:\+|\-)(.*)/i);
730
+
731
+ if (exponentialMatch) {
732
+
733
+ // Exponential form
734
+ numberCode.append(this.createNumberExponentialCode(exponentialMatch));
735
+ } else {
736
+ var factor = 1;
737
+
738
+ // Spaces before end and decimal separator (.)
739
+ //section = section.replace(/(0|#|\?)(\s+)([^0?#]*?)($|\.)/, (a, m1, m2, m3, m4)=> {
740
+ section = section.replace(/(0|#|\?)(,+)([^0?#]*?)($|\.)/, function (a, m1, m2, m3, m4) {
741
+ factor *= Math.pow(1000, m2.length);
742
+ return m1 + m3 + m4;
743
+ });
744
+
745
+ // Percents
746
+ var percentMatch = section.match(/%/g);
747
+ if (percentMatch) {
748
+ factor /= Math.pow(100, percentMatch.length);
749
+ }
750
+
751
+ // Factor
752
+ if (factor !== 1) {
753
+ numberCode.append('\n n /= {0};\n ', factor);
754
+ }
755
+
756
+ var fractialMatch = void 0;
757
+ var decimalMatch = void 0;
758
+
759
+ switch (true) {
760
+
761
+ // Fractial form
762
+ case !!(fractialMatch = section.match(/(.*?)\/(.*)/)):
763
+ numberCode.append(this.createNumberFractialCode(fractialMatch));
764
+ break;
765
+
766
+ // Decimal form
767
+ case !!(decimalMatch = section.match(/(.*?)\.(.*)/)):
768
+ numberCode.append(this.createNumberDecimalCode(decimalMatch));
769
+ break;
770
+
771
+ // Integer form
772
+ default:
773
+ numberCode.append(this.createNumberIntegerCode(section));
774
+
775
+ }
776
+ }
777
+
778
+ // Final code
779
+ var code = new Code();
780
+
781
+ // Parse to float
782
+ code.append('\n n = parseFloat(n);\n ');
783
+
784
+ // Checks
785
+ 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 ');
786
+
787
+ return code.toString();
788
+ }
789
+ }, {
790
+ key: 'createDateTimeElapsedCode',
791
+ value: function createDateTimeElapsedCode(section) {
792
+ var code = new Code();
793
+
794
+ var foundDays = /d/i.test(section);
795
+ var foundHours = /h/i.test(section);
796
+ var foundMinutes = /m/i.test(section);
797
+
798
+ code.append('\n result.value = this.formatAsDateTimeElapsed(n, {0}, {1}, {2}, {3});\n ', foundDays, foundHours, foundMinutes, section);
799
+
800
+ return code.toString();
801
+ }
802
+ }, {
803
+ key: 'createDateTimeNormalCode',
804
+ value: function createDateTimeNormalCode(section) {
805
+ var code = new Code();
806
+
807
+ code.append('\n result.value = this.formatAsDateTimeNormal(n, {0});\n ', section);
808
+
809
+ return code.toString();
810
+ }
811
+ }, {
812
+ key: 'createDateTimeCode',
813
+ value: function createDateTimeCode(section) {
814
+ var code = new Code();
815
+ var elapsed = false;
816
+
817
+ section = section.replace(/\[(h+?|m+?|s+?|y+?|d+?)]/ig, function (a, m1) {
818
+ elapsed = true;
819
+ return m1;
820
+ });
821
+
822
+ var dateTimeCode = elapsed ? this.createDateTimeElapsedCode(section) : this.createDateTimeNormalCode(section);
823
+
824
+ code.append('\n n = this.createDate(n);\n if (!isNaN(n.getTime())) {\n ' + dateTimeCode + '\n }\n ');
825
+
826
+ return code.toString();
827
+ }
828
+ }, {
829
+ key: 'createSectionCode',
830
+ value: function createSectionCode(section, sectionIndex, sectionsCount) {
831
+ // Start creating code for function
832
+ var code = new Code();
833
+
834
+ var condition = void 0;
835
+ var shouldAbsNumber = false;
836
+
837
+ // Find condition for sector or add standard sector condition (positive number, negative number, etc.)
838
+ var conditionMatch = section.match(/\[((?:>|>=|<|<=|=|<>)[0-9\.]+?)]/);
839
+
840
+ switch (true) {
841
+
842
+ // Found condition
843
+ case !!conditionMatch:
844
+ var cond = conditionMatch[1].replace(/<>/, '!=').replace('/=/', '==');
845
+ condition = 'type == "Number" && parseFloat(n)' + cond;
846
+ break;
847
+
848
+ // Standard condition for first section of 3+
849
+ case sectionIndex === 0 && sectionsCount > 2:
850
+ condition = 'type == "Number" && parseFloat(n) > 0';
851
+ break;
852
+
853
+ // Standard condition for first section of 2
854
+ case sectionIndex === 0 && sectionsCount === 2:
855
+ condition = 'type == "Number" && parseFloat(n) >= 0';
856
+ break;
857
+
858
+ // Standard condition for negative number
859
+ case sectionIndex === 1:
860
+ condition = 'type == "Number" && parseFloat(n) < 0';
861
+ shouldAbsNumber = true;
862
+ break;
863
+
864
+ }
865
+
866
+ // Find text color
867
+ section = section.replace(/\[(Red|Green|White|Blue|Magenta|Yellow|Cyan|Black)]/gi, function (a, m1) {
868
+ code.append('\n result.color = {0};\n ', m1);
869
+ return '';
870
+ });
871
+
872
+ // Remove all [], except our replacements and elapsed days, hours, minutes, seconds
873
+ section = section.replace(/(\[((?!((\$*?)|(d*?)|(h*?)|(m*?)|(s*?))]).*?)])/, '');
874
+
875
+ // Format code
876
+ var formatCode = new Code();
877
+
878
+ // Defaults
879
+ formatCode.append('\n result.value = {0};\n result.pattern = {0};\n ', section);
880
+
881
+ switch (true) {
882
+
883
+ // General format
884
+ case /General/i.test(section):
885
+ formatCode.append(this.createGeneralCode(section));
886
+ break;
887
+
888
+ // Text
889
+ case /@/.test(section):
890
+ formatCode.append(this.createTextCode(section));
891
+ break;
892
+
893
+ // Number
894
+ case /#|\?|0/.test(section):
895
+ if (!condition) {
896
+ condition = 'type === "Number"';
897
+ }
898
+ formatCode.append(this.createNumberCode(section, shouldAbsNumber));
899
+ break;
900
+
901
+ // DateTime
902
+ case /h|m|s|y|d/i.test(section):
903
+ if (!condition) {
904
+ condition = 'type === "DateTime"';
905
+ }
906
+ formatCode.append(this.createDateTimeCode(section));
907
+ break;
908
+
909
+ }
910
+
911
+ // Add return statement
912
+ formatCode.append('\n return makeResult.call(this);\n ');
913
+
914
+ // Build final section code
915
+ if (condition) {
916
+ code.append('\n // Section\n if (' + condition + ') {\n ' + formatCode + '\n }\n // End section\n ');
917
+ } else {
918
+ code.append('\n // Section\n ' + formatCode + '\n // End section\n ');
919
+ }
920
+
921
+ return code.toString();
922
+ }
923
+ }, {
924
+ key: 'createPatternCode',
925
+ value: function createPatternCode(pattern) {
926
+ var _this4 = this;
927
+
928
+ var origins = [];
929
+ var replaces = '';
930
+
931
+ // Find quotes, slash symbols
932
+ var patternReplaced = pattern.replace(/"([^"]+)"|\\(.?)|(_.?)|(\*.?)|(")/g, function (a, m1, m2, m3) {
933
+ // Quote found
934
+ if (m1) {
935
+ origins.push(m1.replace(/("|'|\\)/g, "\\$1"));
936
+ return '[' + (replaces += '$') + ']';
937
+ }
938
+ // Slash found
939
+ if (m2) {
940
+ origins.push(m2.replace(/("|'|\\)/g, "\\$1"));
941
+ return '[' + (replaces += '$') + ']';
942
+ }
943
+ // Space found
944
+ if (m3) {
945
+ origins.push(' ');
946
+ return '[' + (replaces += '$') + ']';
947
+ }
948
+ return '';
949
+ });
950
+
951
+ // Split pattern to sections
952
+ var sections = patternReplaced.split(/;/);
953
+
954
+ // Init code
955
+ var code = new Code();
956
+
957
+ // Start variables
958
+ 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);
959
+
960
+ // Remove unnesessary sections
961
+ sections = sections.slice(0, 4);
962
+
963
+ // Loop trough sections
964
+ sections.forEach(function (section, sectionIndex) {
965
+ return code.append(_this4.createSectionCode(section, sectionIndex, sections.length));
966
+ });
967
+
968
+ // Return statement
969
+ code.append('\n result.value = {0};\n result.pattern = {0};\n return makeResult.call(this);\n ', patternReplaced);
970
+
971
+ return code.toString();
972
+ }
973
+ }, {
974
+ key: 'format',
975
+ value: function format(n, type, pattern) {
976
+ this.log(`Input:`);
977
+ this.log(`n = ${n}, type = ${type}, pattern = ${pattern}`);
978
+ var result=n
979
+ n = n.toString();
980
+ pattern = pattern.toString();
981
+ //pattern=pattern.replace(/"/g, "'");
982
+ // Find predefined format
983
+ if (this.locale.formats[pattern]) {
984
+ pattern = this.locale.formats[pattern];
985
+ }
986
+ try{
987
+ // Create function
988
+ if (!this.memoized[pattern]) {
989
+
990
+ let code = this.createPatternCode(pattern);
991
+
992
+ // Transform code
993
+ code = this.transformCode(code);
994
+
995
+ // Memoize function
996
+ this.memoized[pattern] = Function('n', 'type', code);
997
+
998
+ // Log code
999
+ this.log('Code:');
1000
+ this.log(code);
1001
+
1002
+ }
1003
+
1004
+ // Call function
1005
+ result = this.memoized[pattern].call(this, n, type);
1006
+ }
1007
+ catch (e) {
1008
+
1009
+ this.log(`Input:`,e);
1010
+
1011
+ }
1012
+ // Log result
1013
+ this.log('Result:');
1014
+ this.log(result);
1015
+
1016
+ return result;
1017
+ }
1018
+ }]);
1019
+
1020
+ return DataFormatterImpl;
1021
+ }();
1022
+
1023
1023
  module.exports = new DataFormatterImpl();