Skip to content

Commit

Permalink
fix(bar,summary,xyplot): safely stringify null and undefined cells wh…
Browse files Browse the repository at this point in the history
…en finding unique values

Previously, there would be runtime errors if users switched to columns with cell values that lacked
a toString() method

BREAKING CHANGE: App does not crash when columns contain cell values that do not have toString()
inside, such as "undefined" or "null"
  • Loading branch information
hydrosquall committed Jul 11, 2021
1 parent 741fb67 commit 261e24a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
13 changes: 4 additions & 9 deletions src/charts/bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,10 @@ export const semioticBarChart = (
};
}

const uniqueValues = sortedData.reduce(
(uniques, datapoint) =>
!uniques.find(
(uniqueDimName: string) => uniqueDimName === datapoint[dim1].toString(),
)
? [...uniques, datapoint[dim1].toString()]
: uniques,
[],
);
const uniqueValues =
dim1 === "none"
? []
: [...new Set(sortedData.map((d) => JSON.stringify(d[dim1])))];

if (!colorHashOverride && dim1 && dim1 !== "none") {
uniqueValues.forEach((value: string, index: number) => {
Expand Down
12 changes: 4 additions & 8 deletions src/charts/summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ export const semioticSummaryChart = (

const rAccessor = metric1;

const uniqueValues = data.reduce(
(uniqueArray: string[], datapoint) =>
(!uniqueArray.find(
(dimValue: string) => dimValue === datapoint[dim1].toString(),
) && [...uniqueArray, datapoint[dim1].toString()]) ||
uniqueArray,
[],
);
const uniqueValues =
dim1 === "none"
? []
: [...new Set(data.map((d) => JSON.stringify(d[dim1])))];

if (!colorHashOverride && dim1 && dim1 !== "none") {
uniqueValues.sort().forEach((dimValue, index) => {
Expand Down
11 changes: 3 additions & 8 deletions src/charts/xyplot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,9 @@ export const semioticXYPlot = (
dim1 &&
dim1 !== "none"
) {
const uniqueValues = sortedData.reduce(
(uniqueArray, datapoint) =>
(!uniqueArray.find(
(uniqueDim: string) => uniqueDim === datapoint[dim1].toString(),
) && [...uniqueArray, datapoint[dim1].toString()]) ||
uniqueArray,
[],
);
const uniqueValues = [
...new Set(sortedData.map((d) => JSON.stringify(d[dim1]))),
];

if (!colorHashOverride) {
uniqueValues.sort().forEach((dimValue: string, index: number) => {
Expand Down

0 comments on commit 261e24a

Please sign in to comment.