Skip to content

Commit e321b3b

Browse files
committed
refactor(cli): simplify truncateFilename function
1 parent cc0166e commit e321b3b

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

cli/src/components/image-card.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ const truncateFilename = (filename: string): string => {
2424
if (filename.length <= MAX_FILENAME_LENGTH) {
2525
return filename
2626
}
27-
const ext = filename.split('.').pop() || ''
28-
const nameWithoutExt = filename.slice(0, filename.length - ext.length - 1)
29-
const truncatedName = nameWithoutExt.slice(
30-
0,
31-
MAX_FILENAME_LENGTH - ext.length - 4,
32-
)
33-
return `${truncatedName}${ext ? '.' + ext : ''}`
27+
const lastDot = filename.lastIndexOf('.')
28+
const ext = lastDot !== -1 ? filename.slice(lastDot) : ''
29+
const baseName = lastDot !== -1 ? filename.slice(0, lastDot) : filename
30+
const maxBaseLength = MAX_FILENAME_LENGTH - ext.length - 1 // -1 for ellipsis
31+
return baseName.slice(0, maxBaseLength) + '…' + ext
3432
}
3533

3634
export interface ImageCardImage {

0 commit comments

Comments
 (0)