-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2dddd9
commit 6f8c4d4
Showing
3 changed files
with
55 additions
and
10 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
29 changes: 29 additions & 0 deletions
29
packages/design-system/src/components/N8nMenuItem/__tests__/labelUtil.spec.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,29 @@ | ||
import { getInitials } from '../labelUtil'; | ||
|
||
describe('labelUtil.getInitials', () => { | ||
it.each([ | ||
['', ''], | ||
|
||
// simple words | ||
['Hello', 'He'], | ||
['Hello World', 'HW'], | ||
['H', 'H'], | ||
|
||
// multiple spaces | ||
['Double Space', 'DS'], | ||
[' ', ''], | ||
|
||
// simple emoji | ||
['👋 Hello', '👋H'], | ||
['👋Hello', '👋H'], | ||
['Hello 👋', 'H👋'], | ||
['Hello👋', 'He'], | ||
|
||
// combined emojis | ||
['1️⃣ 1️⃣', '1️⃣1️⃣'], | ||
['1️⃣', '1️⃣'], | ||
['👩⚕️D 👩⚕️D', '👩⚕️👩⚕️'], | ||
])('turns "%s" into "%s"', (input, output) => { | ||
expect(getInitials(input)).toBe(output); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/design-system/src/components/N8nMenuItem/labelUtil.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,25 @@ | ||
export const getInitials = (label: string): string => { | ||
const words = label | ||
.split(' ') | ||
.filter((word) => word !== '') | ||
.map((word) => [...new Intl.Segmenter().segment(word)]); | ||
|
||
if (words.length === 0) { | ||
return ''; | ||
} else if (words.length === 1) { | ||
// first two segments of the first word | ||
return ( | ||
words | ||
.at(0) | ||
?.slice(0, 2) | ||
.map((grapheme) => grapheme.segment) | ||
.join('') ?? '' | ||
); | ||
} else { | ||
// first segment ok the first two words | ||
return words | ||
.slice(0, 2) | ||
.map((word) => word.at(0)?.segment ?? '') | ||
.join(''); | ||
} | ||
}; |