From a6c221aca4f14695cf8727f4413037e15a5bdd10 Mon Sep 17 00:00:00 2001 From: zhujingjing Date: Tue, 14 Nov 2023 15:52:35 +0800 Subject: [PATCH] file utils --- .gitconfig | 4 +- .npmrc | 10 ++--- src/commons/file-util.ts | 80 ++++++++++++++++++++++++++++++++++++++-- src/index.ts | 11 +++++- test/file-util.test.ts | 56 ++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 test/file-util.test.ts diff --git a/.gitconfig b/.gitconfig index 8fa3ffb..cd74743 100644 --- a/.gitconfig +++ b/.gitconfig @@ -1,3 +1,3 @@ [http "https://github.com"] - ;proxy = socks5://192.168.163.111:20170 - proxy = socks5://localhost:1080 \ No newline at end of file + proxy = socks5://192.168.163.111:20170 + ;proxy = socks5://localhost:1080 \ No newline at end of file diff --git a/.npmrc b/.npmrc index cffb48b..a04a325 100644 --- a/.npmrc +++ b/.npmrc @@ -1,12 +1,8 @@ registry=https://registry.npm.taobao.org/ - -# proxy=http://192.168.163.111:20171 -# https-proxy=http://192.168.163.111:20171 -# http-proxy=http://192.168.163.111:20171 -proxy=http://localhost:1081 -https-proxy=http://localhost:1081 -http-proxy=http://localhost:1081 +proxy=http://localhost:2081 +https-proxy=http://localhost:2081 +http-proxy=http://localhost:2081 noproxy=npmmirror.com,taobao.org,sheetjs.com public-hoist-pattern=['eslint', 'prettier'] diff --git a/src/commons/file-util.ts b/src/commons/file-util.ts index bc916a6..51ed1f8 100644 --- a/src/commons/file-util.ts +++ b/src/commons/file-util.ts @@ -12,14 +12,14 @@ export function getFileExt(fileName: string): string { } /** - * 获取文件名 + * 获取文件全名(包含扩展名) * 该函数接收一个表示文件路径的字符串作为参数,返回该路径中文件名部分。 * 首先查找路径中最后一个斜杠或反斜杠的索引,若未找到则直接返回路径; * 若找到,则使用substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回。 * @param path 文件路径 - * @returns 文件名 + * @returns 文件全名(包含扩展名) */ -export function getFileName(path: string): string { +export function getFileFullName(path: string): string { let index = path.lastIndexOf("/"); if (index === -1) { index = path.lastIndexOf("\\"); @@ -29,3 +29,77 @@ export function getFileName(path: string): string { } 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, + }; +} diff --git a/src/index.ts b/src/index.ts index b91481b..0e9f8fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,12 @@ import { equalsIgnoreCase, includeIgnoreCase, } from "./commons/str-utils"; -import { getFileExt, getFileName } from "./commons/file-util"; +import { + getFileExt, + getFileFullName, + getFileNameWithoutExt, + getFileInfo, +} from "./commons/file-util"; export { stringTokenizer, @@ -18,5 +23,7 @@ export { assignRecords, RecordClearMode, getFileExt, - getFileName, + getFileFullName as getFileName, + getFileNameWithoutExt, + getFileInfo, }; diff --git a/test/file-util.test.ts b/test/file-util.test.ts new file mode 100644 index 0000000..5a4eb09 --- /dev/null +++ b/test/file-util.test.ts @@ -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\\", + }); + }); +});