Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and optimize code #147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 43 additions & 59 deletions src/Utilities.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import PropTypes from 'prop-types';

/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS104: Avoid inline assignments
* DS201: Simplify complex destructure assignments
* DS203: Remove `|| {}` from converted for-own loops
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const zeroCharCodeStr = String.fromCharCode(0);

const addSeparators = function(nStr, thousandsSep, decimalSep) {
const x = String(nStr).split('.');
Expand Down Expand Up @@ -197,8 +187,9 @@ const aggregatorTemplates = {
return {
uniq: [],
push(record) {
if (!Array.from(this.uniq).includes(record[attr])) {
this.uniq.push(record[attr]);
const x = record[attr];
if (!this.uniq.includes(x)) {
this.uniq.push(x);
}
},
value() {
Expand All @@ -217,8 +208,9 @@ const aggregatorTemplates = {
return {
sum: 0,
push(record) {
if (!isNaN(parseFloat(record[attr]))) {
this.sum += parseFloat(record[attr]);
const x = parseFloat(record[attr]);
if (!isNaN(x)) {
this.sum += x;
}
},
value() {
Expand Down Expand Up @@ -355,11 +347,13 @@ const aggregatorTemplates = {
sumNum: 0,
sumDenom: 0,
push(record) {
if (!isNaN(parseFloat(record[num]))) {
this.sumNum += parseFloat(record[num]);
const x = parseFloat(record[num]);
const y = parseFloat(record[denom]);
if (!isNaN(x)) {
this.sumNum += x;
}
if (!isNaN(parseFloat(record[denom]))) {
this.sumDenom += parseFloat(record[denom]);
if (!isNaN(y)) {
this.sumDenom += y;
}
},
value() {
Expand All @@ -380,20 +374,18 @@ const aggregatorTemplates = {
selector: {total: [[], []], row: [rowKey, []], col: [[], colKey]}[
type
],
inner: wrapped(...Array.from(x || []))(data, rowKey, colKey),
inner: wrapped(...x)(data, rowKey, colKey),
push(record) {
this.inner.push(record);
},
format: formatter,
value() {
return (
this.inner.value() /
data
.getAggregator(...Array.from(this.selector || []))
.inner.value()
data.getAggregator(...this.selector).inner.value()
);
},
numInputs: wrapped(...Array.from(x || []))().numInputs,
numInputs: wrapped(...x)().numInputs,
};
};
},
Expand Down Expand Up @@ -591,16 +583,13 @@ class PivotData {
}

arrSort(attrs) {
let a;
const sortersArr = (() => {
const result = [];
for (a of Array.from(attrs)) {
result.push(getSort(this.props.sorters, a));
}
return result;
})();
const sortersArr = [];
for (const attr of attrs) {
sortersArr.push(getSort(this.props.sorters, attr));
}

return function(a, b) {
for (const i of Object.keys(sortersArr || {})) {
for (let i = 0; i < sortersArr.length; i++) {
const sorter = sortersArr[i];
const comparison = sorter(a[i], b[i]);
if (comparison !== 0) {
Expand Down Expand Up @@ -652,14 +641,14 @@ class PivotData {
// this code is called in a tight loop
const colKey = [];
const rowKey = [];
for (const x of Array.from(this.props.cols)) {
for (const x of this.props.cols) {
colKey.push(x in record ? record[x] : 'null');
}
for (const x of Array.from(this.props.rows)) {
for (const x of this.props.rows) {
rowKey.push(x in record ? record[x] : 'null');
}
const flatRowKey = rowKey.join(String.fromCharCode(0));
const flatColKey = colKey.join(String.fromCharCode(0));
const flatRowKey = rowKey.join(zeroCharCodeStr);
const flatColKey = colKey.join(zeroCharCodeStr);

this.allTotal.push(record);

Expand Down Expand Up @@ -696,8 +685,8 @@ class PivotData {

getAggregator(rowKey, colKey) {
let agg;
const flatRowKey = rowKey.join(String.fromCharCode(0));
const flatColKey = colKey.join(String.fromCharCode(0));
const flatRowKey = rowKey.join(zeroCharCodeStr);
const flatColKey = colKey.join(zeroCharCodeStr);
if (rowKey.length === 0 && colKey.length === 0) {
agg = this.allTotal;
} else if (rowKey.length === 0) {
Expand Down Expand Up @@ -741,33 +730,28 @@ PivotData.forEachRecord = function(input, derivedAttributes, f) {
if (typeof input === 'function') {
return input(addRecord);
} else if (Array.isArray(input)) {
const result = [];
if (Array.isArray(input[0])) {
// array of arrays
return (() => {
const result = [];
for (const i of Object.keys(input || {})) {
const compactRecord = input[i];
if (i > 0) {
record = {};
for (const j of Object.keys(input[0] || {})) {
const k = input[0][j];
record[k] = compactRecord[j];
}
result.push(addRecord(record));
for (const i of Object.keys(input || {})) {
const compactRecord = input[i];
if (i > 0) {
record = {};
for (const j of Object.keys(input[0] || {})) {
const k = input[0][j];
record[k] = compactRecord[j];
}
result.push(addRecord(record));
}
return result;
})();
}
return result;
}

// array of objects
return (() => {
const result1 = [];
for (record of Array.from(input)) {
result1.push(addRecord(record));
}
return result1;
})();
for (record of input) {
result.push(addRecord(record));
}
return result;
}
throw new Error('unknown input format');
};
Expand Down