-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.ts
More file actions
39 lines (36 loc) · 947 Bytes
/
detect.ts
File metadata and controls
39 lines (36 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @fileoverview Format detection by file extension.
*/
import { StringPrototypeEndsWith } from '../primordials/string'
import type { ArchiveFormat } from './types'
/**
* Detect archive format from file path.
*
* @param filePath - Path to archive file
* @returns Archive format or undefined if unknown
*
* @example
* ```typescript
* detectArchiveFormat('package.tar.gz') // 'tar.gz'
* detectArchiveFormat('archive.zip') // 'zip'
* detectArchiveFormat('data.csv') // undefined
* ```
*/
export function detectArchiveFormat(
filePath: string,
): ArchiveFormat | undefined {
const lower = filePath.toLowerCase()
if (StringPrototypeEndsWith(lower, '.tar.gz')) {
return 'tar.gz'
}
if (StringPrototypeEndsWith(lower, '.tgz')) {
return 'tgz'
}
if (StringPrototypeEndsWith(lower, '.tar')) {
return 'tar'
}
if (StringPrototypeEndsWith(lower, '.zip')) {
return 'zip'
}
return undefined
}