Skip to content

Commit

Permalink
refactor: improve getFirstInitials function
Browse files Browse the repository at this point in the history
  • Loading branch information
thevuong committed Oct 25, 2023
1 parent eee0e49 commit 2657d64
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @codefixlabs/lib

## 0.1.12

### Patch Changes

- Improve getFirstInitials function

## 0.1.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codefixlabs/lib",
"version": "0.1.11",
"version": "0.1.12",
"license": "MIT",
"exports": {
"./package.json": "./package.json",
Expand Down
14 changes: 11 additions & 3 deletions packages/lib/src/string.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
function removeEmoji(text: string): string {
return text.replace(/\p{Emoji}/gu, '');
}

function convertSpecialTextToLatin(text: string): string {
return text.normalize('NFKD').replace(/[\u0300-\u036F]/g, '');
}

export const getFirstInitials = (
str: string | null | undefined,
text: string | null | undefined,
fallback?: string,
): string => {
if (!str) {
if (!text) {
return fallback || '';
}

return str
return convertSpecialTextToLatin(removeEmoji(text))
.split(' ')
.slice(0, 2)
.map((word) => word.charAt(0).toUpperCase())
Expand Down
7 changes: 7 additions & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @codefixlabs/ui

## 0.1.19

### Patch Changes

- Updated dependencies
- @codefixlabs/lib@0.1.12

## 0.1.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codefixlabs/ui",
"version": "0.1.18",
"version": "0.1.19",
"license": "MIT",
"exports": {
"./package.json": "./package.json",
Expand Down

0 comments on commit 2657d64

Please sign in to comment.