-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfile.ts
More file actions
17 lines (17 loc) · 732 Bytes
/
Copy pathfile.ts
File metadata and controls
17 lines (17 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 通过url获取文件后缀
* @param url 图片链接
* @example
* getSuffixByUrl('https://example.com/path/to/file.txt'); // 返回 ".txt"
* getSuffixByUrl('https://example.com/path/to/file.txt?version=1.2'); // 返回 ".txt"
* getSuffixByUrl('https://example.com/path/to/file'); // 返回 ""
* getSuffixByUrl('https://example.com/path/to/file.txt#section'); // 返回 ".txt"
* getSuffixByUrl('哈哈哈.jpg'); // 返回 ".jpg"
* getSuffixByUrl(''); // 返回 ""
*/
export function getSuffixByUrl(url = '') {
const temp = url.split('/');
const filename = temp[temp.length - 1];
const filenameWithoutSuffix = filename?.split(/#|\?/)[0] || '';
return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
}