@hrforte/react-pivottable 2.2.2 → 3.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/dist/Utilities.js CHANGED
@@ -712,11 +712,9 @@ var AggregatorCell = function () {
712
712
  _createClass(AggregatorCell, [{
713
713
  key: 'push',
714
714
  value: function push(record) {
715
- var _this = this;
716
-
717
- Object.keys(this.instances).forEach(function (key) {
718
- _this.instances[key].push(record);
719
- });
715
+ for (var key in this.instances) {
716
+ this.instances[key].push(record);
717
+ }
720
718
  }
721
719
  }, {
722
720
  key: 'getInstance',
@@ -725,8 +723,13 @@ var AggregatorCell = function () {
725
723
  if (targetKey && this.instances[targetKey]) {
726
724
  return this.instances[targetKey];
727
725
  }
728
- var first = this.instances[this.primaryKey] || this.instances[Object.keys(this.instances)[0]];
729
- return first || null;
726
+ if (this.instances[this.primaryKey]) {
727
+ return this.instances[this.primaryKey];
728
+ }
729
+ for (var _key2 in this.instances) {
730
+ return this.instances[_key2];
731
+ }
732
+ return null;
730
733
  }
731
734
  }]);
732
735
 
@@ -787,6 +790,7 @@ var dayNamesEn = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
787
790
  var zeroPad = function zeroPad(number) {
788
791
  return ('0' + number).substr(-2, 2);
789
792
  }; // eslint-disable-line no-magic-numbers
793
+ var FLAT_KEY_SEPARATOR = String.fromCharCode(0);
790
794
 
791
795
  var derivers = {
792
796
  bin: function bin(col, binWidth) {
@@ -839,7 +843,7 @@ Data Model class
839
843
 
840
844
  var PivotData = function () {
841
845
  function PivotData() {
842
- var _this2 = this;
846
+ var _this = this;
843
847
 
844
848
  var inputProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
845
849
 
@@ -850,9 +854,11 @@ var PivotData = function () {
850
854
 
851
855
  this.aggregations = this.normalizeAggregations();
852
856
  this.aggregationKeyLookup = {};
853
- this.aggregations.forEach(function (agg) {
854
- if (!(agg.aggregatorName in _this2.aggregationKeyLookup)) {
855
- _this2.aggregationKeyLookup[agg.aggregatorName] = agg.key;
857
+ this.aggregationIndexLookup = {};
858
+ this.aggregations.forEach(function (agg, index) {
859
+ _this.aggregationIndexLookup[agg.key] = index;
860
+ if (!(agg.aggregatorName in _this.aggregationKeyLookup)) {
861
+ _this.aggregationKeyLookup[agg.aggregatorName] = agg.key;
856
862
  }
857
863
  });
858
864
  if (this.aggregations.length === 0) {
@@ -878,8 +884,8 @@ var PivotData = function () {
878
884
  }
879
885
 
880
886
  PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
881
- if (_this2.filter(record)) {
882
- _this2.processRecord(record);
887
+ if (_this.filter(record)) {
888
+ _this.processRecord(record);
883
889
  }
884
890
  });
885
891
  }
@@ -895,21 +901,22 @@ var PivotData = function () {
895
901
  _createClass(PivotData, [{
896
902
  key: 'hydrateFromPrecomputed',
897
903
  value: function hydrateFromPrecomputed(precomputed) {
898
- var _this3 = this;
904
+ var _this2 = this;
899
905
 
900
906
  // One real aggregator instance per aggregation provides `format()` and
901
907
  // `numInputs` for every hydrated stub cell.
902
908
  var formatSources = {};
903
909
  this.aggregations.forEach(function (agg) {
904
- formatSources[agg.key] = _this3.aggregatorFactories[agg.key](_this3, [], []);
910
+ formatSources[agg.key] = _this2.aggregatorFactories[agg.key](_this2, [], []);
905
911
  });
912
+ this.precomputedFormatSources = formatSources;
906
913
 
907
914
  var makeCell = function makeCell(serializedCell) {
908
- var values = serializedCell && serializedCell.values || {};
915
+ var values = Array.isArray(serializedCell) ? serializedCell : serializedCell && serializedCell.values || {};
909
916
  var instances = {};
910
- _this3.aggregations.forEach(function (agg) {
917
+ _this2.aggregations.forEach(function (agg, index) {
911
918
  var source = formatSources[agg.key];
912
- var _value = values[agg.key];
919
+ var _value = Array.isArray(values) ? values[index] : values[agg.key];
913
920
  instances[agg.key] = {
914
921
  push: function push() {},
915
922
  value: function value() {
@@ -923,7 +930,7 @@ var PivotData = function () {
923
930
  outlet: source.outlet
924
931
  };
925
932
  });
926
- return new AggregatorCell(instances, _this3.primaryAggregation.key);
933
+ return new AggregatorCell(instances, _this2.primaryAggregation.key);
927
934
  };
928
935
 
929
936
  var hydrateTotals = function hydrateTotals(source, target) {
@@ -944,8 +951,8 @@ var PivotData = function () {
944
951
  hydrateTotals(precomputed.colTotals, this.colTotals);
945
952
  this.tree = {};
946
953
  Object.keys(precomputed.tree || {}).forEach(function (flatRowKey) {
947
- _this3.tree[flatRowKey] = {};
948
- hydrateTotals(precomputed.tree[flatRowKey], _this3.tree[flatRowKey]);
954
+ _this2.tree[flatRowKey] = {};
955
+ hydrateTotals(precomputed.tree[flatRowKey], _this2.tree[flatRowKey]);
949
956
  });
950
957
  this.allTotal = makeCell(precomputed.allTotal);
951
958
 
@@ -953,6 +960,40 @@ var PivotData = function () {
953
960
  // thread when a sorter could not cross the worker boundary (functions).
954
961
  this.sorted = !sortersHaveFunctions(this.props.sorters);
955
962
  }
963
+
964
+ // Virtualized worker results keep cell values in the worker. Convert one
965
+ // serialized viewport cell into an aggregator only when that cell is about
966
+ // to render, instead of hydrating every pivot cell on the main thread.
967
+
968
+ }, {
969
+ key: 'getPrecomputedAggregator',
970
+ value: function getPrecomputedAggregator(serializedCell, aggregationKey) {
971
+ if (!serializedCell) {
972
+ return PivotData.emptyAggregator;
973
+ }
974
+ var resolvedKey = aggregationKey;
975
+ if (resolvedKey && !(resolvedKey in this.aggregatorFactories) && this.aggregationKeyLookup[resolvedKey]) {
976
+ resolvedKey = this.aggregationKeyLookup[resolvedKey];
977
+ }
978
+ var source = this.precomputedFormatSources && this.precomputedFormatSources[resolvedKey];
979
+ if (!source) {
980
+ return PivotData.emptyAggregator;
981
+ }
982
+ var values = Array.isArray(serializedCell) ? serializedCell : serializedCell.values || {};
983
+ var _value2 = Array.isArray(values) ? values[this.aggregationIndexLookup[resolvedKey]] : values[resolvedKey];
984
+ return {
985
+ push: function push() {},
986
+ value: function value() {
987
+ return _value2;
988
+ },
989
+ format: function format(x) {
990
+ return source.format(x);
991
+ },
992
+
993
+ numInputs: source.numInputs,
994
+ outlet: source.outlet
995
+ };
996
+ }
956
997
  }, {
957
998
  key: 'filter',
958
999
  value: function filter(record) {
@@ -966,10 +1007,10 @@ var PivotData = function () {
966
1007
  }, {
967
1008
  key: 'forEachMatchingRecord',
968
1009
  value: function forEachMatchingRecord(criteria, callback) {
969
- var _this4 = this;
1010
+ var _this3 = this;
970
1011
 
971
1012
  return PivotData.forEachRecord(this.props.data, this.props.derivedAttributes, function (record) {
972
- if (!_this4.filter(record)) {
1013
+ if (!_this3.filter(record)) {
973
1014
  return;
974
1015
  }
975
1016
  for (var k in criteria) {
@@ -984,7 +1025,7 @@ var PivotData = function () {
984
1025
  }, {
985
1026
  key: 'arrSort',
986
1027
  value: function arrSort(attrs) {
987
- var _this5 = this;
1028
+ var _this4 = this;
988
1029
 
989
1030
  var a = void 0;
990
1031
  var sortersArr = function () {
@@ -997,7 +1038,7 @@ var PivotData = function () {
997
1038
  for (var _iterator2 = Array.from(attrs)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
998
1039
  a = _step2.value;
999
1040
 
1000
- result.push(getSort(_this5.props.sorters, a));
1041
+ result.push(getSort(_this4.props.sorters, a));
1001
1042
  }
1002
1043
  } catch (err) {
1003
1044
  _didIteratorError2 = true;
@@ -1052,12 +1093,12 @@ var PivotData = function () {
1052
1093
  }, {
1053
1094
  key: 'sortKeys',
1054
1095
  value: function sortKeys() {
1055
- var _this6 = this;
1096
+ var _this5 = this;
1056
1097
 
1057
1098
  if (!this.sorted) {
1058
1099
  this.sorted = true;
1059
1100
  var v = function v(r, c) {
1060
- return _this6.getAggregator(r, c).value();
1101
+ return _this5.getAggregator(r, c).value();
1061
1102
  };
1062
1103
 
1063
1104
  // Handle per-row sorting
@@ -1065,11 +1106,11 @@ var PivotData = function () {
1065
1106
  var rowSorts = this.props.rowSorts;
1066
1107
  this.rowKeys.sort(function (a, b) {
1067
1108
  // First, check if any sorted attribute has a difference
1068
- for (var i = 0; i < _this6.props.rows.length; i++) {
1069
- var attr = _this6.props.rows[i];
1109
+ for (var i = 0; i < _this5.props.rows.length; i++) {
1110
+ var attr = _this5.props.rows[i];
1070
1111
  if (attr && rowSorts[attr]) {
1071
1112
  var sortDir = rowSorts[attr] === 'ASC' ? 1 : -1;
1072
- var sorter = getSort(_this6.props.sorters, attr);
1113
+ var sorter = getSort(_this5.props.sorters, attr);
1073
1114
  var comparison = sorter(a[i] || '', b[i] || '');
1074
1115
  if (comparison !== 0) {
1075
1116
  return sortDir * comparison;
@@ -1077,7 +1118,7 @@ var PivotData = function () {
1077
1118
  }
1078
1119
  }
1079
1120
  // If sorted attributes are equal, use default sorting for remaining attributes
1080
- return _this6.arrSort(_this6.props.rows)(a, b);
1121
+ return _this5.arrSort(_this5.props.rows)(a, b);
1081
1122
  });
1082
1123
  } else {
1083
1124
  // Use global row order
@@ -1102,11 +1143,11 @@ var PivotData = function () {
1102
1143
  var colSorts = this.props.colSorts;
1103
1144
  this.colKeys.sort(function (a, b) {
1104
1145
  // First, check if any sorted attribute has a difference
1105
- for (var i = 0; i < _this6.props.cols.length; i++) {
1106
- var attr = _this6.props.cols[i];
1146
+ for (var i = 0; i < _this5.props.cols.length; i++) {
1147
+ var attr = _this5.props.cols[i];
1107
1148
  if (attr && colSorts[attr]) {
1108
1149
  var sortDir = colSorts[attr] === 'ASC' ? 1 : -1;
1109
- var sorter = getSort(_this6.props.sorters, attr);
1150
+ var sorter = getSort(_this5.props.sorters, attr);
1110
1151
  var comparison = sorter(a[i] || '', b[i] || '');
1111
1152
  if (comparison !== 0) {
1112
1153
  return sortDir * comparison;
@@ -1114,7 +1155,7 @@ var PivotData = function () {
1114
1155
  }
1115
1156
  }
1116
1157
  // If sorted attributes are equal, use default sorting for remaining attributes
1117
- return _this6.arrSort(_this6.props.cols)(a, b);
1158
+ return _this5.arrSort(_this5.props.cols)(a, b);
1118
1159
  });
1119
1160
  } else {
1120
1161
  // Use global col order
@@ -1153,58 +1194,16 @@ var PivotData = function () {
1153
1194
  // this code is called in a tight loop
1154
1195
  var colKey = [];
1155
1196
  var rowKey = [];
1156
- var _iteratorNormalCompletion4 = true;
1157
- var _didIteratorError4 = false;
1158
- var _iteratorError4 = undefined;
1159
-
1160
- try {
1161
- for (var _iterator4 = Array.from(this.props.cols)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
1162
- var x = _step4.value;
1163
-
1164
- colKey.push(x in record ? record[x] : 'null');
1165
- }
1166
- } catch (err) {
1167
- _didIteratorError4 = true;
1168
- _iteratorError4 = err;
1169
- } finally {
1170
- try {
1171
- if (!_iteratorNormalCompletion4 && _iterator4.return) {
1172
- _iterator4.return();
1173
- }
1174
- } finally {
1175
- if (_didIteratorError4) {
1176
- throw _iteratorError4;
1177
- }
1178
- }
1197
+ for (var index = 0; index < this.props.cols.length; index += 1) {
1198
+ var x = this.props.cols[index];
1199
+ colKey.push(x in record ? record[x] : 'null');
1179
1200
  }
1180
-
1181
- var _iteratorNormalCompletion5 = true;
1182
- var _didIteratorError5 = false;
1183
- var _iteratorError5 = undefined;
1184
-
1185
- try {
1186
- for (var _iterator5 = Array.from(this.props.rows)[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
1187
- var _x17 = _step5.value;
1188
-
1189
- rowKey.push(_x17 in record ? record[_x17] : 'null');
1190
- }
1191
- } catch (err) {
1192
- _didIteratorError5 = true;
1193
- _iteratorError5 = err;
1194
- } finally {
1195
- try {
1196
- if (!_iteratorNormalCompletion5 && _iterator5.return) {
1197
- _iterator5.return();
1198
- }
1199
- } finally {
1200
- if (_didIteratorError5) {
1201
- throw _iteratorError5;
1202
- }
1203
- }
1201
+ for (var _index = 0; _index < this.props.rows.length; _index += 1) {
1202
+ var _x17 = this.props.rows[_index];
1203
+ rowKey.push(_x17 in record ? record[_x17] : 'null');
1204
1204
  }
1205
-
1206
- var flatRowKey = rowKey.join(String.fromCharCode(0));
1207
- var flatColKey = colKey.join(String.fromCharCode(0));
1205
+ var flatRowKey = rowKey.join(FLAT_KEY_SEPARATOR);
1206
+ var flatColKey = colKey.join(FLAT_KEY_SEPARATOR);
1208
1207
 
1209
1208
  this.allTotal.push(record);
1210
1209
 
@@ -1253,8 +1252,8 @@ var PivotData = function () {
1253
1252
  key: 'getAggregatorCell',
1254
1253
  value: function getAggregatorCell(rowKey, colKey) {
1255
1254
  var cell = void 0;
1256
- var flatRowKey = rowKey.join(String.fromCharCode(0));
1257
- var flatColKey = colKey.join(String.fromCharCode(0));
1255
+ var flatRowKey = rowKey.join(FLAT_KEY_SEPARATOR);
1256
+ var flatColKey = colKey.join(FLAT_KEY_SEPARATOR);
1258
1257
  if (rowKey.length === 0 && colKey.length === 0) {
1259
1258
  cell = this.allTotal;
1260
1259
  } else if (rowKey.length === 0) {
@@ -1358,25 +1357,25 @@ var PivotData = function () {
1358
1357
  }, {
1359
1358
  key: 'buildAggregatorFactories',
1360
1359
  value: function buildAggregatorFactories() {
1361
- var _this7 = this;
1360
+ var _this6 = this;
1362
1361
 
1363
1362
  var factories = {};
1364
1363
  this.aggregations.forEach(function (agg) {
1365
- if (!(agg.aggregatorName in _this7.props.aggregators)) {
1364
+ if (!(agg.aggregatorName in _this6.props.aggregators)) {
1366
1365
  throw new Error('unknown aggregator ' + agg.aggregatorName);
1367
1366
  }
1368
- factories[agg.key] = _this7.props.aggregators[agg.aggregatorName](agg.vals);
1367
+ factories[agg.key] = _this6.props.aggregators[agg.aggregatorName](agg.vals);
1369
1368
  });
1370
1369
  return factories;
1371
1370
  }
1372
1371
  }, {
1373
1372
  key: 'createAggregatorCell',
1374
1373
  value: function createAggregatorCell(rowKey, colKey) {
1375
- var _this8 = this;
1374
+ var _this7 = this;
1376
1375
 
1377
1376
  var instances = {};
1378
1377
  this.aggregations.forEach(function (agg) {
1379
- instances[agg.key] = _this8.aggregatorFactories[agg.key](_this8, rowKey, colKey);
1378
+ instances[agg.key] = _this7.aggregatorFactories[agg.key](_this7, rowKey, colKey);
1380
1379
  });
1381
1380
  return new AggregatorCell(instances, this.primaryAggregation.key);
1382
1381
  }
@@ -1419,97 +1418,23 @@ PivotData.forEachRecord = function (input, derivedAttributes, f) {
1419
1418
  } else if (Array.isArray(input)) {
1420
1419
  if (Array.isArray(input[0])) {
1421
1420
  // array of arrays
1422
- return function () {
1423
- var result = [];
1424
- var _iteratorNormalCompletion6 = true;
1425
- var _didIteratorError6 = false;
1426
- var _iteratorError6 = undefined;
1427
-
1428
- try {
1429
- for (var _iterator6 = Object.keys(input || {})[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
1430
- var i = _step6.value;
1431
-
1432
- var compactRecord = input[i];
1433
- if (i > 0) {
1434
- record = {};
1435
- var _iteratorNormalCompletion7 = true;
1436
- var _didIteratorError7 = false;
1437
- var _iteratorError7 = undefined;
1438
-
1439
- try {
1440
- for (var _iterator7 = Object.keys(input[0] || {})[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
1441
- var j = _step7.value;
1442
-
1443
- var k = input[0][j];
1444
- record[k] = compactRecord[j];
1445
- }
1446
- } catch (err) {
1447
- _didIteratorError7 = true;
1448
- _iteratorError7 = err;
1449
- } finally {
1450
- try {
1451
- if (!_iteratorNormalCompletion7 && _iterator7.return) {
1452
- _iterator7.return();
1453
- }
1454
- } finally {
1455
- if (_didIteratorError7) {
1456
- throw _iteratorError7;
1457
- }
1458
- }
1459
- }
1460
-
1461
- result.push(addRecord(record));
1462
- }
1463
- }
1464
- } catch (err) {
1465
- _didIteratorError6 = true;
1466
- _iteratorError6 = err;
1467
- } finally {
1468
- try {
1469
- if (!_iteratorNormalCompletion6 && _iterator6.return) {
1470
- _iterator6.return();
1471
- }
1472
- } finally {
1473
- if (_didIteratorError6) {
1474
- throw _iteratorError6;
1475
- }
1476
- }
1421
+ var headers = input[0] || [];
1422
+ for (var i = 1; i < input.length; i += 1) {
1423
+ var compactRecord = input[i];
1424
+ record = {};
1425
+ for (var j = 0; j < headers.length; j += 1) {
1426
+ record[headers[j]] = compactRecord[j];
1477
1427
  }
1478
-
1479
- return result;
1480
- }();
1428
+ addRecord(record);
1429
+ }
1430
+ return null;
1481
1431
  }
1482
1432
 
1483
1433
  // array of objects
1484
- return function () {
1485
- var result1 = [];
1486
- var _iteratorNormalCompletion8 = true;
1487
- var _didIteratorError8 = false;
1488
- var _iteratorError8 = undefined;
1489
-
1490
- try {
1491
- for (var _iterator8 = Array.from(input)[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
1492
- record = _step8.value;
1493
-
1494
- result1.push(addRecord(record));
1495
- }
1496
- } catch (err) {
1497
- _didIteratorError8 = true;
1498
- _iteratorError8 = err;
1499
- } finally {
1500
- try {
1501
- if (!_iteratorNormalCompletion8 && _iterator8.return) {
1502
- _iterator8.return();
1503
- }
1504
- } finally {
1505
- if (_didIteratorError8) {
1506
- throw _iteratorError8;
1507
- }
1508
- }
1509
- }
1510
-
1511
- return result1;
1512
- }();
1434
+ for (var _i = 0; _i < input.length; _i += 1) {
1435
+ addRecord(input[_i]);
1436
+ }
1437
+ return null;
1513
1438
  }
1514
1439
  throw new Error('unknown input format');
1515
1440
  };