fix: resolve comments
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parentDirectoryOf } from "./userPreferences";
|
||||
|
||||
describe("parentDirectoryOf", () => {
|
||||
it("returns the directory for a POSIX path", () => {
|
||||
expect(parentDirectoryOf("/Users/me/Movies/clip.mp4")).toBe("/Users/me/Movies");
|
||||
});
|
||||
|
||||
it("returns the directory for a Windows path", () => {
|
||||
expect(parentDirectoryOf("C:\\Users\\me\\Movies\\clip.mp4")).toBe("C:\\Users\\me\\Movies");
|
||||
});
|
||||
|
||||
it("preserves the POSIX root when the file is at /", () => {
|
||||
expect(parentDirectoryOf("/video.mp4")).toBe("/");
|
||||
});
|
||||
|
||||
it("preserves the Windows drive root with its trailing separator", () => {
|
||||
expect(parentDirectoryOf("C:\\video.mp4")).toBe("C:\\");
|
||||
expect(parentDirectoryOf("D:/video.mp4")).toBe("D:/");
|
||||
});
|
||||
|
||||
it("returns null when no separator is present", () => {
|
||||
expect(parentDirectoryOf("video.mp4")).toBeNull();
|
||||
expect(parentDirectoryOf("")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -89,11 +89,26 @@ export function loadUserPreferences(): UserPreferences {
|
||||
/**
|
||||
* Extracts the parent directory from a saved file path. Handles both POSIX
|
||||
* and Windows separators since the path comes from the OS save dialog.
|
||||
*
|
||||
* Root directories are preserved with their trailing separator so that the
|
||||
* value is still a valid directory path:
|
||||
* "/video.mp4" -> "/"
|
||||
* "C:\\video.mp4" -> "C:\\"
|
||||
*
|
||||
* Returns null if no separator is found.
|
||||
*/
|
||||
export function parentDirectoryOf(filePath: string): string | null {
|
||||
const lastSep = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
|
||||
if (lastSep <= 0) return null;
|
||||
if (lastSep < 0) return null;
|
||||
|
||||
// POSIX root, e.g. "/video.mp4" -> "/"
|
||||
if (lastSep === 0) return filePath[0];
|
||||
|
||||
// Windows drive root, e.g. "C:\\video.mp4" -> "C:\\"
|
||||
if (lastSep === 2 && /^[A-Za-z]:[/\\]/.test(filePath)) {
|
||||
return filePath.slice(0, lastSep + 1);
|
||||
}
|
||||
|
||||
return filePath.slice(0, lastSep);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user