-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
711d283
commit ae7b2ec
Showing
9 changed files
with
80 additions
and
22 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 |
---|---|---|
|
@@ -34,5 +34,6 @@ exports[`e2e:mlly > should expose public api 1`] = ` | |
"resolver", | ||
"root", | ||
"toRelativeSpecifier", | ||
"toUrl", | ||
] | ||
`; |
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,11 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`unit:lib/toUrl > should return \`id\` as \`URL\` (0) 1`] = `"node:os"`; | ||
|
||
exports[`unit:lib/toUrl > should return \`id\` as \`URL\` (1) 1`] = `file://\${process.cwd()}/path`; | ||
|
||
exports[`unit:lib/toUrl > should return \`id\` as \`URL\` (2) 1`] = `file://\${process.cwd()}/src/lib/__tests__/to-url.spec.mts`; | ||
|
||
exports[`unit:lib/toUrl > should return \`id\` as \`URL\` (3) 1`] = `"node:test"`; | ||
|
||
exports[`unit:lib/toUrl > should return \`id\` as \`URL\` (4) 1`] = `file://\${process.cwd()}/tsconfig.build.json`; |
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,20 @@ | ||
/** | ||
* @file Unit Tests - toUrl | ||
* @module mlly/lib/tests/unit/toUrl | ||
*/ | ||
|
||
import cwd from '#lib/cwd' | ||
import testSubject from '#lib/to-url' | ||
import pathe from '@flex-development/pathe' | ||
|
||
describe('unit:lib/toUrl', () => { | ||
it.each<Parameters<typeof testSubject>>([ | ||
['os'], | ||
['path', cwd()], | ||
[import.meta.url], | ||
[new URL('node:test')], | ||
[pathe.resolve('tsconfig.build.json')] | ||
])('should return `id` as `URL` (%#)', (id, parent) => { | ||
expect(testSubject(id, parent)).toMatchSnapshot() | ||
}) | ||
}) |
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
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
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,36 @@ | ||
/** | ||
* @file toUrl | ||
* @module mlly/lib/toUrl | ||
*/ | ||
|
||
import canParseUrl from '#lib/can-parse-url' | ||
import { isBuiltin } from '@flex-development/is-builtin' | ||
import type { ModuleId } from '@flex-development/mlly' | ||
import pathe from '@flex-development/pathe' | ||
|
||
/** | ||
* Convert `id` to a {@linkcode URL}. | ||
* | ||
* > 👉 **Note**: If `id` cannot be parsed as a `URL` (in conjunction with | ||
* > `parent`) and is also not a [builtin module][builtin-module], it will be | ||
* > assumed to be a path and converted to a [`file:` URL][file-url]. | ||
* | ||
* [builtin-module]: https://nodejs.org/api/esm.html#builtin-modules | ||
* [file-url]: https://nodejs.org/api/esm.html#file-urls | ||
* | ||
* @param {ModuleId} id | ||
* The module id to convert | ||
* @param {ModuleId | null | undefined} [parent] | ||
* Base URL to resolve against if `id` is not absolute | ||
* @return {URL} | ||
* New URL | ||
*/ | ||
function toUrl(id: ModuleId, parent?: ModuleId | null | undefined): URL { | ||
return canParseUrl(id, parent) | ||
? new URL(id, parent ?? undefined) | ||
: isBuiltin(id) | ||
? new URL('node:' + String(id)) | ||
: pathe.pathToFileURL(String(id)) | ||
} | ||
|
||
export default toUrl |