file utils

This commit is contained in:
zhujingjing 2023-11-14 15:52:35 +08:00
parent 8755ea374b
commit a6c221aca4
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
5 changed files with 147 additions and 14 deletions

View File

@ -1,3 +1,3 @@
[http "https://github.com"] [http "https://github.com"]
;proxy = socks5://192.168.163.111:20170 proxy = socks5://192.168.163.111:20170
proxy = socks5://localhost:1080 ;proxy = socks5://localhost:1080

10
.npmrc
View File

@ -1,12 +1,8 @@
registry=https://registry.npm.taobao.org/ registry=https://registry.npm.taobao.org/
# proxy=http://192.168.163.111:20171 proxy=http://localhost:2081
# https-proxy=http://192.168.163.111:20171 https-proxy=http://localhost:2081
# http-proxy=http://192.168.163.111:20171 http-proxy=http://localhost:2081
proxy=http://localhost:1081
https-proxy=http://localhost:1081
http-proxy=http://localhost:1081
noproxy=npmmirror.com,taobao.org,sheetjs.com noproxy=npmmirror.com,taobao.org,sheetjs.com
public-hoist-pattern=['eslint', 'prettier'] public-hoist-pattern=['eslint', 'prettier']

View File

@ -12,14 +12,14 @@ export function getFileExt(fileName: string): string {
} }
/** /**
* *
* *
* *
* 使substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回 * 使substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回
* @param path * @param path
* @returns * @returns
*/ */
export function getFileName(path: string): string { export function getFileFullName(path: string): string {
let index = path.lastIndexOf("/"); let index = path.lastIndexOf("/");
if (index === -1) { if (index === -1) {
index = path.lastIndexOf("\\"); index = path.lastIndexOf("\\");
@ -29,3 +29,77 @@ export function getFileName(path: string): string {
} }
return path.substring(index + 1); return path.substring(index + 1);
} }
/**
*
*/
export function getFileNameWithoutExt(path: string): string {
const fileName = getFileFullName(path);
const index = fileName.lastIndexOf(".");
if (index === -1) {
return fileName;
}
return fileName.substring(0, index);
}
/**
*
*/
export interface IFileInfo {
/** 文件名(包含扩展名) */
fileName: string;
/**
*
*/
fileNameNoExt: string;
/** 文件扩展名 */
fileExtName: string;
/** 路径仅包含文件夹 */
pathOnly: string;
/** 完整路径 */
fullPath: string;
}
/**
*
* @param path
* @param platform "win32"
* @returns
*/
export function getFileInfo(
path: string,
platform: string = "win32"
): IFileInfo {
const pathSaprator = platform === "win32" ? "\\" : "/";
let pathOnly: string;
let fileName: string;
let fileExtName: string;
let fileNameNoExt: string;
const pathIndex = path.lastIndexOf(pathSaprator);
if (pathIndex === -1) {
pathOnly = "";
fileName = path;
} else {
pathOnly = path.substring(0, pathIndex + 1);
fileName = path.substring(pathIndex + 1);
}
const extIndex = fileName.lastIndexOf(".");
if (extIndex === -1) {
fileExtName = "";
fileNameNoExt = fileName;
} else {
fileExtName = fileName.substring(extIndex + 1);
fileNameNoExt = fileName.substring(0, extIndex);
}
return {
fileName,
fileNameNoExt,
fileExtName,
fullPath: path,
pathOnly,
};
}

View File

@ -8,7 +8,12 @@ import {
equalsIgnoreCase, equalsIgnoreCase,
includeIgnoreCase, includeIgnoreCase,
} from "./commons/str-utils"; } from "./commons/str-utils";
import { getFileExt, getFileName } from "./commons/file-util"; import {
getFileExt,
getFileFullName,
getFileNameWithoutExt,
getFileInfo,
} from "./commons/file-util";
export { export {
stringTokenizer, stringTokenizer,
@ -18,5 +23,7 @@ export {
assignRecords, assignRecords,
RecordClearMode, RecordClearMode,
getFileExt, getFileExt,
getFileName, getFileFullName as getFileName,
getFileNameWithoutExt,
getFileInfo,
}; };

56
test/file-util.test.ts Normal file
View File

@ -0,0 +1,56 @@
import { describe, expect, test } from "vitest";
import {
getFileFullName,
getFileExt,
getFileInfo,
getFileNameWithoutExt,
} from "../src/commons/file-util";
describe("file-util", () => {
test("getFileFullName", () => {
expect(getFileFullName("foo/aaaa.txt")).toBe("aaaa.txt");
expect(getFileFullName("foo/bar/aaaa.txt")).toBe("aaaa.txt");
expect(getFileFullName("aaaa.txt")).toBe("aaaa.txt");
expect(getFileFullName("foo/bar/aaaa")).toBe("aaaa");
});
test("getFileExt", () => {
expect(getFileExt("foo/aaaa.txt")).toBe("txt");
expect(getFileExt("foo/bar/aaaa.txt")).toBe("txt");
expect(getFileExt("aaaa.txt")).toBe("txt");
expect(getFileExt("foo/bar/aaaa")).toBe("");
});
test("getFileInfo", () => {
expect(getFileInfo("foo/aaaa.txt", "linux")).toEqual({
fileName: "aaaa.txt",
fileNameNoExt: "aaaa",
fileExtName: "txt",
fullPath: "foo/aaaa.txt",
pathOnly: "foo/",
});
expect(getFileInfo("foo\\bar\\aaaa.txt", "win32")).toEqual({
fileName: "aaaa.txt",
fileNameNoExt: "aaaa",
fileExtName: "txt",
fullPath: "foo\\bar\\aaaa.txt",
pathOnly: "foo\\bar\\",
});
expect(getFileInfo("foo/bar/aaaa", "linux")).toEqual({
fileName: "aaaa",
fileNameNoExt: "aaaa",
fileExtName: "",
fullPath: "foo/bar/aaaa",
pathOnly: "foo/bar/",
});
expect(getFileInfo("foo\\bar\\aaaa", "win32")).toEqual({
fileName: "aaaa",
fileNameNoExt: "aaaa",
fileExtName: "",
fullPath: "foo\\bar\\aaaa",
pathOnly: "foo\\bar\\",
});
});
});