-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4 . Note that some newer supported Node.js versions have a regression where `deepStrictEquals` fails to compare the URL HREF, but a fix has recently been merged: - nodejs/node#50836 - nodejs/node#50853 (comment) Until the fix has been published in new Node.js releases, we can rely on the GitHub Actions CI workflow testing with Node.js v18 which doesn’t have the regression.
- Loading branch information
1 parent
2f357d8
commit 216774f
Showing
14 changed files
with
312 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// @ts-check | ||
|
||
import { pathToFileURL } from "node:url"; | ||
|
||
/** | ||
* Converts a directory path to a file URL that always ends with `/` so it can | ||
* be safely used as a base URL for constructing file URLs with relative paths. | ||
* @param {string} directoryPath Directory path to convert. | ||
* @returns {URL} Directory file URL. | ||
*/ | ||
export default function directoryPathToFileURL(directoryPath) { | ||
if (typeof directoryPath !== "string") | ||
throw new TypeError("Argument 1 `directoryPath` must be a string."); | ||
|
||
// @ts-ignore https://github.com/microsoft/TypeScript/issues/59996 | ||
return pathToFileURL( | ||
directoryPath.endsWith("/") ? directoryPath : `${directoryPath}/`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @ts-check | ||
|
||
import { deepStrictEqual, throws } from "node:assert"; | ||
import { describe, it } from "node:test"; | ||
|
||
import directoryPathToFileURL from "./directoryPathToFileURL.mjs"; | ||
|
||
describe("Function `directoryPathToFileURL`.", { concurrency: true }, () => { | ||
it("Argument 1 `directoryPath` not a string.", () => { | ||
throws(() => { | ||
directoryPathToFileURL( | ||
// @ts-expect-error Testing invalid. | ||
true, | ||
); | ||
}, new TypeError("Argument 1 `directoryPath` must be a string.")); | ||
}); | ||
|
||
it("Directory path ends with `/`.", () => { | ||
const directoryPath = "/a/b/c/"; | ||
|
||
deepStrictEqual( | ||
directoryPathToFileURL(directoryPath), | ||
new URL(`file://${directoryPath}`), | ||
); | ||
}); | ||
|
||
it("Directory path doesn’t end with `/`.", () => { | ||
const directoryPath = "/a/b/c"; | ||
|
||
deepStrictEqual( | ||
directoryPathToFileURL(directoryPath), | ||
new URL(`file://${directoryPath}/`), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.