Skip to content

Commit

Permalink
feat: [avatar] - create getInitialLetter func
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgoud committed Nov 8, 2023
1 parent cd87675 commit dd887b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
28 changes: 8 additions & 20 deletions packages/components/avatar/src/avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<script setup lang="ts">
import { computed } from 'vue'
import { getInitialLetter } from '@puik/utils'
import PuikIcon from '../../icon'
import {
avatarProps,
Expand All @@ -45,34 +46,21 @@ const props = defineProps(avatarProps)
const initials = computed(() => {
const firstInitial = props.firstname
? props.firstname
.replace(/[^a-zA-Z0-9]/g, '')
.charAt(0)
.toUpperCase()
: ''
const lastInitial = props.lastname
? props.lastname
.replace(/[^a-zA-Z0-9]/g, '')
.charAt(0)
.toUpperCase()
? getInitialLetter(props.firstname, 0)
: ''
const lastInitial = props.lastname ? getInitialLetter(props.lastname, 0) : ''
const initialsValue = props.singleInitial
? firstInitial || lastInitial || 'P'
: firstInitial && lastInitial
? firstInitial + lastInitial
: firstInitial && props.firstname.length > 1
? firstInitial +
props.firstname
.replace(/[^a-zA-Z0-9]/g, '')
.charAt(1)
.toUpperCase()
? firstInitial + getInitialLetter(props.firstname, 1)
: lastInitial && props.lastname.length > 1
? lastInitial +
props.lastname
.replace(/[^a-zA-Z0-9]/g, '')
.charAt(1)
.toUpperCase()
? lastInitial + getInitialLetter(props.lastname, 1)
: 'PS'
return initialsValue
})
</script>
13 changes: 13 additions & 0 deletions packages/utils/getInitialLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Retrieves a letter from a string.
*
* @param {string} str - The string to process (with removal of special characters + uppercase process).
* @param {number} index - The index of the letter to retrieve.
* @returns {string} The letter at the specified index.
*/
export const getInitialLetter = (str: string, index: number): string => {
return str
.replace(/[^a-zA-Z0-9]/g, '')
.charAt(index)
.toUpperCase()
}
1 change: 1 addition & 0 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './types'
export * from './typescript'
export * from './isEllipsisActive'
export * from './clamp'
export * from './getInitialLetter'

0 comments on commit dd887b7

Please sign in to comment.