add file-util

This commit is contained in:
zhujingjing 2023-11-12 22:00:29 +08:00
parent 44149b70a1
commit 8755ea374b
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
2 changed files with 46 additions and 0 deletions

15
lib/commons/file-util.d.ts vendored Normal file
View File

@ -0,0 +1,15 @@
/**
*
* @param fileName
* @returns
*/
export declare function getFileExt(fileName: string): string;
/**
*
*
*
* 使substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回
* @param path
* @returns
*/
export declare function getFileName(path: string): string;

31
src/commons/file-util.ts Normal file
View File

@ -0,0 +1,31 @@
/**
*
* @param fileName
* @returns
*/
export function getFileExt(fileName: string): string {
const index = fileName.lastIndexOf(".");
if (index === -1) {
return "";
}
return fileName.substring(index + 1);
}
/**
*
*
*
* 使substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回
* @param path
* @returns
*/
export function getFileName(path: string): string {
let index = path.lastIndexOf("/");
if (index === -1) {
index = path.lastIndexOf("\\");
if (index === -1) {
return path;
}
}
return path.substring(index + 1);
}