Skip to content

Commit

Permalink
fix: undefined and null values get replaced w/ "-"
Browse files Browse the repository at this point in the history
  • Loading branch information
noahonyejese committed Nov 25, 2024
1 parent 3ddc292 commit 0fab373
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
35 changes: 27 additions & 8 deletions app/components/data-download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from "@/domain/data";
import {
dateFormatterFromDimension,
formatIdentity,
getFormatFullDateAuto,
getFormattersForLocale,
} from "@/formatters";
Expand Down Expand Up @@ -105,6 +106,7 @@ const prepareData = ({
sortingType: "byAuto",
sortingOrder: "asc",
};

const sorters = sortedComponents.map<
[string, ReturnType<typeof makeDimensionValueSorters>]
>((d) => {
Expand All @@ -126,23 +128,39 @@ const prepareData = ({
}
}
}

return 0;
});

const parsedData = sortedData.map((obs) => {
return Object.keys(obs).reduce<Observation>((acc, key) => {
const formattedObs = Object.keys(obs).reduce<Observation>((acc, key) => {
return {
...acc,
[key]: formatIdentity(obs[key]),
};
}, {});

return Object.keys(formattedObs).reduce<Observation>((acc, key) => {
const col = columns[key];
const parser = dimensionParsers[key];

return col
? {
...acc,
...{ [makeColumnLabel(col)]: parser(obs[key] as string) },
}
: acc;
if (!col) return acc;

const value = formattedObs[key];
let parsedValue;

if (value?.toString() === "–") {
parsedValue = "-";
} else {
parsedValue = parser(value as string);
}

return {
...acc,
[makeColumnLabel(col)]: parsedValue,
};
}, {});
});

const columnKeys = Object.values(columns).map(makeColumnLabel);

return {
Expand Down Expand Up @@ -343,6 +361,7 @@ const DownloadMenuItem = ({
const components = [...dimensions, ...measures];
const dimensionParsers = getDimensionParsers(components, { locale });
const observations = observationsData.dataCubesObservations.data;

const { columnKeys, data } = prepareData({
components,
observations,
Expand Down
4 changes: 2 additions & 2 deletions app/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export const dateFormatterFromDimension = (

type Formatter = (x: string) => string;

const formatIdentity = (x: string | Date | null) => {
return x !== DIMENSION_VALUE_UNDEFINED ? `${x}` : "–";
export const formatIdentity = (x: string | number | Date | null) => {
return x !== DIMENSION_VALUE_UNDEFINED && x !== null ? `${x}` : "–";
};

const decimalFormatter = (dim: NumericalMeasure, formatNumber: Formatter) => {
Expand Down

0 comments on commit 0fab373

Please sign in to comment.