-
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.
refactor(Initials): get color variant based on whole text
- Loading branch information
Showing
5 changed files
with
22 additions
and
20 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
19 changes: 10 additions & 9 deletions
19
packages/components/src/components/Initials/lib/getVariantFromInitials.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 |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import { getVariantFromInitials } from "./getVariantFromInitials"; | ||
import { getVariantFromString } from "./getVariantFromString"; | ||
|
||
describe('"getVariantFromInitials()', () => { | ||
test("does return 1 if array is empty", () => { | ||
expect(getVariantFromInitials([])).toStrictEqual(1); | ||
expect(getVariantFromString("")).toStrictEqual(1); | ||
}); | ||
|
||
test.each([ | ||
[["A"], 2], | ||
[["B"], 3], | ||
[["C", "D"], 4], | ||
[["Z"], 3], | ||
[["Ä"], 1], | ||
[["1"], 2], | ||
["A", 2], | ||
["AB", 4], | ||
["B", 3], | ||
["C", 4], | ||
["Z", 3], | ||
["Ä", 1], | ||
["1", 2], | ||
])("does get correct variant for given initial", (item, expectedVariant) => { | ||
expect(getVariantFromInitials(item)).toBe(expectedVariant); | ||
expect(getVariantFromString(item)).toBe(expectedVariant); | ||
}); | ||
}); |
6 changes: 0 additions & 6 deletions
6
packages/components/src/components/Initials/lib/getVariantFromInitials.ts
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/components/src/components/Initials/lib/getVariantFromString.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,7 @@ | ||
export const getVariantFromString = (str: string): number => { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
hash += str.charCodeAt(i); | ||
} | ||
return (hash % 4) + 1; | ||
}; |
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
b6d3e7f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for
./packages/components/
Test suite run success
52 tests passing in 9 suites.
Report generated by 🧪jest coverage report action from b6d3e7f