Skip to content

Commit

Permalink
refactor: move string functions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsiemens committed Jul 8, 2024
1 parent 69c14e3 commit a9dd9b9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
13 changes: 1 addition & 12 deletions src/components/data-display/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Avatar as AntAvatar, Flex, Typography } from 'antd'
import { type AvatarProps as AntAvatarProps } from 'antd'
import { ConfigProvider } from 'src/components'
import './Avatar.css'
import { formatString, validString } from 'src/utils/utils'

export interface IAvatarProps extends AntAvatarProps {
subtitleOptions?: {
Expand All @@ -11,18 +12,6 @@ export interface IAvatarProps extends AntAvatarProps {
}
}

export const validString = (str?: string) => {
return (str?.trim?.()?.length ?? 0) > 0
}

export const formatString = (str?: string, limit?: number) => {
const _str = str?.trim() ?? ''
if (limit !== undefined && _str.length > limit) {
return `${_str.substring(0, limit)}...`
}
return _str
}

export const Avatar = ({ className, subtitleOptions, ...props }: IAvatarProps) => {
return (
<ConfigProvider>
Expand Down
71 changes: 70 additions & 1 deletion src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInitials, getOS } from './utils'
import { formatString, getInitials, getOS, validString } from './utils'
import { expect, describe, it, beforeEach, vi } from 'vitest'

describe('Testing utils', () => {
Expand Down Expand Up @@ -55,4 +55,73 @@ describe('Testing utils', () => {
expect(actualOS).toBe('Macintosh')
})
})
describe('formatString()', () => {
it('should return the string when no limit given', () => {
const str = 'test string'
const result = formatString(str)
expect(result).toBe(str)
})
it('should return the trimmed string when no limit given and spaces included', () => {
const str = 'test string '
const result = formatString(str)
expect(result).toBe('test string')
})
it('should return the shortened string when limit given', () => {
const str = 'test string'
const limit = 5
const result = formatString(str, limit)
expect(result).toBe('test...')
})
it('should return an empty string when input is undefined', () => {
const str = undefined
const result = formatString(str)
expect(result).toBe('')
})
it('should return the string input when limit is not an integer', () => {
const str = 'test string'
const result = formatString(str)
expect(result).toBe(str)
})
it('should return the string input when limit is undefined', () => {
const str = 'test string'
const limit = undefined
const result = formatString(str, limit)
expect(result).toBe(str)
})
it('should return the string input when limit is not an integer', () => {
const str = 'test string'
const limit = 3.23
const result = formatString(str, limit)
expect(result).toBe(str)
})
it('should return the string input when limit is less than 0', () => {
const str = 'test string'
const limit = -5
const result = formatString(str, limit)
expect(result).toBe(str)
})
})

describe('validString()', () => {
it('should return true when string is valid', () => {
const str = 'test string'
const result = validString(str)
expect(result).toBe(true)
})
it('should return false when string is empty', () => {
const str = ''
const result = validString(str)
expect(result).toBe(false)
})
it('should return false when string is only spaces', () => {
const str = ' \n\t'
const result = validString(str)
expect(result).toBe(false)
})
it('should return false when string is undefined', () => {
const str = undefined
const result = validString(str)
expect(result).toBe(false)
})
})
})
15 changes: 15 additions & 0 deletions src/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ export function buildLinkFromHrefOptions(label: ReactNode, hrefOptions?: HrefOpt
</a>
)
}

/** Returns `true` when a string has contents that are not just spaces */
export const validString = (str?: string) => {
return (str?.trim?.()?.length ?? 0) > 0
}

/** Returns a string that is trimmed of extraneous spacing, and shortened to the `limit` if parameter provided/applicable */
export const formatString = (str?: string, limit?: number) => {
const _str = str?.trim() ?? ''
const _limit = limit && Number.isInteger(limit) && limit >= 0 ? limit : undefined
if (_limit !== undefined && _str.length > _limit) {
return `${_str.substring(0, limit).trim()}...`
}
return _str
}

0 comments on commit a9dd9b9

Please sign in to comment.