Skip to content

Commit

Permalink
refactor: use helper function for computing totals
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Aug 15, 2023
1 parent 2249e73 commit 01f201f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/modules/pivotTable/PivotTableEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
NUMBER_TYPE_ROW_PERCENTAGE,
NUMBER_TYPE_VALUE,
} from './pivotTableConstants.js'
import { processTotalValue } from './processTotalValue.js'

const dataFields = [
'value',
Expand Down Expand Up @@ -705,9 +706,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
totalCell[field] = (totalCell[field] || 0) + value
}
processTotalValue(value, totalCell, field)
})
}
totalCell.count += 1
Expand All @@ -724,10 +723,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})

if (totals.columnSubtotal) {
Expand All @@ -742,10 +738,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})
}

Expand All @@ -761,10 +754,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})
}
}
Expand All @@ -780,10 +770,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})

if (totals.rowSubtotal) {
Expand All @@ -798,10 +785,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})
}

Expand All @@ -817,10 +801,7 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (!isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
processTotalValue(value, percentageTotal, field)
})
}
}
Expand Down
98 changes: 98 additions & 0 deletions src/modules/pivotTable/__tests__/processTotalValue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { processTotalValue } from '../processTotalValue.js'

const field = 'test'
const tests = [
{
testName: 'negative value',
value: -1,
totalObj: {},
field,
expected: -1,
},
{
testName: 'zero value',
value: 0,
totalObj: {},
field,
expected: 0,
},
{
testName: 'positive value',
value: 1,
totalObj: {},
field,
expected: 1,
},
{
testName: 'null value',
value: null,
totalObj: {},
field,
expected: undefined,
},
{
testName: 'undefined value',
value: undefined,
totalObj: {},
field,
expected: undefined,
},
{
testName: 'string value',
value: 'string',
totalObj: {},
field,
expected: undefined,
},
{
testName: 'negative value with existing total',
value: -1,
totalObj: { [field]: 100 },
field,
expected: 99,
},
{
testName: 'zero value with existing total',
value: 0,
totalObj: { [field]: 100 },
field,
expected: 100,
},
{
testName: 'positive value with existing total',
value: 1,
totalObj: { [field]: 100 },
field,
expected: 101,
},
{
testName: 'null value with existing total',
value: null,
totalObj: { [field]: 100 },
field,
expected: 100,
},
{
testName: 'undefined value with existing total',
value: undefined,
totalObj: { [field]: 100 },
field,
expected: 100,
},
{
testName: 'string value with existing total',
value: 'string',
totalObj: { [field]: 100 },
field,
expected: 100,
},
]

describe('processTotalValue', () => {
tests.forEach((t) => {
it(t.testName, () => {
processTotalValue(t.value, t.totalObj, t.field)
expect(t.totalObj[field]).toEqual(t.expected)
})
})
})
5 changes: 5 additions & 0 deletions src/modules/pivotTable/processTotalValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const processTotalValue = (value, totalObj, field) => {
if (typeof value === 'number' && Number.isFinite(value)) {
totalObj[field] = (totalObj[field] ?? 0) + value
}
}

0 comments on commit 01f201f

Please sign in to comment.