react-pivottable-plus 1.0.0

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/Utilities.js ADDED
@@ -0,0 +1,868 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.sortAs = exports.numberFormat = exports.naturalSort = exports.locales = exports.getSort = exports.flatKey = exports.derivers = exports.aggregators = exports.aggregatorTemplates = exports.PivotData = void 0;
8
+ var _propTypes = _interopRequireDefault(require("prop-types"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
10
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
11
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
12
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
15
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
16
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
17
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
18
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
19
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
20
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
21
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
22
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
23
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
24
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
25
+ /*
26
+ * decaffeinate suggestions:
27
+ * DS101: Remove unnecessary use of Array.from
28
+ * DS102: Remove unnecessary code created because of implicit returns
29
+ * DS104: Avoid inline assignments
30
+ * DS201: Simplify complex destructure assignments
31
+ * DS203: Remove `|| {}` from converted for-own loops
32
+ * DS205: Consider reworking code to avoid use of IIFEs
33
+ * DS207: Consider shorter variations of null checks
34
+ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
35
+ */
36
+
37
+ var addSeparators = function addSeparators(nStr, thousandsSep, decimalSep) {
38
+ var x = String(nStr).split('.');
39
+ var x1 = x[0];
40
+ var x2 = x.length > 1 ? decimalSep + x[1] : '';
41
+ var rgx = /(\d+)(\d{3})/;
42
+ while (rgx.test(x1)) {
43
+ x1 = x1.replace(rgx, "$1".concat(thousandsSep, "$2"));
44
+ }
45
+ return x1 + x2;
46
+ };
47
+ var numberFormat = exports.numberFormat = function numberFormat(opts_in) {
48
+ var defaults = {
49
+ digitsAfterDecimal: 2,
50
+ scaler: 1,
51
+ thousandsSep: ',',
52
+ decimalSep: '.',
53
+ prefix: '',
54
+ suffix: ''
55
+ };
56
+ var opts = Object.assign({}, defaults, opts_in);
57
+ return function (x) {
58
+ if (isNaN(x) || !isFinite(x)) {
59
+ return '';
60
+ }
61
+ var result = addSeparators((opts.scaler * x).toFixed(opts.digitsAfterDecimal), opts.thousandsSep, opts.decimalSep);
62
+ return "".concat(opts.prefix).concat(result).concat(opts.suffix);
63
+ };
64
+ };
65
+ var rx = /(\d+)|(\D+)/g;
66
+ var rd = /\d/;
67
+ var rz = /^0/;
68
+ var naturalSort = exports.naturalSort = function naturalSort(as, bs) {
69
+ // nulls first
70
+ if (bs !== null && as === null) {
71
+ return -1;
72
+ }
73
+ if (as !== null && bs === null) {
74
+ return 1;
75
+ }
76
+
77
+ // then raw NaNs
78
+ if (typeof as === 'number' && isNaN(as)) {
79
+ return -1;
80
+ }
81
+ if (typeof bs === 'number' && isNaN(bs)) {
82
+ return 1;
83
+ }
84
+
85
+ // numbers and numbery strings group together
86
+ var nas = Number(as);
87
+ var nbs = Number(bs);
88
+ if (nas < nbs) {
89
+ return -1;
90
+ }
91
+ if (nas > nbs) {
92
+ return 1;
93
+ }
94
+
95
+ // within that, true numbers before numbery strings
96
+ if (typeof as === 'number' && typeof bs !== 'number') {
97
+ return -1;
98
+ }
99
+ if (typeof bs === 'number' && typeof as !== 'number') {
100
+ return 1;
101
+ }
102
+ if (typeof as === 'number' && typeof bs === 'number') {
103
+ return 0;
104
+ }
105
+
106
+ // 'Infinity' is a textual number, so less than 'A'
107
+ if (isNaN(nbs) && !isNaN(nas)) {
108
+ return -1;
109
+ }
110
+ if (isNaN(nas) && !isNaN(nbs)) {
111
+ return 1;
112
+ }
113
+
114
+ // finally, "smart" string sorting per http://stackoverflow.com/a/4373421/112871
115
+ var a = String(as);
116
+ var b = String(bs);
117
+ if (a === b) {
118
+ return 0;
119
+ }
120
+ if (!rd.test(a) || !rd.test(b)) {
121
+ return a > b ? 1 : -1;
122
+ }
123
+
124
+ // special treatment for strings containing digits
125
+ a = a.match(rx);
126
+ b = b.match(rx);
127
+ while (a.length && b.length) {
128
+ var a1 = a.shift();
129
+ var b1 = b.shift();
130
+ if (a1 !== b1) {
131
+ if (rd.test(a1) && rd.test(b1)) {
132
+ return a1.replace(rz, '.0') - b1.replace(rz, '.0');
133
+ }
134
+ return a1 > b1 ? 1 : -1;
135
+ }
136
+ }
137
+ return a.length - b.length;
138
+ };
139
+ var sortAs = exports.sortAs = function sortAs(order) {
140
+ var mapping = {};
141
+
142
+ // sort lowercased keys similarly
143
+ var l_mapping = {};
144
+ for (var i in order) {
145
+ var x = order[i];
146
+ mapping[x] = i;
147
+ if (typeof x === 'string') {
148
+ l_mapping[x.toLowerCase()] = i;
149
+ }
150
+ }
151
+ return function (a, b) {
152
+ if (a in mapping && b in mapping) {
153
+ return mapping[a] - mapping[b];
154
+ } else if (a in mapping) {
155
+ return -1;
156
+ } else if (b in mapping) {
157
+ return 1;
158
+ } else if (a in l_mapping && b in l_mapping) {
159
+ return l_mapping[a] - l_mapping[b];
160
+ } else if (a in l_mapping) {
161
+ return -1;
162
+ } else if (b in l_mapping) {
163
+ return 1;
164
+ }
165
+ return naturalSort(a, b);
166
+ };
167
+ };
168
+ var getSort = exports.getSort = function getSort(sorters, attr) {
169
+ if (sorters) {
170
+ if (typeof sorters === 'function') {
171
+ var sort = sorters(attr);
172
+ if (typeof sort === 'function') {
173
+ return sort;
174
+ }
175
+ } else if (attr in sorters) {
176
+ return sorters[attr];
177
+ }
178
+ }
179
+ return naturalSort;
180
+ };
181
+
182
+ // aggregator templates default to US number formatting but this is overrideable
183
+ var usFmt = numberFormat();
184
+ var usFmtInt = numberFormat({
185
+ digitsAfterDecimal: 0
186
+ });
187
+ var usFmtPct = numberFormat({
188
+ digitsAfterDecimal: 1,
189
+ scaler: 100,
190
+ suffix: '%'
191
+ });
192
+ var aggregatorTemplates = exports.aggregatorTemplates = {
193
+ count: function count() {
194
+ var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmtInt;
195
+ return function () {
196
+ return function () {
197
+ return {
198
+ count: 0,
199
+ push: function push() {
200
+ this.count++;
201
+ },
202
+ value: function value() {
203
+ return this.count;
204
+ },
205
+ format: formatter
206
+ };
207
+ };
208
+ };
209
+ },
210
+ uniques: function uniques(fn) {
211
+ var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmtInt;
212
+ return function (_ref) {
213
+ var _ref2 = _slicedToArray(_ref, 1),
214
+ attr = _ref2[0];
215
+ return function () {
216
+ return {
217
+ uniq: [],
218
+ push: function push(record) {
219
+ if (!Array.from(this.uniq).includes(record[attr])) {
220
+ this.uniq.push(record[attr]);
221
+ }
222
+ },
223
+ value: function value() {
224
+ return fn(this.uniq);
225
+ },
226
+ format: formatter,
227
+ numInputs: typeof attr !== 'undefined' ? 0 : 1
228
+ };
229
+ };
230
+ };
231
+ },
232
+ sum: function sum() {
233
+ var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmt;
234
+ return function (_ref3) {
235
+ var _ref4 = _slicedToArray(_ref3, 1),
236
+ attr = _ref4[0];
237
+ return function () {
238
+ return {
239
+ sum: 0,
240
+ push: function push(record) {
241
+ if (!isNaN(parseFloat(record[attr]))) {
242
+ this.sum += parseFloat(record[attr]);
243
+ }
244
+ },
245
+ value: function value() {
246
+ return this.sum;
247
+ },
248
+ format: formatter,
249
+ numInputs: typeof attr !== 'undefined' ? 0 : 1
250
+ };
251
+ };
252
+ };
253
+ },
254
+ extremes: function extremes(mode) {
255
+ var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmt;
256
+ return function (_ref5) {
257
+ var _ref6 = _slicedToArray(_ref5, 1),
258
+ attr = _ref6[0];
259
+ return function (data) {
260
+ return {
261
+ val: null,
262
+ sorter: getSort(typeof data !== 'undefined' ? data.sorters : null, attr),
263
+ push: function push(record) {
264
+ var x = record[attr];
265
+ if (['min', 'max'].includes(mode)) {
266
+ x = parseFloat(x);
267
+ if (!isNaN(x)) {
268
+ this.val = Math[mode](x, this.val !== null ? this.val : x);
269
+ }
270
+ }
271
+ if (mode === 'first' && this.sorter(x, this.val !== null ? this.val : x) <= 0) {
272
+ this.val = x;
273
+ }
274
+ if (mode === 'last' && this.sorter(x, this.val !== null ? this.val : x) >= 0) {
275
+ this.val = x;
276
+ }
277
+ },
278
+ value: function value() {
279
+ return this.val;
280
+ },
281
+ format: function format(x) {
282
+ if (isNaN(x)) {
283
+ return x;
284
+ }
285
+ return formatter(x);
286
+ },
287
+ numInputs: typeof attr !== 'undefined' ? 0 : 1
288
+ };
289
+ };
290
+ };
291
+ },
292
+ quantile: function quantile(q) {
293
+ var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmt;
294
+ return function (_ref7) {
295
+ var _ref8 = _slicedToArray(_ref7, 1),
296
+ attr = _ref8[0];
297
+ return function () {
298
+ return {
299
+ vals: [],
300
+ push: function push(record) {
301
+ var x = parseFloat(record[attr]);
302
+ if (!isNaN(x)) {
303
+ this.vals.push(x);
304
+ }
305
+ },
306
+ value: function value() {
307
+ if (this.vals.length === 0) {
308
+ return null;
309
+ }
310
+ this.vals.sort(function (a, b) {
311
+ return a - b;
312
+ });
313
+ var i = (this.vals.length - 1) * q;
314
+ return (this.vals[Math.floor(i)] + this.vals[Math.ceil(i)]) / 2.0;
315
+ },
316
+ format: formatter,
317
+ numInputs: typeof attr !== 'undefined' ? 0 : 1
318
+ };
319
+ };
320
+ };
321
+ },
322
+ runningStat: function runningStat() {
323
+ var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'mean';
324
+ var ddof = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
325
+ var formatter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : usFmt;
326
+ return function (_ref9) {
327
+ var _ref0 = _slicedToArray(_ref9, 1),
328
+ attr = _ref0[0];
329
+ return function () {
330
+ return {
331
+ n: 0.0,
332
+ m: 0.0,
333
+ s: 0.0,
334
+ push: function push(record) {
335
+ var x = parseFloat(record[attr]);
336
+ if (isNaN(x)) {
337
+ return;
338
+ }
339
+ this.n += 1.0;
340
+ if (this.n === 1.0) {
341
+ this.m = x;
342
+ }
343
+ var m_new = this.m + (x - this.m) / this.n;
344
+ this.s = this.s + (x - this.m) * (x - m_new);
345
+ this.m = m_new;
346
+ },
347
+ value: function value() {
348
+ if (mode === 'mean') {
349
+ if (this.n === 0) {
350
+ return 0 / 0;
351
+ }
352
+ return this.m;
353
+ }
354
+ if (this.n <= ddof) {
355
+ return 0;
356
+ }
357
+ switch (mode) {
358
+ case 'var':
359
+ return this.s / (this.n - ddof);
360
+ case 'stdev':
361
+ return Math.sqrt(this.s / (this.n - ddof));
362
+ default:
363
+ throw new Error('unknown mode for runningStat');
364
+ }
365
+ },
366
+ format: formatter,
367
+ numInputs: typeof attr !== 'undefined' ? 0 : 1
368
+ };
369
+ };
370
+ };
371
+ },
372
+ sumOverSum: function sumOverSum() {
373
+ var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmt;
374
+ return function (_ref1) {
375
+ var _ref10 = _slicedToArray(_ref1, 2),
376
+ num = _ref10[0],
377
+ denom = _ref10[1];
378
+ return function () {
379
+ return {
380
+ sumNum: 0,
381
+ sumDenom: 0,
382
+ push: function push(record) {
383
+ if (!isNaN(parseFloat(record[num]))) {
384
+ this.sumNum += parseFloat(record[num]);
385
+ }
386
+ if (!isNaN(parseFloat(record[denom]))) {
387
+ this.sumDenom += parseFloat(record[denom]);
388
+ }
389
+ },
390
+ value: function value() {
391
+ return this.sumNum / this.sumDenom;
392
+ },
393
+ format: formatter,
394
+ numInputs: typeof num !== 'undefined' && typeof denom !== 'undefined' ? 0 : 2
395
+ };
396
+ };
397
+ };
398
+ },
399
+ fractionOf: function fractionOf(wrapped) {
400
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'total';
401
+ var formatter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : usFmtPct;
402
+ return function () {
403
+ for (var _len = arguments.length, x = new Array(_len), _key = 0; _key < _len; _key++) {
404
+ x[_key] = arguments[_key];
405
+ }
406
+ return function (data, rowKey, colKey) {
407
+ return {
408
+ selector: {
409
+ total: [[], []],
410
+ row: [rowKey, []],
411
+ col: [[], colKey]
412
+ }[type],
413
+ inner: wrapped.apply(void 0, _toConsumableArray(Array.from(x || [])))(data, rowKey, colKey),
414
+ push: function push(record) {
415
+ this.inner.push(record);
416
+ },
417
+ format: formatter,
418
+ value: function value() {
419
+ return this.inner.value() / data.getAggregator.apply(data, _toConsumableArray(Array.from(this.selector || []))).inner.value();
420
+ },
421
+ numInputs: wrapped.apply(void 0, _toConsumableArray(Array.from(x || [])))().numInputs
422
+ };
423
+ };
424
+ };
425
+ }
426
+ };
427
+ aggregatorTemplates.countUnique = function (f) {
428
+ return aggregatorTemplates.uniques(function (x) {
429
+ return x.length;
430
+ }, f);
431
+ };
432
+ aggregatorTemplates.listUnique = function (s) {
433
+ return aggregatorTemplates.uniques(function (x) {
434
+ return x.join(s);
435
+ }, function (x) {
436
+ return x;
437
+ });
438
+ };
439
+ aggregatorTemplates.max = function (f) {
440
+ return aggregatorTemplates.extremes('max', f);
441
+ };
442
+ aggregatorTemplates.min = function (f) {
443
+ return aggregatorTemplates.extremes('min', f);
444
+ };
445
+ aggregatorTemplates.first = function (f) {
446
+ return aggregatorTemplates.extremes('first', f);
447
+ };
448
+ aggregatorTemplates.last = function (f) {
449
+ return aggregatorTemplates.extremes('last', f);
450
+ };
451
+ aggregatorTemplates.median = function (f) {
452
+ return aggregatorTemplates.quantile(0.5, f);
453
+ };
454
+ aggregatorTemplates.average = function (f) {
455
+ return aggregatorTemplates.runningStat('mean', 1, f);
456
+ };
457
+ aggregatorTemplates["var"] = function (ddof, f) {
458
+ return aggregatorTemplates.runningStat('var', ddof, f);
459
+ };
460
+ aggregatorTemplates.stdev = function (ddof, f) {
461
+ return aggregatorTemplates.runningStat('stdev', ddof, f);
462
+ };
463
+
464
+ // default aggregators & renderers use US naming and number formatting
465
+ var aggregators = exports.aggregators = function (tpl) {
466
+ return {
467
+ Count: tpl.count(usFmtInt),
468
+ 'Count Unique Values': tpl.countUnique(usFmtInt),
469
+ 'List Unique Values': tpl.listUnique(', '),
470
+ Sum: tpl.sum(usFmt),
471
+ 'Integer Sum': tpl.sum(usFmtInt),
472
+ Average: tpl.average(usFmt),
473
+ Median: tpl.median(usFmt),
474
+ 'Sample Variance': tpl["var"](1, usFmt),
475
+ 'Sample Standard Deviation': tpl.stdev(1, usFmt),
476
+ Minimum: tpl.min(usFmt),
477
+ Maximum: tpl.max(usFmt),
478
+ First: tpl.first(usFmt),
479
+ Last: tpl.last(usFmt),
480
+ 'Sum over Sum': tpl.sumOverSum(usFmt),
481
+ 'Sum as Fraction of Total': tpl.fractionOf(tpl.sum(), 'total', usFmtPct),
482
+ 'Sum as Fraction of Rows': tpl.fractionOf(tpl.sum(), 'row', usFmtPct),
483
+ 'Sum as Fraction of Columns': tpl.fractionOf(tpl.sum(), 'col', usFmtPct),
484
+ 'Count as Fraction of Total': tpl.fractionOf(tpl.count(), 'total', usFmtPct),
485
+ 'Count as Fraction of Rows': tpl.fractionOf(tpl.count(), 'row', usFmtPct),
486
+ 'Count as Fraction of Columns': tpl.fractionOf(tpl.count(), 'col', usFmtPct)
487
+ };
488
+ }(aggregatorTemplates);
489
+ var locales = exports.locales = {
490
+ en: {
491
+ aggregators: aggregators,
492
+ localeStrings: {
493
+ renderError: 'An error occurred rendering the PivotTable results.',
494
+ computeError: 'An error occurred computing the PivotTable results.',
495
+ uiRenderError: 'An error occurred rendering the PivotTable UI.',
496
+ selectAll: 'Select All',
497
+ selectNone: 'Select None',
498
+ tooMany: '(too many to list)',
499
+ filterResults: 'Filter values',
500
+ apply: 'Apply',
501
+ cancel: 'Cancel',
502
+ totals: 'Totals',
503
+ vs: 'vs',
504
+ by: 'by'
505
+ }
506
+ }
507
+ };
508
+
509
+ // dateFormat deriver l10n requires month and day names to be passed in directly
510
+ var mthNamesEn = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
511
+ var dayNamesEn = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
512
+ var zeroPad = function zeroPad(number) {
513
+ return "0".concat(number).substr(-2, 2);
514
+ }; // eslint-disable-line no-magic-numbers
515
+
516
+ var derivers = exports.derivers = {
517
+ bin: function bin(col, binWidth) {
518
+ return function (record) {
519
+ return record[col] - record[col] % binWidth;
520
+ };
521
+ },
522
+ dateFormat: function dateFormat(col, formatString) {
523
+ var utcOutput = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
524
+ var mthNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : mthNamesEn;
525
+ var dayNames = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : dayNamesEn;
526
+ var utc = utcOutput ? 'UTC' : '';
527
+ return function (record) {
528
+ var date = new Date(Date.parse(record[col]));
529
+ if (isNaN(date)) {
530
+ return '';
531
+ }
532
+ return formatString.replace(/%(.)/g, function (m, p) {
533
+ switch (p) {
534
+ case 'y':
535
+ return date["get".concat(utc, "FullYear")]();
536
+ case 'm':
537
+ return zeroPad(date["get".concat(utc, "Month")]() + 1);
538
+ case 'n':
539
+ return mthNames[date["get".concat(utc, "Month")]()];
540
+ case 'd':
541
+ return zeroPad(date["get".concat(utc, "Date")]());
542
+ case 'w':
543
+ return dayNames[date["get".concat(utc, "Day")]()];
544
+ case 'x':
545
+ return date["get".concat(utc, "Day")]();
546
+ case 'H':
547
+ return zeroPad(date["get".concat(utc, "Hours")]());
548
+ case 'M':
549
+ return zeroPad(date["get".concat(utc, "Minutes")]());
550
+ case 'S':
551
+ return zeroPad(date["get".concat(utc, "Seconds")]());
552
+ default:
553
+ return "%".concat(p);
554
+ }
555
+ });
556
+ };
557
+ }
558
+ };
559
+
560
+ // Given an array of attribute values, convert to a key that
561
+ // can be used in objects.
562
+ var flatKey = exports.flatKey = function flatKey(attrVals) {
563
+ return attrVals.join(String.fromCharCode(0));
564
+ };
565
+
566
+ /*
567
+ Data Model class
568
+ */
569
+ var PivotData = exports.PivotData = /*#__PURE__*/function () {
570
+ function PivotData() {
571
+ var _this = this;
572
+ var inputProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
573
+ _classCallCheck(this, PivotData);
574
+ this.props = Object.assign({}, PivotData.defaultProps, inputProps);
575
+ _propTypes["default"].checkPropTypes(PivotData.propTypes, this.props, 'prop', 'PivotData');
576
+ this.aggregator = this.props.aggregators[this.props.aggregatorName](this.props.vals);
577
+ this.tree = {};
578
+ this.rowKeys = [];
579
+ this.colKeys = [];
580
+ this.rowTotals = {};
581
+ this.colTotals = {};
582
+ this.allTotal = this.aggregator(this, [], []);
583
+ this.sorted = false;
584
+ PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
585
+ if (_this.filter(record)) {
586
+ _this.processRecord(record);
587
+ }
588
+ });
589
+ }
590
+ return _createClass(PivotData, [{
591
+ key: "filter",
592
+ value: function filter(record) {
593
+ for (var k in this.props.valueFilter) {
594
+ if (record[k] in this.props.valueFilter[k]) {
595
+ return false;
596
+ }
597
+ }
598
+ return true;
599
+ }
600
+ }, {
601
+ key: "forEachMatchingRecord",
602
+ value: function forEachMatchingRecord(criteria, callback) {
603
+ var _this2 = this;
604
+ return PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
605
+ if (!_this2.filter(record)) {
606
+ return;
607
+ }
608
+ for (var k in criteria) {
609
+ var v = criteria[k];
610
+ if (v !== (k in record ? record[k] : 'null')) {
611
+ return;
612
+ }
613
+ }
614
+ callback(record);
615
+ });
616
+ }
617
+ }, {
618
+ key: "arrSort",
619
+ value: function arrSort(attrs) {
620
+ var _this3 = this;
621
+ var a;
622
+ var sortersArr = function () {
623
+ var result = [];
624
+ for (var _i = 0, _Array$from = Array.from(attrs); _i < _Array$from.length; _i++) {
625
+ a = _Array$from[_i];
626
+ result.push(getSort(_this3.props.sorters, a));
627
+ }
628
+ return result;
629
+ }();
630
+ return function (a, b) {
631
+ for (var _i2 = 0, _Object$keys = Object.keys(sortersArr || {}); _i2 < _Object$keys.length; _i2++) {
632
+ var i = _Object$keys[_i2];
633
+ var sorter = sortersArr[i];
634
+ var comparison = sorter(a[i], b[i]);
635
+ if (comparison !== 0) {
636
+ return comparison;
637
+ }
638
+ }
639
+ return 0;
640
+ };
641
+ }
642
+ }, {
643
+ key: "sortKeys",
644
+ value: function sortKeys() {
645
+ var _this4 = this;
646
+ if (!this.sorted) {
647
+ this.sorted = true;
648
+ var v = function v(r, c) {
649
+ return _this4.getAggregator(r, c).value();
650
+ };
651
+ switch (this.props.rowOrder) {
652
+ case 'value_a_to_z':
653
+ this.rowKeys.sort(function (a, b) {
654
+ return naturalSort(v(a, []), v(b, []));
655
+ });
656
+ break;
657
+ case 'value_z_to_a':
658
+ this.rowKeys.sort(function (a, b) {
659
+ return -naturalSort(v(a, []), v(b, []));
660
+ });
661
+ break;
662
+ default:
663
+ this.rowKeys.sort(this.arrSort(this.props.rows));
664
+ }
665
+ switch (this.props.colOrder) {
666
+ case 'value_a_to_z':
667
+ this.colKeys.sort(function (a, b) {
668
+ return naturalSort(v([], a), v([], b));
669
+ });
670
+ break;
671
+ case 'value_z_to_a':
672
+ this.colKeys.sort(function (a, b) {
673
+ return -naturalSort(v([], a), v([], b));
674
+ });
675
+ break;
676
+ default:
677
+ this.colKeys.sort(this.arrSort(this.props.cols));
678
+ }
679
+ }
680
+ }
681
+ }, {
682
+ key: "getColKeys",
683
+ value: function getColKeys() {
684
+ this.sortKeys();
685
+ return this.colKeys;
686
+ }
687
+ }, {
688
+ key: "getRowKeys",
689
+ value: function getRowKeys() {
690
+ this.sortKeys();
691
+ return this.rowKeys;
692
+ }
693
+ }, {
694
+ key: "processRecord",
695
+ value: function processRecord(record) {
696
+ // this code is called in a tight loop
697
+ var colKey = [];
698
+ var rowKey = [];
699
+ for (var _i3 = 0, _Array$from2 = Array.from(this.props.cols); _i3 < _Array$from2.length; _i3++) {
700
+ var x = _Array$from2[_i3];
701
+ colKey.push(x in record ? record[x] : 'null');
702
+ }
703
+ for (var _i4 = 0, _Array$from3 = Array.from(this.props.rows); _i4 < _Array$from3.length; _i4++) {
704
+ var _x = _Array$from3[_i4];
705
+ rowKey.push(_x in record ? record[_x] : 'null');
706
+ }
707
+ var flatRowKey = rowKey.join(String.fromCharCode(0));
708
+ var flatColKey = colKey.join(String.fromCharCode(0));
709
+ this.allTotal.push(record);
710
+ if (rowKey.length !== 0) {
711
+ if (!this.rowTotals[flatRowKey]) {
712
+ this.rowKeys.push(rowKey);
713
+ this.rowTotals[flatRowKey] = this.aggregator(this, rowKey, []);
714
+ }
715
+ this.rowTotals[flatRowKey].push(record);
716
+ }
717
+ if (colKey.length !== 0) {
718
+ if (!this.colTotals[flatColKey]) {
719
+ this.colKeys.push(colKey);
720
+ this.colTotals[flatColKey] = this.aggregator(this, [], colKey);
721
+ }
722
+ this.colTotals[flatColKey].push(record);
723
+ }
724
+ if (colKey.length !== 0 && rowKey.length !== 0) {
725
+ if (!this.tree[flatRowKey]) {
726
+ this.tree[flatRowKey] = {};
727
+ }
728
+ if (!this.tree[flatRowKey][flatColKey]) {
729
+ this.tree[flatRowKey][flatColKey] = this.aggregator(this, rowKey, colKey);
730
+ }
731
+ this.tree[flatRowKey][flatColKey].push(record);
732
+ }
733
+ }
734
+ }, {
735
+ key: "getAggregator",
736
+ value: function getAggregator(rowKey, colKey) {
737
+ var agg;
738
+ var flatRowKey = rowKey.join(String.fromCharCode(0));
739
+ var flatColKey = colKey.join(String.fromCharCode(0));
740
+ if (rowKey.length === 0 && colKey.length === 0) {
741
+ agg = this.allTotal;
742
+ } else if (rowKey.length === 0) {
743
+ agg = this.colTotals[flatColKey];
744
+ } else if (colKey.length === 0) {
745
+ agg = this.rowTotals[flatRowKey];
746
+ } else {
747
+ agg = this.tree[flatRowKey][flatColKey];
748
+ }
749
+ return agg || {
750
+ value: function value() {
751
+ return null;
752
+ },
753
+ format: function format() {
754
+ return '';
755
+ }
756
+ };
757
+ }
758
+ }, {
759
+ key: "forEachTotal",
760
+ value: function forEachTotal(callback) {
761
+ // Process row totals
762
+ this.getRowKeys().forEach(function (rowKey, i) {
763
+ callback([rowKey, null], i);
764
+ });
765
+
766
+ // Process column totals
767
+ this.getColKeys().forEach(function (colKey, i) {
768
+ callback([null, colKey], i);
769
+ });
770
+ }
771
+ }, {
772
+ key: "forEachCell",
773
+ value: function forEachCell(callback) {
774
+ var _this5 = this;
775
+ this.getRowKeys().forEach(function (rowKey) {
776
+ _this5.getColKeys().forEach(function (colKey) {
777
+ var agg = _this5.getAggregator(rowKey, colKey);
778
+ var value = agg.value();
779
+ callback(value, rowKey, colKey);
780
+ });
781
+ });
782
+ }
783
+ }]);
784
+ }(); // can handle arrays or jQuery selections of tables
785
+ PivotData.forEachRecord = function (input, derivedAttributes, f) {
786
+ var addRecord, record;
787
+ if (Object.getOwnPropertyNames(derivedAttributes).length === 0) {
788
+ addRecord = f;
789
+ } else {
790
+ addRecord = function addRecord(record) {
791
+ for (var k in derivedAttributes) {
792
+ var derived = derivedAttributes[k](record);
793
+ if (derived !== null) {
794
+ record[k] = derived;
795
+ }
796
+ }
797
+ return f(record);
798
+ };
799
+ }
800
+
801
+ // if it's a function, have it call us back
802
+ if (typeof input === 'function') {
803
+ return input(addRecord);
804
+ } else if (Array.isArray(input)) {
805
+ if (Array.isArray(input[0])) {
806
+ // array of arrays
807
+ return function () {
808
+ var result = [];
809
+ for (var _i5 = 0, _Object$keys2 = Object.keys(input || {}); _i5 < _Object$keys2.length; _i5++) {
810
+ var i = _Object$keys2[_i5];
811
+ var compactRecord = input[i];
812
+ if (i > 0) {
813
+ record = {};
814
+ for (var _i6 = 0, _Object$keys3 = Object.keys(input[0] || {}); _i6 < _Object$keys3.length; _i6++) {
815
+ var j = _Object$keys3[_i6];
816
+ var k = input[0][j];
817
+ record[k] = compactRecord[j];
818
+ }
819
+ result.push(addRecord(record));
820
+ }
821
+ }
822
+ return result;
823
+ }();
824
+ }
825
+
826
+ // array of objects
827
+ return function () {
828
+ var result1 = [];
829
+ for (var _i7 = 0, _Array$from4 = Array.from(input); _i7 < _Array$from4.length; _i7++) {
830
+ record = _Array$from4[_i7];
831
+ result1.push(addRecord(record));
832
+ }
833
+ return result1;
834
+ }();
835
+ }
836
+ throw new Error('unknown input format');
837
+ };
838
+ PivotData.defaultProps = {
839
+ aggregators: aggregators,
840
+ cols: [],
841
+ rows: [],
842
+ vals: [],
843
+ aggregatorName: 'Count',
844
+ sorters: {},
845
+ valueFilter: {},
846
+ rowOrder: 'key_a_to_z',
847
+ colOrder: 'key_a_to_z',
848
+ derivedAttributes: {},
849
+ pagination: false,
850
+ pageSize: 20,
851
+ page: 1
852
+ };
853
+ PivotData.propTypes = {
854
+ data: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].object, _propTypes["default"].func]).isRequired,
855
+ aggregatorName: _propTypes["default"].string,
856
+ cols: _propTypes["default"].arrayOf(_propTypes["default"].string),
857
+ rows: _propTypes["default"].arrayOf(_propTypes["default"].string),
858
+ vals: _propTypes["default"].arrayOf(_propTypes["default"].string),
859
+ valueFilter: _propTypes["default"].objectOf(_propTypes["default"].objectOf(_propTypes["default"].bool)),
860
+ sorters: _propTypes["default"].oneOfType([_propTypes["default"].func, _propTypes["default"].objectOf(_propTypes["default"].func)]),
861
+ derivedAttributes: _propTypes["default"].objectOf(_propTypes["default"].func),
862
+ rowOrder: _propTypes["default"].oneOf(['key_a_to_z', 'value_a_to_z', 'value_z_to_a']),
863
+ colOrder: _propTypes["default"].oneOf(['key_a_to_z', 'value_a_to_z', 'value_z_to_a']),
864
+ pagination: _propTypes["default"].bool,
865
+ pageSize: _propTypes["default"].number,
866
+ page: _propTypes["default"].number
867
+ };
868
+ //# sourceMappingURL=Utilities.js.map