1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.PivotData = exports.sortAs = exports.getSort = exports.numberFormat = exports.naturalSort = exports.locales = exports.derivers = exports.aggregators = exports.aggregatorTemplates = undefined;
|
7 |
|
8 | 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; }; }();
|
9 |
|
10 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
11 |
|
12 | var _propTypes = require('prop-types');
|
13 |
|
14 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
19 |
|
20 | 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); } }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | var addSeparators = function addSeparators(nStr, thousandsSep, decimalSep) {
|
35 | var x = String(nStr).split('.');
|
36 | var x1 = x[0];
|
37 | var x2 = x.length > 1 ? decimalSep + x[1] : '';
|
38 | var rgx = /(\d+)(\d{3})/;
|
39 | while (rgx.test(x1)) {
|
40 | x1 = x1.replace(rgx, '$1' + thousandsSep + '$2');
|
41 | }
|
42 | return x1 + x2;
|
43 | };
|
44 |
|
45 | var numberFormat = function numberFormat(opts_in) {
|
46 | var defaults = {
|
47 | digitsAfterDecimal: 2,
|
48 | scaler: 1,
|
49 | thousandsSep: ',',
|
50 | decimalSep: '.',
|
51 | prefix: '',
|
52 | suffix: ''
|
53 | };
|
54 | var opts = Object.assign({}, defaults, opts_in);
|
55 | return function (x) {
|
56 | if (isNaN(x) || !isFinite(x)) {
|
57 | return '';
|
58 | }
|
59 | var result = addSeparators((opts.scaler * x).toFixed(opts.digitsAfterDecimal), opts.thousandsSep, opts.decimalSep);
|
60 | return '' + opts.prefix + result + opts.suffix;
|
61 | };
|
62 | };
|
63 |
|
64 | var rx = /(\d+)|(\D+)/g;
|
65 | var rd = /\d/;
|
66 | var rz = /^0/;
|
67 | var naturalSort = function naturalSort(as, bs) {
|
68 |
|
69 | if (bs !== null && as === null) {
|
70 | return -1;
|
71 | }
|
72 | if (as !== null && bs === null) {
|
73 | return 1;
|
74 | }
|
75 |
|
76 |
|
77 | if (typeof as === 'number' && isNaN(as)) {
|
78 | return -1;
|
79 | }
|
80 | if (typeof bs === 'number' && isNaN(bs)) {
|
81 | return 1;
|
82 | }
|
83 |
|
84 |
|
85 | var nas = Number(as);
|
86 | var nbs = Number(bs);
|
87 | if (nas < nbs) {
|
88 | return -1;
|
89 | }
|
90 | if (nas > nbs) {
|
91 | return 1;
|
92 | }
|
93 |
|
94 |
|
95 | if (typeof as === 'number' && typeof bs !== 'number') {
|
96 | return -1;
|
97 | }
|
98 | if (typeof bs === 'number' && typeof as !== 'number') {
|
99 | return 1;
|
100 | }
|
101 | if (typeof as === 'number' && typeof bs === 'number') {
|
102 | return 0;
|
103 | }
|
104 |
|
105 |
|
106 | if (isNaN(nbs) && !isNaN(nas)) {
|
107 | return -1;
|
108 | }
|
109 | if (isNaN(nas) && !isNaN(nbs)) {
|
110 | return 1;
|
111 | }
|
112 |
|
113 |
|
114 | var a = String(as);
|
115 | var b = String(bs);
|
116 | if (a === b) {
|
117 | return 0;
|
118 | }
|
119 | if (!rd.test(a) || !rd.test(b)) {
|
120 | return a > b ? 1 : -1;
|
121 | }
|
122 |
|
123 |
|
124 | a = a.match(rx);
|
125 | b = b.match(rx);
|
126 | while (a.length && b.length) {
|
127 | var a1 = a.shift();
|
128 | var b1 = b.shift();
|
129 | if (a1 !== b1) {
|
130 | if (rd.test(a1) && rd.test(b1)) {
|
131 | return a1.replace(rz, '.0') - b1.replace(rz, '.0');
|
132 | }
|
133 | return a1 > b1 ? 1 : -1;
|
134 | }
|
135 | }
|
136 | return a.length - b.length;
|
137 | };
|
138 |
|
139 | var sortAs = function sortAs(order) {
|
140 | var mapping = {};
|
141 |
|
142 |
|
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 |
|
169 | var getSort = function getSort(sorters, attr) {
|
170 | if (sorters) {
|
171 | if (typeof sorters === 'function') {
|
172 | var sort = sorters(attr);
|
173 | if (typeof sort === 'function') {
|
174 | return sort;
|
175 | }
|
176 | } else if (attr in sorters) {
|
177 | return sorters[attr];
|
178 | }
|
179 | }
|
180 | return naturalSort;
|
181 | };
|
182 |
|
183 |
|
184 | var usFmt = numberFormat();
|
185 | var usFmtInt = numberFormat({ digitsAfterDecimal: 0 });
|
186 | var usFmtPct = numberFormat({
|
187 | digitsAfterDecimal: 1,
|
188 | scaler: 100,
|
189 | suffix: '%'
|
190 | });
|
191 |
|
192 | var aggregatorTemplates = {
|
193 | count: function count() {
|
194 | var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmtInt;
|
195 |
|
196 | return function () {
|
197 | return function () {
|
198 | return {
|
199 | count: 0,
|
200 | push: function push() {
|
201 | this.count++;
|
202 | },
|
203 | value: function value() {
|
204 | return this.count;
|
205 | },
|
206 |
|
207 | format: formatter
|
208 | };
|
209 | };
|
210 | };
|
211 | },
|
212 | uniques: function uniques(fn) {
|
213 | var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmtInt;
|
214 |
|
215 | return function (_ref) {
|
216 | var _ref2 = _slicedToArray(_ref, 1),
|
217 | attr = _ref2[0];
|
218 |
|
219 | return function () {
|
220 | return {
|
221 | uniq: [],
|
222 | push: function push(record) {
|
223 | if (!Array.from(this.uniq).includes(record[attr])) {
|
224 | this.uniq.push(record[attr]);
|
225 | }
|
226 | },
|
227 | value: function value() {
|
228 | return fn(this.uniq);
|
229 | },
|
230 |
|
231 | format: formatter,
|
232 | numInputs: typeof attr !== 'undefined' ? 0 : 1
|
233 | };
|
234 | };
|
235 | };
|
236 | },
|
237 | sum: function sum() {
|
238 | var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmt;
|
239 |
|
240 | return function (_ref3) {
|
241 | var _ref4 = _slicedToArray(_ref3, 1),
|
242 | attr = _ref4[0];
|
243 |
|
244 | return function () {
|
245 | return {
|
246 | sum: 0,
|
247 | push: function push(record) {
|
248 | if (!isNaN(parseFloat(record[attr]))) {
|
249 | this.sum += parseFloat(record[attr]);
|
250 | }
|
251 | },
|
252 | value: function value() {
|
253 | return this.sum;
|
254 | },
|
255 |
|
256 | format: formatter,
|
257 | numInputs: typeof attr !== 'undefined' ? 0 : 1
|
258 | };
|
259 | };
|
260 | };
|
261 | },
|
262 | extremes: function extremes(mode) {
|
263 | var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmt;
|
264 |
|
265 | return function (_ref5) {
|
266 | var _ref6 = _slicedToArray(_ref5, 1),
|
267 | attr = _ref6[0];
|
268 |
|
269 | return function (data) {
|
270 | return {
|
271 | val: null,
|
272 | sorter: getSort(typeof data !== 'undefined' ? data.sorters : null, attr),
|
273 | push: function push(record) {
|
274 | var x = record[attr];
|
275 | if (['min', 'max'].includes(mode)) {
|
276 | x = parseFloat(x);
|
277 | if (!isNaN(x)) {
|
278 | this.val = Math[mode](x, this.val !== null ? this.val : x);
|
279 | }
|
280 | }
|
281 | if (mode === 'first' && this.sorter(x, this.val !== null ? this.val : x) <= 0) {
|
282 | this.val = x;
|
283 | }
|
284 | if (mode === 'last' && this.sorter(x, this.val !== null ? this.val : x) >= 0) {
|
285 | this.val = x;
|
286 | }
|
287 | },
|
288 | value: function value() {
|
289 | return this.val;
|
290 | },
|
291 | format: function format(x) {
|
292 | if (isNaN(x)) {
|
293 | return x;
|
294 | }
|
295 | return formatter(x);
|
296 | },
|
297 |
|
298 | numInputs: typeof attr !== 'undefined' ? 0 : 1
|
299 | };
|
300 | };
|
301 | };
|
302 | },
|
303 | quantile: function quantile(q) {
|
304 | var formatter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : usFmt;
|
305 |
|
306 | return function (_ref7) {
|
307 | var _ref8 = _slicedToArray(_ref7, 1),
|
308 | attr = _ref8[0];
|
309 |
|
310 | return function () {
|
311 | return {
|
312 | vals: [],
|
313 | push: function push(record) {
|
314 | var x = parseFloat(record[attr]);
|
315 | if (!isNaN(x)) {
|
316 | this.vals.push(x);
|
317 | }
|
318 | },
|
319 | value: function value() {
|
320 | if (this.vals.length === 0) {
|
321 | return null;
|
322 | }
|
323 | this.vals.sort(function (a, b) {
|
324 | return a - b;
|
325 | });
|
326 | var i = (this.vals.length - 1) * q;
|
327 | return (this.vals[Math.floor(i)] + this.vals[Math.ceil(i)]) / 2.0;
|
328 | },
|
329 |
|
330 | format: formatter,
|
331 | numInputs: typeof attr !== 'undefined' ? 0 : 1
|
332 | };
|
333 | };
|
334 | };
|
335 | },
|
336 | runningStat: function runningStat() {
|
337 | var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'mean';
|
338 | var ddof = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
339 | var formatter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : usFmt;
|
340 |
|
341 | return function (_ref9) {
|
342 | var _ref10 = _slicedToArray(_ref9, 1),
|
343 | attr = _ref10[0];
|
344 |
|
345 | return function () {
|
346 | return {
|
347 | n: 0.0,
|
348 | m: 0.0,
|
349 | s: 0.0,
|
350 | push: function push(record) {
|
351 | var x = parseFloat(record[attr]);
|
352 | if (isNaN(x)) {
|
353 | return;
|
354 | }
|
355 | this.n += 1.0;
|
356 | if (this.n === 1.0) {
|
357 | this.m = x;
|
358 | }
|
359 | var m_new = this.m + (x - this.m) / this.n;
|
360 | this.s = this.s + (x - this.m) * (x - m_new);
|
361 | this.m = m_new;
|
362 | },
|
363 | value: function value() {
|
364 | if (mode === 'mean') {
|
365 | if (this.n === 0) {
|
366 | return 0 / 0;
|
367 | }
|
368 | return this.m;
|
369 | }
|
370 | if (this.n <= ddof) {
|
371 | return 0;
|
372 | }
|
373 | switch (mode) {
|
374 | case 'var':
|
375 | return this.s / (this.n - ddof);
|
376 | case 'stdev':
|
377 | return Math.sqrt(this.s / (this.n - ddof));
|
378 | default:
|
379 | throw new Error('unknown mode for runningStat');
|
380 | }
|
381 | },
|
382 |
|
383 | format: formatter,
|
384 | numInputs: typeof attr !== 'undefined' ? 0 : 1
|
385 | };
|
386 | };
|
387 | };
|
388 | },
|
389 | sumOverSum: function sumOverSum() {
|
390 | var formatter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : usFmt;
|
391 |
|
392 | return function (_ref11) {
|
393 | var _ref12 = _slicedToArray(_ref11, 2),
|
394 | num = _ref12[0],
|
395 | denom = _ref12[1];
|
396 |
|
397 | return function () {
|
398 | return {
|
399 | sumNum: 0,
|
400 | sumDenom: 0,
|
401 | push: function push(record) {
|
402 | if (!isNaN(parseFloat(record[num]))) {
|
403 | this.sumNum += parseFloat(record[num]);
|
404 | }
|
405 | if (!isNaN(parseFloat(record[denom]))) {
|
406 | this.sumDenom += parseFloat(record[denom]);
|
407 | }
|
408 | },
|
409 | value: function value() {
|
410 | return this.sumNum / this.sumDenom;
|
411 | },
|
412 |
|
413 | format: formatter,
|
414 | numInputs: typeof num !== 'undefined' && typeof denom !== 'undefined' ? 0 : 2
|
415 | };
|
416 | };
|
417 | };
|
418 | },
|
419 | fractionOf: function fractionOf(wrapped) {
|
420 | var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'total';
|
421 | var formatter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : usFmtPct;
|
422 |
|
423 | return function () {
|
424 | for (var _len = arguments.length, x = Array(_len), _key = 0; _key < _len; _key++) {
|
425 | x[_key] = arguments[_key];
|
426 | }
|
427 |
|
428 | return function (data, rowKey, colKey) {
|
429 | return {
|
430 | selector: { total: [[], []], row: [rowKey, []], col: [[], colKey] }[type],
|
431 | inner: wrapped.apply(undefined, _toConsumableArray(Array.from(x || [])))(data, rowKey, colKey),
|
432 | push: function push(record) {
|
433 | this.inner.push(record);
|
434 | },
|
435 |
|
436 | format: formatter,
|
437 | value: function value() {
|
438 | return this.inner.value() / data.getAggregator.apply(data, _toConsumableArray(Array.from(this.selector || []))).inner.value();
|
439 | },
|
440 |
|
441 | numInputs: wrapped.apply(undefined, _toConsumableArray(Array.from(x || [])))().numInputs
|
442 | };
|
443 | };
|
444 | };
|
445 | }
|
446 | };
|
447 |
|
448 | aggregatorTemplates.countUnique = function (f) {
|
449 | return aggregatorTemplates.uniques(function (x) {
|
450 | return x.length;
|
451 | }, f);
|
452 | };
|
453 | aggregatorTemplates.listUnique = function (s) {
|
454 | return aggregatorTemplates.uniques(function (x) {
|
455 | return x.join(s);
|
456 | }, function (x) {
|
457 | return x;
|
458 | });
|
459 | };
|
460 | aggregatorTemplates.max = function (f) {
|
461 | return aggregatorTemplates.extremes('max', f);
|
462 | };
|
463 | aggregatorTemplates.min = function (f) {
|
464 | return aggregatorTemplates.extremes('min', f);
|
465 | };
|
466 | aggregatorTemplates.first = function (f) {
|
467 | return aggregatorTemplates.extremes('first', f);
|
468 | };
|
469 | aggregatorTemplates.last = function (f) {
|
470 | return aggregatorTemplates.extremes('last', f);
|
471 | };
|
472 | aggregatorTemplates.median = function (f) {
|
473 | return aggregatorTemplates.quantile(0.5, f);
|
474 | };
|
475 | aggregatorTemplates.average = function (f) {
|
476 | return aggregatorTemplates.runningStat('mean', 1, f);
|
477 | };
|
478 | aggregatorTemplates.var = function (ddof, f) {
|
479 | return aggregatorTemplates.runningStat('var', ddof, f);
|
480 | };
|
481 | aggregatorTemplates.stdev = function (ddof, f) {
|
482 | return aggregatorTemplates.runningStat('stdev', ddof, f);
|
483 | };
|
484 |
|
485 |
|
486 | var aggregators = function (tpl) {
|
487 | return {
|
488 | Count: tpl.count(usFmtInt),
|
489 | 'Count Unique Values': tpl.countUnique(usFmtInt),
|
490 | 'List Unique Values': tpl.listUnique(', '),
|
491 | Sum: tpl.sum(usFmt),
|
492 | 'Integer Sum': tpl.sum(usFmtInt),
|
493 | Average: tpl.average(usFmt),
|
494 | Median: tpl.median(usFmt),
|
495 | 'Sample Variance': tpl.var(1, usFmt),
|
496 | 'Sample Standard Deviation': tpl.stdev(1, usFmt),
|
497 | Minimum: tpl.min(usFmt),
|
498 | Maximum: tpl.max(usFmt),
|
499 | First: tpl.first(usFmt),
|
500 | Last: tpl.last(usFmt),
|
501 | 'Sum over Sum': tpl.sumOverSum(usFmt),
|
502 | 'Sum as Fraction of Total': tpl.fractionOf(tpl.sum(), 'total', usFmtPct),
|
503 | 'Sum as Fraction of Rows': tpl.fractionOf(tpl.sum(), 'row', usFmtPct),
|
504 | 'Sum as Fraction of Columns': tpl.fractionOf(tpl.sum(), 'col', usFmtPct),
|
505 | 'Count as Fraction of Total': tpl.fractionOf(tpl.count(), 'total', usFmtPct),
|
506 | 'Count as Fraction of Rows': tpl.fractionOf(tpl.count(), 'row', usFmtPct),
|
507 | 'Count as Fraction of Columns': tpl.fractionOf(tpl.count(), 'col', usFmtPct)
|
508 | };
|
509 | }(aggregatorTemplates);
|
510 |
|
511 | var locales = {
|
512 | en: {
|
513 | aggregators: aggregators,
|
514 | localeStrings: {
|
515 | renderError: 'An error occurred rendering the PivotTable results.',
|
516 | computeError: 'An error occurred computing the PivotTable results.',
|
517 | uiRenderError: 'An error occurred rendering the PivotTable UI.',
|
518 | selectAll: 'Select All',
|
519 | selectNone: 'Select None',
|
520 | tooMany: '(too many to list)',
|
521 | filterResults: 'Filter values',
|
522 | apply: 'Apply',
|
523 | cancel: 'Cancel',
|
524 | totals: 'Totals',
|
525 | vs: 'vs',
|
526 | by: 'by'
|
527 | }
|
528 | }
|
529 | };
|
530 |
|
531 |
|
532 | var mthNamesEn = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
533 | var dayNamesEn = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
534 | var zeroPad = function zeroPad(number) {
|
535 | return ('0' + number).substr(-2, 2);
|
536 | };
|
537 |
|
538 | var derivers = {
|
539 | bin: function bin(col, binWidth) {
|
540 | return function (record) {
|
541 | return record[col] - record[col] % binWidth;
|
542 | };
|
543 | },
|
544 | dateFormat: function dateFormat(col, formatString) {
|
545 | var utcOutput = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
546 | var mthNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : mthNamesEn;
|
547 | var dayNames = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : dayNamesEn;
|
548 |
|
549 | var utc = utcOutput ? 'UTC' : '';
|
550 | return function (record) {
|
551 | var date = new Date(Date.parse(record[col]));
|
552 | if (isNaN(date)) {
|
553 | return '';
|
554 | }
|
555 | return formatString.replace(/%(.)/g, function (m, p) {
|
556 | switch (p) {
|
557 | case 'y':
|
558 | return date['get' + utc + 'FullYear']();
|
559 | case 'm':
|
560 | return zeroPad(date['get' + utc + 'Month']() + 1);
|
561 | case 'n':
|
562 | return mthNames[date['get' + utc + 'Month']()];
|
563 | case 'd':
|
564 | return zeroPad(date['get' + utc + 'Date']());
|
565 | case 'w':
|
566 | return dayNames[date['get' + utc + 'Day']()];
|
567 | case 'x':
|
568 | return date['get' + utc + 'Day']();
|
569 | case 'H':
|
570 | return zeroPad(date['get' + utc + 'Hours']());
|
571 | case 'M':
|
572 | return zeroPad(date['get' + utc + 'Minutes']());
|
573 | case 'S':
|
574 | return zeroPad(date['get' + utc + 'Seconds']());
|
575 | default:
|
576 | return '%' + p;
|
577 | }
|
578 | });
|
579 | };
|
580 | }
|
581 | };
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 | var PivotData = function () {
|
588 | function PivotData() {
|
589 | var _this = this;
|
590 |
|
591 | var inputProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
592 |
|
593 | _classCallCheck(this, PivotData);
|
594 |
|
595 | this.props = Object.assign({}, PivotData.defaultProps, inputProps);
|
596 | _propTypes2.default.checkPropTypes(PivotData.propTypes, this.props, 'prop', 'PivotData');
|
597 |
|
598 | this.aggregator = this.props.aggregators[this.props.aggregatorName](this.props.vals);
|
599 | this.tree = {};
|
600 | this.rowKeys = [];
|
601 | this.colKeys = [];
|
602 | this.rowTotals = {};
|
603 | this.colTotals = {};
|
604 | this.allTotal = this.aggregator(this, [], []);
|
605 | this.sorted = false;
|
606 |
|
607 |
|
608 | PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
|
609 | if (_this.filter(record)) {
|
610 | _this.processRecord(record);
|
611 | }
|
612 | });
|
613 | }
|
614 |
|
615 | _createClass(PivotData, [{
|
616 | key: 'filter',
|
617 | value: function filter(record) {
|
618 | for (var k in this.props.valueFilter) {
|
619 | if (record[k] in this.props.valueFilter[k]) {
|
620 | return false;
|
621 | }
|
622 | }
|
623 | return true;
|
624 | }
|
625 | }, {
|
626 | key: 'forEachMatchingRecord',
|
627 | value: function forEachMatchingRecord(criteria, callback) {
|
628 | var _this2 = this;
|
629 |
|
630 | return PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
|
631 | if (!_this2.filter(record)) {
|
632 | return;
|
633 | }
|
634 | for (var k in criteria) {
|
635 | var v = criteria[k];
|
636 | if (v !== (k in record ? record[k] : 'null')) {
|
637 | return;
|
638 | }
|
639 | }
|
640 | callback(record);
|
641 | });
|
642 | }
|
643 | }, {
|
644 | key: 'arrSort',
|
645 | value: function arrSort(attrs) {
|
646 | var _this3 = this;
|
647 |
|
648 | var a = void 0;
|
649 | var sortersArr = function () {
|
650 | var result = [];
|
651 | var _iteratorNormalCompletion = true;
|
652 | var _didIteratorError = false;
|
653 | var _iteratorError = undefined;
|
654 |
|
655 | try {
|
656 | for (var _iterator = Array.from(attrs)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
657 | a = _step.value;
|
658 |
|
659 | result.push(getSort(_this3.props.sorters, a));
|
660 | }
|
661 | } catch (err) {
|
662 | _didIteratorError = true;
|
663 | _iteratorError = err;
|
664 | } finally {
|
665 | try {
|
666 | if (!_iteratorNormalCompletion && _iterator.return) {
|
667 | _iterator.return();
|
668 | }
|
669 | } finally {
|
670 | if (_didIteratorError) {
|
671 | throw _iteratorError;
|
672 | }
|
673 | }
|
674 | }
|
675 |
|
676 | return result;
|
677 | }();
|
678 | return function (a, b) {
|
679 | var _iteratorNormalCompletion2 = true;
|
680 | var _didIteratorError2 = false;
|
681 | var _iteratorError2 = undefined;
|
682 |
|
683 | try {
|
684 | for (var _iterator2 = Object.keys(sortersArr || {})[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
685 | var i = _step2.value;
|
686 |
|
687 | var sorter = sortersArr[i];
|
688 | var comparison = sorter(a[i], b[i]);
|
689 | if (comparison !== 0) {
|
690 | return comparison;
|
691 | }
|
692 | }
|
693 | } catch (err) {
|
694 | _didIteratorError2 = true;
|
695 | _iteratorError2 = err;
|
696 | } finally {
|
697 | try {
|
698 | if (!_iteratorNormalCompletion2 && _iterator2.return) {
|
699 | _iterator2.return();
|
700 | }
|
701 | } finally {
|
702 | if (_didIteratorError2) {
|
703 | throw _iteratorError2;
|
704 | }
|
705 | }
|
706 | }
|
707 |
|
708 | return 0;
|
709 | };
|
710 | }
|
711 | }, {
|
712 | key: 'sortKeys',
|
713 | value: function sortKeys() {
|
714 | var _this4 = this;
|
715 |
|
716 | if (!this.sorted) {
|
717 | this.sorted = true;
|
718 | var v = function v(r, c) {
|
719 | return _this4.getAggregator(r, c).value();
|
720 | };
|
721 | switch (this.props.rowOrder) {
|
722 | case 'value_a_to_z':
|
723 | this.rowKeys.sort(function (a, b) {
|
724 | return naturalSort(v(a, []), v(b, []));
|
725 | });
|
726 | break;
|
727 | case 'value_z_to_a':
|
728 | this.rowKeys.sort(function (a, b) {
|
729 | return -naturalSort(v(a, []), v(b, []));
|
730 | });
|
731 | break;
|
732 | default:
|
733 | this.rowKeys.sort(this.arrSort(this.props.rows));
|
734 | }
|
735 | switch (this.props.colOrder) {
|
736 | case 'value_a_to_z':
|
737 | this.colKeys.sort(function (a, b) {
|
738 | return naturalSort(v([], a), v([], b));
|
739 | });
|
740 | break;
|
741 | case 'value_z_to_a':
|
742 | this.colKeys.sort(function (a, b) {
|
743 | return -naturalSort(v([], a), v([], b));
|
744 | });
|
745 | break;
|
746 | default:
|
747 | this.colKeys.sort(this.arrSort(this.props.cols));
|
748 | }
|
749 | }
|
750 | }
|
751 | }, {
|
752 | key: 'getColKeys',
|
753 | value: function getColKeys() {
|
754 | this.sortKeys();
|
755 | return this.colKeys;
|
756 | }
|
757 | }, {
|
758 | key: 'getRowKeys',
|
759 | value: function getRowKeys() {
|
760 | this.sortKeys();
|
761 | return this.rowKeys;
|
762 | }
|
763 | }, {
|
764 | key: 'processRecord',
|
765 | value: function processRecord(record) {
|
766 |
|
767 | var colKey = [];
|
768 | var rowKey = [];
|
769 | var _iteratorNormalCompletion3 = true;
|
770 | var _didIteratorError3 = false;
|
771 | var _iteratorError3 = undefined;
|
772 |
|
773 | try {
|
774 | for (var _iterator3 = Array.from(this.props.cols)[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
775 | var x = _step3.value;
|
776 |
|
777 | colKey.push(x in record ? record[x] : 'null');
|
778 | }
|
779 | } catch (err) {
|
780 | _didIteratorError3 = true;
|
781 | _iteratorError3 = err;
|
782 | } finally {
|
783 | try {
|
784 | if (!_iteratorNormalCompletion3 && _iterator3.return) {
|
785 | _iterator3.return();
|
786 | }
|
787 | } finally {
|
788 | if (_didIteratorError3) {
|
789 | throw _iteratorError3;
|
790 | }
|
791 | }
|
792 | }
|
793 |
|
794 | var _iteratorNormalCompletion4 = true;
|
795 | var _didIteratorError4 = false;
|
796 | var _iteratorError4 = undefined;
|
797 |
|
798 | try {
|
799 | for (var _iterator4 = Array.from(this.props.rows)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
|
800 | var _x16 = _step4.value;
|
801 |
|
802 | rowKey.push(_x16 in record ? record[_x16] : 'null');
|
803 | }
|
804 | } catch (err) {
|
805 | _didIteratorError4 = true;
|
806 | _iteratorError4 = err;
|
807 | } finally {
|
808 | try {
|
809 | if (!_iteratorNormalCompletion4 && _iterator4.return) {
|
810 | _iterator4.return();
|
811 | }
|
812 | } finally {
|
813 | if (_didIteratorError4) {
|
814 | throw _iteratorError4;
|
815 | }
|
816 | }
|
817 | }
|
818 |
|
819 | var flatRowKey = rowKey.join(String.fromCharCode(0));
|
820 | var flatColKey = colKey.join(String.fromCharCode(0));
|
821 |
|
822 | this.allTotal.push(record);
|
823 |
|
824 | if (rowKey.length !== 0) {
|
825 | if (!this.rowTotals[flatRowKey]) {
|
826 | this.rowKeys.push(rowKey);
|
827 | this.rowTotals[flatRowKey] = this.aggregator(this, rowKey, []);
|
828 | }
|
829 | this.rowTotals[flatRowKey].push(record);
|
830 | }
|
831 |
|
832 | if (colKey.length !== 0) {
|
833 | if (!this.colTotals[flatColKey]) {
|
834 | this.colKeys.push(colKey);
|
835 | this.colTotals[flatColKey] = this.aggregator(this, [], colKey);
|
836 | }
|
837 | this.colTotals[flatColKey].push(record);
|
838 | }
|
839 |
|
840 | if (colKey.length !== 0 && rowKey.length !== 0) {
|
841 | if (!this.tree[flatRowKey]) {
|
842 | this.tree[flatRowKey] = {};
|
843 | }
|
844 | if (!this.tree[flatRowKey][flatColKey]) {
|
845 | this.tree[flatRowKey][flatColKey] = this.aggregator(this, rowKey, colKey);
|
846 | }
|
847 | this.tree[flatRowKey][flatColKey].push(record);
|
848 | }
|
849 | }
|
850 | }, {
|
851 | key: 'getAggregator',
|
852 | value: function getAggregator(rowKey, colKey) {
|
853 | var agg = void 0;
|
854 | var flatRowKey = rowKey.join(String.fromCharCode(0));
|
855 | var flatColKey = colKey.join(String.fromCharCode(0));
|
856 | if (rowKey.length === 0 && colKey.length === 0) {
|
857 | agg = this.allTotal;
|
858 | } else if (rowKey.length === 0) {
|
859 | agg = this.colTotals[flatColKey];
|
860 | } else if (colKey.length === 0) {
|
861 | agg = this.rowTotals[flatRowKey];
|
862 | } else {
|
863 | agg = this.tree[flatRowKey][flatColKey];
|
864 | }
|
865 | return agg || {
|
866 | value: function value() {
|
867 | return null;
|
868 | },
|
869 | format: function format() {
|
870 | return '';
|
871 | }
|
872 | };
|
873 | }
|
874 | }]);
|
875 |
|
876 | return PivotData;
|
877 | }();
|
878 |
|
879 |
|
880 |
|
881 |
|
882 | PivotData.forEachRecord = function (input, derivedAttributes, f) {
|
883 | var addRecord = void 0,
|
884 | record = void 0;
|
885 | if (Object.getOwnPropertyNames(derivedAttributes).length === 0) {
|
886 | addRecord = f;
|
887 | } else {
|
888 | addRecord = function addRecord(record) {
|
889 | for (var k in derivedAttributes) {
|
890 | var derived = derivedAttributes[k](record);
|
891 | if (derived !== null) {
|
892 | record[k] = derived;
|
893 | }
|
894 | }
|
895 | return f(record);
|
896 | };
|
897 | }
|
898 |
|
899 |
|
900 | if (typeof input === 'function') {
|
901 | return input(addRecord);
|
902 | } else if (Array.isArray(input)) {
|
903 | if (Array.isArray(input[0])) {
|
904 |
|
905 | return function () {
|
906 | var result = [];
|
907 | var _iteratorNormalCompletion5 = true;
|
908 | var _didIteratorError5 = false;
|
909 | var _iteratorError5 = undefined;
|
910 |
|
911 | try {
|
912 | for (var _iterator5 = Object.keys(input || {})[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
|
913 | var i = _step5.value;
|
914 |
|
915 | var compactRecord = input[i];
|
916 | if (i > 0) {
|
917 | record = {};
|
918 | var _iteratorNormalCompletion6 = true;
|
919 | var _didIteratorError6 = false;
|
920 | var _iteratorError6 = undefined;
|
921 |
|
922 | try {
|
923 | for (var _iterator6 = Object.keys(input[0] || {})[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
|
924 | var j = _step6.value;
|
925 |
|
926 | var k = input[0][j];
|
927 | record[k] = compactRecord[j];
|
928 | }
|
929 | } catch (err) {
|
930 | _didIteratorError6 = true;
|
931 | _iteratorError6 = err;
|
932 | } finally {
|
933 | try {
|
934 | if (!_iteratorNormalCompletion6 && _iterator6.return) {
|
935 | _iterator6.return();
|
936 | }
|
937 | } finally {
|
938 | if (_didIteratorError6) {
|
939 | throw _iteratorError6;
|
940 | }
|
941 | }
|
942 | }
|
943 |
|
944 | result.push(addRecord(record));
|
945 | }
|
946 | }
|
947 | } catch (err) {
|
948 | _didIteratorError5 = true;
|
949 | _iteratorError5 = err;
|
950 | } finally {
|
951 | try {
|
952 | if (!_iteratorNormalCompletion5 && _iterator5.return) {
|
953 | _iterator5.return();
|
954 | }
|
955 | } finally {
|
956 | if (_didIteratorError5) {
|
957 | throw _iteratorError5;
|
958 | }
|
959 | }
|
960 | }
|
961 |
|
962 | return result;
|
963 | }();
|
964 | }
|
965 |
|
966 |
|
967 | return function () {
|
968 | var result1 = [];
|
969 | var _iteratorNormalCompletion7 = true;
|
970 | var _didIteratorError7 = false;
|
971 | var _iteratorError7 = undefined;
|
972 |
|
973 | try {
|
974 | for (var _iterator7 = Array.from(input)[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
|
975 | record = _step7.value;
|
976 |
|
977 | result1.push(addRecord(record));
|
978 | }
|
979 | } catch (err) {
|
980 | _didIteratorError7 = true;
|
981 | _iteratorError7 = err;
|
982 | } finally {
|
983 | try {
|
984 | if (!_iteratorNormalCompletion7 && _iterator7.return) {
|
985 | _iterator7.return();
|
986 | }
|
987 | } finally {
|
988 | if (_didIteratorError7) {
|
989 | throw _iteratorError7;
|
990 | }
|
991 | }
|
992 | }
|
993 |
|
994 | return result1;
|
995 | }();
|
996 | }
|
997 | throw new Error('unknown input format');
|
998 | };
|
999 |
|
1000 | PivotData.defaultProps = {
|
1001 | aggregators: aggregators,
|
1002 | cols: [],
|
1003 | rows: [],
|
1004 | vals: [],
|
1005 | aggregatorName: 'Count',
|
1006 | sorters: {},
|
1007 | valueFilter: {},
|
1008 | rowOrder: 'key_a_to_z',
|
1009 | colOrder: 'key_a_to_z',
|
1010 | derivedAttributes: {}
|
1011 | };
|
1012 |
|
1013 | PivotData.propTypes = {
|
1014 | data: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object, _propTypes2.default.func]).isRequired,
|
1015 | aggregatorName: _propTypes2.default.string,
|
1016 | cols: _propTypes2.default.arrayOf(_propTypes2.default.string),
|
1017 | rows: _propTypes2.default.arrayOf(_propTypes2.default.string),
|
1018 | vals: _propTypes2.default.arrayOf(_propTypes2.default.string),
|
1019 | valueFilter: _propTypes2.default.objectOf(_propTypes2.default.objectOf(_propTypes2.default.bool)),
|
1020 | sorters: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.objectOf(_propTypes2.default.func)]),
|
1021 | derivedAttributes: _propTypes2.default.objectOf(_propTypes2.default.func),
|
1022 | rowOrder: _propTypes2.default.oneOf(['key_a_to_z', 'value_a_to_z', 'value_z_to_a']),
|
1023 | colOrder: _propTypes2.default.oneOf(['key_a_to_z', 'value_a_to_z', 'value_z_to_a'])
|
1024 | };
|
1025 |
|
1026 | exports.aggregatorTemplates = aggregatorTemplates;
|
1027 | exports.aggregators = aggregators;
|
1028 | exports.derivers = derivers;
|
1029 | exports.locales = locales;
|
1030 | exports.naturalSort = naturalSort;
|
1031 | exports.numberFormat = numberFormat;
|
1032 | exports.getSort = getSort;
|
1033 | exports.sortAs = sortAs;
|
1034 | exports.PivotData = PivotData;
|
1035 |
|
\ | No newline at end of file |