-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extnames.mts
62 lines (53 loc) · 1.14 KB
/
extnames.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
/**
* @file extnames
* @module pathe/lib/extnames
*/
import validateURLString from '#internal/validate-url-string'
import extname from '#lib/extname'
import type { EmptyString, Ext } from '@flex-development/pathe'
/**
* Get a list of file extensions for `input`.
*
* @see {@linkcode Ext}
* @see {@linkcode extname}
*
* @category
* utils
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @return {Ext[]}
* List of extensions
*/
function extnames(this: void, input: URL | string): Ext[] {
validateURLString(input, 'input')
/**
* List of extensions.
*
* @const {Ext[]} extensions
*/
const extensions: Ext[] = []
/**
* Current path.
*
* @var {string} subpath
*/
let subpath: string = String(input)
if (subpath) {
while (true) {
/**
* Current extension.
*
* @const {EmptyString | Ext} ext
*/
const ext: EmptyString | Ext = extname(subpath)
if (ext === '') break
extensions.unshift(ext)
subpath = subpath.slice(0, subpath.lastIndexOf(ext))
}
}
return extensions
}
export default extnames