-
Notifications
You must be signed in to change notification settings - Fork 162
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 #979 from dlowder-salesforce/dlowder/sortedDomain
Fix #973 (Legend ordering)
- Loading branch information
Showing
3 changed files
with
84 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const sortedDomain = (domain, attr, stateCount) => { | ||
/* sorting technique depends on the colorBy */ | ||
const sorted = Array.from(domain); | ||
if (attr === "clade_membership") { | ||
sorted.sort(); | ||
} else { | ||
sorted.sort( | ||
(a, b) => | ||
stateCount.get(a) === stateCount.get(b) | ||
? a < b ? -1 : 1 | ||
: stateCount.get(a) > stateCount.get(b) ? -1 : 1 | ||
); | ||
} | ||
return sorted; | ||
}; |
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,67 @@ | ||
import { sortedDomain } from "../src/util/sortedDomain"; | ||
|
||
test("sortedDomain works correctly in normal case", () => { | ||
const stateCount = new Map([ | ||
["Italy", 8], | ||
["Lombardy", 9], | ||
["USA", 1], | ||
["Iran", 18], | ||
["Comunitat Valenciana", 1], | ||
["Hubei", 35], | ||
["China", 2], | ||
["Grand Princess", 2], | ||
["Hong Kong", 1], | ||
["South Korea", 1], | ||
["Europe", 1], | ||
["UK", 1] | ||
]); | ||
const domain = stateCount.keys(); | ||
const sorted = sortedDomain(domain, "", stateCount); | ||
expect(sorted).toMatchObject([ | ||
"Hubei", | ||
"Iran", | ||
"Lombardy", | ||
"Italy", | ||
"China", | ||
"Grand Princess", | ||
"Comunitat Valenciana", | ||
"Europe", | ||
"Hong Kong", | ||
"South Korea", | ||
"UK", | ||
"USA" | ||
]); | ||
}); | ||
|
||
test("sortedDomain works correctly in special case", () => { | ||
const stateCount = new Map([ | ||
["Italy", 8], | ||
["Lombardy", 9], | ||
["USA", 1], | ||
["Iran", 18], | ||
["Comunitat Valenciana", 1], | ||
["Hubei", 35], | ||
["China", 2], | ||
["Grand Princess", 2], | ||
["Hong Kong", 1], | ||
["South Korea", 1], | ||
["Europe", 1], | ||
["UK", 1] | ||
]); | ||
const domain = stateCount.keys(); | ||
const sorted = sortedDomain(domain, "clade_membership", stateCount); | ||
expect(sorted).toMatchObject([ | ||
"China", | ||
"Comunitat Valenciana", | ||
"Europe", | ||
"Grand Princess", | ||
"Hong Kong", | ||
"Hubei", | ||
"Iran", | ||
"Italy", | ||
"Lombardy", | ||
"South Korea", | ||
"UK", | ||
"USA" | ||
]); | ||
}); |