file utils
This commit is contained in:
parent
8755ea374b
commit
a6c221aca4
|
|
@ -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
10
.npmrc
|
|
@ -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']
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
11
src/index.ts
11
src/index.ts
|
|
@ -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
56
test/file-util.test.ts
Normal 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\\",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user