Skip to content

Commit

Permalink
Merge branch 'main' of github.com:visualize-admin/visualization-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Jun 8, 2023
2 parents 226e02a + 2d92675 commit 23046b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

## Unreleased

Nothing yet.
Fixes

- Sorting now works correctly for temporal dimensions

# [3.19.20] - 2023-06-08

Expand Down
44 changes: 44 additions & 0 deletions app/utils/sorting-values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ const hierarchicalDimension = {
],
} as unknown as DimensionMetadataWithHierarchiesFragment;

const temporalDimensionYear = {
__typename: "TemporalDimension",
values: [
{ value: "2020", label: "2020" },
{ value: "1996", label: "1996" },
{ value: "2019", label: "2019" },
],
} as unknown as DimensionMetadataFragment;

const temporalDimensionFullDate = {
__typename: "TemporalDimension",
values: [
{ value: "2020-01-01", label: "2020-01-01" },
{ value: "1996-05-05", label: "1996-05-05" },
{ value: "2019-12-12", label: "2019-12-12" },
],
} as unknown as DimensionMetadataFragment;

describe("makeDimensionValueSorters", () => {
const sorting: NonNullable<SortingField["sorting"]> = {
sortingType: "byAuto",
Expand Down Expand Up @@ -142,4 +160,30 @@ describe("makeDimensionValueSorters", () => {
"CH-BE",
]);
});

it("should correctly sort temporal dimensions (year) byAuto", () => {
const values = temporalDimensionYear.values.map((d) => d.value);
const sorters = makeDimensionValueSorters(temporalDimensionYear, {
sorting,
});
const sortingOrders = getSortingOrders(sorters, sorting);
expect(orderBy(values, sorters, sortingOrders)).toEqual([
"1996",
"2019",
"2020",
]);
});

it("should correctly sort temporal dimensions (full date) byAuto", () => {
const values = temporalDimensionFullDate.values.map((d) => d.value);
const sorters = makeDimensionValueSorters(temporalDimensionFullDate, {
sorting,
});
const sortingOrders = getSortingOrders(sorters, sorting);
expect(orderBy(values, sorters, sortingOrders)).toEqual([
"1996-05-05",
"2019-12-12",
"2020-01-01",
]);
});
});
15 changes: 12 additions & 3 deletions app/utils/sorting-values.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { FilterValue, SortingField } from "@/configurator";
import { DimensionValue } from "@/domain/data";
import {
DimensionValue,
isNumericalMeasure,
isTemporalDimension,
} from "@/domain/data";
import { DimensionMetadataWithHierarchiesFragment } from "@/graphql/query-hooks";

import { bfs } from "./bfs";
Expand Down Expand Up @@ -41,8 +45,13 @@ export const makeDimensionValueSorters = (
dimensionFilter: undefined,
}
): ((label: string) => string | undefined | number)[] => {
if (dimension?.__typename === "NumericalMeasure") {
return [(d) => +d];
if (isNumericalMeasure(dimension) || isTemporalDimension(dimension)) {
return [
(d) => {
const maybeNumber = +d;
return isNaN(maybeNumber) ? d : maybeNumber;
},
];
}

const {
Expand Down

1 comment on commit 23046b8

@vercel
Copy link

@vercel vercel bot commented on 23046b8 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app

Please sign in to comment.