add file-util
This commit is contained in:
parent
44149b70a1
commit
8755ea374b
15
lib/commons/file-util.d.ts
vendored
Normal file
15
lib/commons/file-util.d.ts
vendored
Normal 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
31
src/commons/file-util.ts
Normal 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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user