Browse Source

feat: 增加路径相关

main
谢亚昕 1 month ago
parent
commit
ee8547dbf7
  1. 2
      packages/core/src/path/index.ts
  2. 13
      packages/core/src/path/sanitizeFileName/__tests__/index.ts
  3. 6
      packages/core/src/path/sanitizeFileName/docs/index.md
  4. 20
      packages/core/src/path/sanitizeFileName/index.ts
  5. 13
      packages/core/src/path/slash/__tests__/index.ts
  6. 9
      packages/core/src/path/slash/index.ts

2
packages/core/src/path/index.ts

@ -0,0 +1,2 @@
export * from "./slash";
export * from "./sanitizeFileName";

13
packages/core/src/path/sanitizeFileName/__tests__/index.ts

@ -0,0 +1,13 @@
import { sanitizeFileName } from "../index";
describe("slash", async () => {
it("转换", async () => {
expect(sanitizeFileName("C:\\aa\\bb.txt")).toStrictEqual("C:\\aa\\bb.txt");
});
it("转换空格", async () => {
expect(sanitizeFileName("C:\\a a\\b b.txt")).toStrictEqual("C:\\a a\\b b.txt");
});
it("转换特殊字符", async () => {
expect(sanitizeFileName("C:\\a.*?a\\b b.txt")).toStrictEqual("C:\\a.__a\\b b.txt");
});
});

6
packages/core/src/path/sanitizeFileName/docs/index.md

@ -0,0 +1,6 @@
---
title: 清理无效文件名
category: 工具
---
sanitizeFileName

20
packages/core/src/path/sanitizeFileName/index.ts

@ -0,0 +1,20 @@
// https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts
const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$&*+,:;<=>?[\]^`{|}\u007F]/g;
const DRIVE_LETTER_REGEX = /^[a-z]:/i;
/**
* 使
*/
export function sanitizeFileName(name: string): string {
const match = DRIVE_LETTER_REGEX.exec(name);
const driveLetter = match ? match[0] : "";
return (
driveLetter +
name
.slice(driveLetter.length)
.replace(INVALID_CHAR_REGEX, "_")
.replace(/(^|\/)_+(?=[^/]*$)/, "$1")
);
}

13
packages/core/src/path/slash/__tests__/index.ts

@ -0,0 +1,13 @@
import { slash } from "../index";
describe("slash", async () => {
it("转换", async () => {
expect(slash("C:\\aa\\bb.txt")).toStrictEqual("C:/aa/bb.txt");
});
it("转换空格", async () => {
expect(slash("C:\\a a\\b b.txt")).toStrictEqual("C:/a a/b b.txt");
});
it("转换特殊字符", async () => {
expect(slash("C:\\a.*?a\\b b.txt")).toStrictEqual("C:/a.*?a/b b.txt");
});
});

9
packages/core/src/path/slash/index.ts

@ -0,0 +1,9 @@
export function slash(path) {
const isExtendedLengthPath = path.startsWith("\\\\?\\");
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, "/");
}
Loading…
Cancel
Save