@lix-js/fs
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,2 +0,2 @@ | ||
export declare function normalizePath(path: string): string; | ||
export declare function normalizePath(path: string, stripTrailing?: boolean): string; | ||
//# sourceMappingURL=normalizePath.d.ts.map |
@@ -7,3 +7,3 @@ /* | ||
*/ | ||
export function normalizePath(path) { | ||
export function normalizePath(path, stripTrailing) { | ||
if (path === "\\" || path === "/") | ||
@@ -26,3 +26,15 @@ return "/"; | ||
const segs = path.split(/[/\\]+/); | ||
return prefix + segs.join("/"); | ||
const stack = []; | ||
for (const seg of segs) { | ||
if (seg === "..") { | ||
stack.pop(); | ||
} | ||
else if (seg !== ".") { | ||
stack.push(seg); | ||
} | ||
} | ||
if (stripTrailing !== false && stack.at(-1) === "") { | ||
stack.pop(); | ||
} | ||
return prefix + stack.join("/"); | ||
} |
{ | ||
"name": "@lix-js/fs", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"type": "module", | ||
@@ -28,2 +28,3 @@ "publishConfig": { | ||
"devDependencies": { | ||
"@types/node": "20.6.0", | ||
"tsd": "^0.28.1", | ||
@@ -30,0 +31,0 @@ "@vitest/coverage-v8": "0.34.3", |
@@ -52,6 +52,6 @@ import { test, expect, afterAll, describe } from "vitest" | ||
expect(await fs.mkdir(`${tempDir}/home/user1/documents/`, { recursive: true })).toMatch( | ||
/^.*\/home\/?$/, | ||
/^.*\/home\/?$/ | ||
) | ||
expect(await fs.mkdir(`${tempDir}/home/user1/downloads/`, { recursive: true })).toMatch( | ||
/^.*\/home\/user1\/downloads\/?$/, | ||
/^.*\/home\/user1\/downloads\/?$/ | ||
) | ||
@@ -83,3 +83,3 @@ expect(await fs.readdir(tempDir)).toEqual(["home"]) | ||
expect( | ||
await fs.readFile(`${tempDir}/home/user1/documents/file1`, { encoding: "utf-8" }), | ||
await fs.readFile(`${tempDir}/home/user1/documents/file1`, { encoding: "utf-8" }) | ||
).toEqual(textInFirstFile) | ||
@@ -99,3 +99,3 @@ | ||
`${tempDir}/home/./user1/../user1/documents///./file1`, | ||
`${tempDir}/file1.link`, | ||
`${tempDir}/file1.link` | ||
) | ||
@@ -106,3 +106,3 @@ await fs.symlink(`${tempDir}/file3`, `${tempDir}/file3.link`) | ||
expect(await fs.readFile(`${tempDir}/file1.link`, { encoding: "utf-8" })).toEqual( | ||
textInFirstFile, | ||
textInFirstFile | ||
) | ||
@@ -121,3 +121,3 @@ expect(await fs.readFile(`${tempDir}/file3.link`, { encoding: "utf-8" })).toEqual("") | ||
expect(await fs.readlink(`${tempDir}/file1.link`)).toEqual( | ||
`${tempDir}/home/./user1/../user1/documents///./file1`, | ||
`${tempDir}/home/./user1/../user1/documents///./file1` | ||
) | ||
@@ -138,11 +138,11 @@ | ||
expect([stats.user1.isFile(), stats.user1.isDirectory(), stats.user1.isSymbolicLink()]).toEqual( | ||
[false, false, true], | ||
[false, false, true] | ||
) | ||
expect([stats.file1.isFile(), stats.file1.isDirectory(), stats.file1.isSymbolicLink()]).toEqual( | ||
[false, false, true], | ||
[false, false, true] | ||
) | ||
expect([stats.file2.isFile(), stats.file2.isDirectory(), stats.file2.isSymbolicLink()]).toEqual( | ||
[true, false, false], | ||
[true, false, false] | ||
) | ||
@@ -184,3 +184,3 @@ | ||
await expect( | ||
async () => await fs.readFile(`${tempDir}/home/dne/file`, { encoding: "utf-8" }), | ||
async () => await fs.readFile(`${tempDir}/home/dne/file`, { encoding: "utf-8" }) | ||
).rejects.toThrow(/ENOENT/) | ||
@@ -191,7 +191,7 @@ }) | ||
await expect( | ||
async () => await fs.readFile(`${tempDir}/home/dne`, { encoding: "utf-8" }), | ||
async () => await fs.readFile(`${tempDir}/home/dne`, { encoding: "utf-8" }) | ||
).rejects.toThrow(/ENOENT/) | ||
await expect( | ||
async () => await fs.readFile(`${tempDir}/home/user1`, { encoding: "utf-8" }), | ||
async () => await fs.readFile(`${tempDir}/home/user1`, { encoding: "utf-8" }) | ||
).rejects.toThrow(/EISDIR/) | ||
@@ -204,3 +204,3 @@ }) | ||
await expect( | ||
async () => await fs.readdir(`${tempDir}/home/user1/documents/file1`), | ||
async () => await fs.readdir(`${tempDir}/home/user1/documents/file1`) | ||
).rejects.toThrow(/ENOTDIR/) | ||
@@ -212,3 +212,3 @@ }) | ||
await expect(async () => await fs.rm(`${tempDir}/home/user1/documents/`)).rejects.toThrow( | ||
/EISDIR/, | ||
/EISDIR/ | ||
) | ||
@@ -218,3 +218,3 @@ | ||
await expect( | ||
async () => await fs.readFile(`${tempDir}/home/user1/documents/file1`, { encoding: "utf-8" }), | ||
async () => await fs.readFile(`${tempDir}/home/user1/documents/file1`, { encoding: "utf-8" }) | ||
).rejects.toThrow(/ENOENT/) | ||
@@ -221,0 +221,0 @@ |
@@ -35,3 +35,3 @@ import type { NodeishFilesystem, NodeishStats } from "../NodeishFilesystemApi.js" | ||
data: Parameters<NodeishFilesystem["writeFile"]>[1], | ||
options?: Parameters<NodeishFilesystem["writeFile"]>[2], | ||
options?: Parameters<NodeishFilesystem["writeFile"]>[2] | ||
) { | ||
@@ -59,3 +59,3 @@ const encoder = new TextEncoder() | ||
path: Parameters<NodeishFilesystem["readFile"]>[0], | ||
options?: Parameters<NodeishFilesystem["readFile"]>[1], | ||
options?: Parameters<NodeishFilesystem["readFile"]>[1] | ||
) { | ||
@@ -84,3 +84,3 @@ const decoder = new TextDecoder() | ||
path: Parameters<NodeishFilesystem["mkdir"]>[0], | ||
options: Parameters<NodeishFilesystem["mkdir"]>[1], | ||
options: Parameters<NodeishFilesystem["mkdir"]>[1] | ||
): Promise<string | undefined> { | ||
@@ -110,3 +110,3 @@ path = normalPath(path) | ||
path: Parameters<NodeishFilesystem["rm"]>[0], | ||
options: Parameters<NodeishFilesystem["rm"]>[1], | ||
options: Parameters<NodeishFilesystem["rm"]>[1] | ||
) { | ||
@@ -133,3 +133,3 @@ path = normalPath(path) | ||
await rm(`${path}/${child}`, { recursive: true }) | ||
}), | ||
}) | ||
) | ||
@@ -165,3 +165,3 @@ parentDir.delete(getBasename(path)) | ||
target: Parameters<NodeishFilesystem["symlink"]>[0], | ||
path: Parameters<NodeishFilesystem["symlink"]>[1], | ||
path: Parameters<NodeishFilesystem["symlink"]>[1] | ||
) { | ||
@@ -231,3 +231,3 @@ path = normalPath(path) | ||
modeBits: number, | ||
target?: string, | ||
target?: string | ||
) { | ||
@@ -258,3 +258,3 @@ const cdateMs: number = Date.now() | ||
.slice(0, -1) | ||
.join("/") ?? path, | ||
.join("/") ?? path | ||
) | ||
@@ -261,0 +261,0 @@ } |
@@ -7,3 +7,3 @@ /* | ||
*/ | ||
export function normalizePath(path: string): string { | ||
export function normalizePath(path: string, stripTrailing?: boolean): string { | ||
if (path === "\\" || path === "/") return "/" | ||
@@ -26,3 +26,17 @@ | ||
const segs = path.split(/[/\\]+/) | ||
return prefix + segs.join("/") | ||
const stack: string[] = [] | ||
for (const seg of segs) { | ||
if (seg === "..") { | ||
stack.pop() | ||
} else if (seg !== ".") { | ||
stack.push(seg) | ||
} | ||
} | ||
if (stripTrailing !== false && stack.at(-1) === "") { | ||
stack.pop() | ||
} | ||
return prefix + stack.join("/") | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54678
38
1262
5