-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-ext.mts
51 lines (47 loc) · 1020 Bytes
/
format-ext.mts
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
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @file formatExt
* @module pathe/lib/formatExt
*/
import validateString from '#internal/validate-string'
import dot from '#lib/dot'
import type { EmptyString, Ext } from '@flex-development/pathe'
/**
* Format a file extension.
*
* @see {@linkcode EmptyString}
* @see {@linkcode Ext}
*
* @example
* formatExt('') // ''
* @example
* formatExt(null) // ''
* @example
* formatExt('.ts ') // '.ts'
* @example
* formatExt(' mjs') // '.mjs'
* @example
* formatExt('d.mts') // '.d.mts'
*
* @category
* utils
*
* @this {void}
*
* @param {string | null | undefined} ext
* The file extension to format
* @return {EmptyString | Ext}
* Formatted file extension or empty string
*/
function formatExt(
this: void,
ext: string | null | undefined
): EmptyString | Ext {
if (ext !== null && ext !== undefined) {
validateString(ext, 'ext')
ext = ext.trim()
}
if (!ext) return ''
if (ext.startsWith(dot)) return ext as Ext
return `${dot}${ext}`
}
export default formatExt