diff --git a/src/Utilities.js b/src/Utilities.js index 15dfac4..40377be 100644 --- a/src/Utilities.js +++ b/src/Utilities.js @@ -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('.'); @@ -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() { @@ -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() { @@ -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() { @@ -380,7 +374,7 @@ 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); }, @@ -388,12 +382,10 @@ const aggregatorTemplates = { 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, }; }; }, @@ -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) { @@ -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); @@ -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) { @@ -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'); };