Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing a PathRef in when creating a PathRef #123

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { assert, assertEquals, assertRejects, assertThrows, withTempDir } from "
import { createPathRef, PathRef } from "./path.ts";
import { path as stdPath } from "./deps.ts";

Deno.test("create from path ref", () => {
const path = createPathRef("src");
const path2 = createPathRef(path);
const path3 = new PathRef(path);
assertEquals(path, path2);
assertEquals(path.toString(), path3.toString());
});

Deno.test("custom inspect", () => {
const path = createPathRef("src");
assertEquals(Deno.inspect(path), 'PathRef("src")');
Expand Down
12 changes: 9 additions & 3 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { fs, path as stdPath, writeAll, writeAllSync } from "./deps.ts";

const PERIOD_CHAR_CODE = ".".charCodeAt(0);

export function createPathRef(path: string | URL | ImportMeta): PathRef {
return new PathRef(path);
export function createPathRef(path: string | URL | ImportMeta | PathRef): PathRef {
if (path instanceof PathRef) {
return path;
} else {
return new PathRef(path);
}
}

export interface WalkEntry extends Deno.DirEntry {
Expand Down Expand Up @@ -35,9 +39,11 @@ export class PathRef {
*/
private static instanceofSymbol = Symbol.for("dax.PathRef");

constructor(path: string | URL | ImportMeta) {
constructor(path: string | URL | ImportMeta | PathRef) {
if (path instanceof URL) {
this.#path = stdPath.fromFileUrl(path);
} else if (path instanceof PathRef) {
this.#path = path.toString();
} else if (typeof path === "string") {
if (path.startsWith("file://")) {
this.#path = stdPath.fromFileUrl(path);
Expand Down