Skip to content

Commit 5a4b332

Browse files
committed
refactor: Apply formatUtil code review feedback and improve type safety
- Remove deprecated getMediaKindFromFilename function (no usages found) - Define MediaType using const assertion pattern - Apply as const to extension arrays with type guards - Use type assertions for type-safe includes checks
1 parent e832e03 commit 5a4b332

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

packages/shared-frontend-utils/src/formatUtil.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,19 @@ export function formatDuration(milliseconds: number): string {
475475
return parts.join(' ')
476476
}
477477

478-
// Module scope constants to avoid re-initialization on every call
479-
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp']
480-
const VIDEO_EXTENSIONS = ['mp4', 'webm', 'mov', 'avi']
481-
const AUDIO_EXTENSIONS = ['mp3', 'wav', 'ogg', 'flac']
482-
const THREE_D_EXTENSIONS = ['obj', 'fbx', 'gltf', 'glb']
478+
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp'] as const
479+
const VIDEO_EXTENSIONS = ['mp4', 'webm', 'mov', 'avi'] as const
480+
const AUDIO_EXTENSIONS = ['mp3', 'wav', 'ogg', 'flac'] as const
481+
const THREE_D_EXTENSIONS = ['obj', 'fbx', 'gltf', 'glb'] as const
482+
483+
const MEDIA_TYPES = ['image', 'video', 'audio', '3D'] as const
484+
type MediaType = (typeof MEDIA_TYPES)[number]
485+
486+
// Type guard helper for checking array membership
487+
type ImageExtension = (typeof IMAGE_EXTENSIONS)[number]
488+
type VideoExtension = (typeof VIDEO_EXTENSIONS)[number]
489+
type AudioExtension = (typeof AUDIO_EXTENSIONS)[number]
490+
type ThreeDExtension = (typeof THREE_D_EXTENSIONS)[number]
483491

484492
/**
485493
* Truncates a filename while preserving the extension
@@ -518,28 +526,16 @@ export function truncateFilename(
518526
* @param filename The filename to analyze
519527
* @returns The media type: 'image', 'video', 'audio', or '3D'
520528
*/
521-
export function getMediaTypeFromFilename(
522-
filename: string
523-
): 'image' | 'video' | 'audio' | '3D' {
529+
export function getMediaTypeFromFilename(filename: string): MediaType {
524530
if (!filename) return 'image'
525531
const ext = filename.split('.').pop()?.toLowerCase()
526532
if (!ext) return 'image'
527533

528-
if (IMAGE_EXTENSIONS.includes(ext)) return 'image'
529-
if (VIDEO_EXTENSIONS.includes(ext)) return 'video'
530-
if (AUDIO_EXTENSIONS.includes(ext)) return 'audio'
531-
if (THREE_D_EXTENSIONS.includes(ext)) return '3D'
534+
// Type-safe array includes check using type assertion
535+
if (IMAGE_EXTENSIONS.includes(ext as ImageExtension)) return 'image'
536+
if (VIDEO_EXTENSIONS.includes(ext as VideoExtension)) return 'video'
537+
if (AUDIO_EXTENSIONS.includes(ext as AudioExtension)) return 'audio'
538+
if (THREE_D_EXTENSIONS.includes(ext as ThreeDExtension)) return '3D'
532539

533540
return 'image'
534541
}
535-
536-
/**
537-
* @deprecated Use getMediaTypeFromFilename instead - kept for backward compatibility
538-
* @param filename The filename to analyze
539-
* @returns The media kind: 'image', 'video', 'audio', or '3D'
540-
*/
541-
export function getMediaKindFromFilename(
542-
filename: string
543-
): 'image' | 'video' | 'audio' | '3D' {
544-
return getMediaTypeFromFilename(filename)
545-
}

0 commit comments

Comments
 (0)