fix: resolve comments

This commit is contained in:
AbhinRustagi
2026-05-02 01:19:44 +05:30
parent c40727672f
commit b801c1ccea
2 changed files with 42 additions and 1 deletions
+26
View File
@@ -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();
});
});
+16 -1
View File
@@ -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);
}