Skip to content

Commit

Permalink
🚿 Remove redundant substring() + test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Mar 15, 2024
1 parent 34e35cc commit 99fbc6c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/textual/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ describe('@acusti/text-transform', () => {
expect(getInitials('BigO')).toBe('BO');
expect(getInitials(' "LastPass"')).toBe('LP');
expect(getInitials(' "LastP4ss"')).toBe('LP4');
expect(getInitials('LastPassFirstTask')).toBe('LPF');
expect(getInitials('LastPassFirstTask', 4)).toBe('LPFT');
// if initials would be only numbers, include 1st letter (if present)
expect(getInitials('4ty')).toBe('4T');
});
Expand Down
2 changes: 1 addition & 1 deletion packages/textual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getInitials = (name: string, maxLength = 3) => {
let initials = '';
// for single word names, use uppercase letters and numbers
if (!name.includes(' ')) {
initials = name.replace(SINGLE_WORD_NON_INITIALS_REGEX, '').substring(0, maxLength);
initials = name.replace(SINGLE_WORD_NON_INITIALS_REGEX, '');
// if initials are only numbers, include 1st letter (if present)
if (!/[a-zA-Z]/.test(initials)) {
initials += name.replace(/[^A-Za-z]+/, '')[0];
Expand Down

0 comments on commit 99fbc6c

Please sign in to comment.