-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #809 from navikt/typescript-sort-filters
Refactor sorting utils and add tests
- Loading branch information
Showing
14 changed files
with
110 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/app/(sok)/_components/utils/sortFiltersAlphabetically.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; | ||
import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; | ||
|
||
describe("sortFiltersAlphabetically", () => { | ||
test("Should sort filters alphabetically", () => { | ||
const filters: FilterAggregation[] = [ | ||
{ key: "a", count: 1 }, | ||
{ key: "a2", count: 1 }, | ||
{ key: "a1", count: 1 }, | ||
{ key: "c", count: 1 }, | ||
{ key: "b", count: 1 }, | ||
]; | ||
|
||
const expected: FilterAggregation[] = [ | ||
{ key: "a", count: 1 }, | ||
{ key: "a1", count: 1 }, | ||
{ key: "a2", count: 1 }, | ||
{ key: "b", count: 1 }, | ||
{ key: "c", count: 1 }, | ||
]; | ||
|
||
const result = sortFiltersAlphabetically(filters); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; | ||
|
||
export default function sortFiltersAlphabetically(filters: FilterAggregation[]): FilterAggregation[] { | ||
const clone = filters; | ||
|
||
clone.sort((a, b) => a.key.localeCompare(b.key, "no")); | ||
|
||
return clone; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; | ||
import sortPublishedFiltersByDayOffset from "@/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset"; | ||
|
||
describe("sortPublishedFiltersByDayOffset", () => { | ||
test("Should sort filters by days offset", () => { | ||
const filters: FilterAggregation[] = [ | ||
{ key: "now-3d", count: 1 }, | ||
{ key: "now-7d", count: 1 }, | ||
{ key: "now/d", count: 1 }, | ||
]; | ||
|
||
const expected: FilterAggregation[] = [ | ||
{ key: "now/d", count: 1 }, | ||
{ key: "now-3d", count: 1 }, | ||
{ key: "now-7d", count: 1 }, | ||
]; | ||
|
||
const result = sortPublishedFiltersByDayOffset(filters); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; | ||
|
||
export default function sortPublishedFiltersByDayOffset(filters: FilterAggregation[]): FilterAggregation[] { | ||
const publishedValuesByDayOffset = ["now/d", "now-3d", "now-7d"]; | ||
const clone = filters; | ||
|
||
clone.sort((a, b) => publishedValuesByDayOffset.indexOf(a.key) - publishedValuesByDayOffset.indexOf(b.key)); | ||
|
||
return clone; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; | ||
import sortRemoteFilters from "@/app/(sok)/_components/utils/sortRemoteFilters"; | ||
|
||
describe("sortRemoteFilters", () => { | ||
test("Should sort filters by expected order", () => { | ||
const filters: FilterAggregation[] = [ | ||
{ key: "Hjemmekontor ikke mulig", count: 1 }, | ||
{ key: "Hybridkontor", count: 1 }, | ||
{ key: "Hjemmekontor", count: 1 }, | ||
]; | ||
|
||
const expected: FilterAggregation[] = [ | ||
{ key: "Hybridkontor", count: 1 }, | ||
{ key: "Hjemmekontor", count: 1 }, | ||
{ key: "Hjemmekontor ikke mulig", count: 1 }, | ||
]; | ||
|
||
const result = sortRemoteFilters(filters); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); |
9 changes: 4 additions & 5 deletions
9
...ok)/_components/utils/sortRemoteValues.js → ...k)/_components/utils/sortRemoteFilters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/app/(sok)/_components/utils/sortValuesByFirstLetter.js
This file was deleted.
Oops, something went wrong.