-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-ext.mts
103 lines (94 loc) · 2.53 KB
/
remove-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file removeExt
* @module pathe/lib/removeExt
*/
import validateString from '#internal/validate-string'
import validateURLString from '#internal/validate-url-string'
import formatExt from '#lib/format-ext'
export default removeExt
/**
* Remove the file extension of `input`.
*
* Does nothing if `input` does not end with the provided file extension,
* or if a file extension is not provided.
*
* @category
* utils
*
* @param {string} input
* The path or URL string to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {string}
* `input` unmodified or with `ext` removed
*/
function removeExt(input: string, ext: string | null | undefined): string
/**
* Remove the file extension of `url`.
*
* Does nothing if `url` does not end with the provided file extension, or if a
* file extension is not provided.
*
* @category
* utils
*
* @param {URL} url
* The {@linkcode URL} to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {URL}
* `url` unmodified or with `ext` removed
*/
function removeExt(url: URL, ext: string | null | undefined): URL
/**
* Remove the file extension of `input`.
*
* Does nothing if `input` does not end with the provided file extension, or if
* a file extension is not provided.
*
* @example
* removeExt('file') // 'file'
* @example
* removeExt('file.mjs', 'mjs') // 'file'
* @example
* removeExt('file.mjs', '.mjs') // 'file'
* @example
* removeExt('file.d.mts', '.mjs') // 'file.d.mts'
*
* @category
* utils
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {URL | string}
* `input` unmodified or with `ext` removed
*/
function removeExt(
input: URL | string,
ext: string | null | undefined
): URL | string
/**
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {URL | string}
* `input` unmodified or with `ext` removed
*/
function removeExt(
input: URL | string,
ext: string | null | undefined
): URL | string {
validateURLString(input, 'input')
if (typeof input === 'string') {
if (ext !== null && ext !== undefined) {
validateString(ext, 'ext')
ext = formatExt(ext)
}
if (!ext || !input.endsWith(ext)) return input
return input.slice(0, input.lastIndexOf(ext))
}
return input.href = removeExt(input.href, ext), input
}