Skip to content

Commit

Permalink
refactor(Initials): get color variant based on whole text
Browse files Browse the repository at this point in the history
  • Loading branch information
mfal committed Feb 1, 2024
1 parent 458a6e1 commit b6d3e7f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/components/Initials/Initials.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, PropsWithChildren } from "react";
import { getVariantFromInitials } from "./lib/getVariantFromInitials";
import { getVariantFromString } from "./lib/getVariantFromString";
import { getInitialsFromString } from "./lib/getInitialsFromString";
import styles from "./Initials.module.scss";
import clsx from "clsx";
Expand All @@ -16,7 +16,7 @@ export const Initials: FC<InitialsProps> = (props) => {

const rootClassName = clsx(
styles.initials,
styles[`variant-${getVariantFromInitials(initials)}`],
styles[`variant-${getVariantFromString(children)}`],
className,
);

Expand Down
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);
});
});

This file was deleted.

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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import Initials from "@mittwald/flow-next-components/Initials";
<Initials>Gillian Gopher</Initials>
</Avatar>
<Avatar size="m">
<Initials>James Holden</Initials>
<Initials>Gustav Gopher</Initials>
</Avatar>
<Avatar size="m">
<Initials>Peter Parker</Initials>
<Initials>Gina-Lisa Gopher</Initials>
</Avatar>
<Avatar size="m">
<Initials>Amos Burton</Initials>
<Initials>Gorgio Gopher</Initials>
</Avatar>
</Row>;

1 comment on commit b6d3e7f

@github-actions
Copy link
Contributor

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/

St.
Category Percentage Covered / Total
🟢 Statements 98.08% 102/104
🟢 Branches 93.55% 29/31
🟢 Functions 100% 27/27
🟢 Lines 98.06% 101/103

Test suite run success

52 tests passing in 9 suites.

Report generated by 🧪jest coverage report action from b6d3e7f

Please sign in to comment.