Skip to content

Commit

Permalink
add tailing slash option
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Xavier committed Sep 15, 2020
1 parent 2908cd7 commit 4770d16
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
47 changes: 43 additions & 4 deletions Path.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copryright 2020 Matheus Xavier all rights reserved. MIT licensed
import { _determineSeparators } from "./_separator.ts";

const LINUX_SEPS = ['/'];
const WINDOWS_SEPS = ['\\', '/'];
export const LINUX_SEPS = ["/"];
export const WINDOWS_SEPS = ["\\", "/"];

/**
* this class represents a filesystem path, and allows for easy manipulation of said path
*/
export class Path {
private pathElements: string[];
private separators: string[];
public trailingSlash: boolean = false;

/**
*
Expand All @@ -19,6 +20,7 @@ export class Path {
constructor(path?: string, separators?: string[]) {
this.separators = separators || _determineSeparators();
if (path) {
this.trailingSlash = path[0] === "/";
this.pathElements = Path.explodePath(this.separators, path);
} else {
this.pathElements = new Array<string>();
Expand Down Expand Up @@ -57,7 +59,8 @@ export class Path {
* the preferred system separator will be used
*/
public toString(): string {
return this.pathElements.join(this.separators[0]);
const path = this.pathElements.join(this.separators[0])
return this.trailingSlash ? "/".concat(path) : path;
}

public push(e: string) {
Expand Down Expand Up @@ -93,14 +96,50 @@ export class Path {
get exists(): boolean {
try {
Deno.statSync(this.toString());
return true;
} catch (e) {
// do not hide permission errors from the user
if (e instanceof Deno.errors.PermissionDenied) {
throw e;
}
return false;
}
}

get isFile(): boolean {
try {
return Deno.statSync(this.toString()).isFile;
} catch (e) {
// do not hide permission errors from the user
if (e instanceof Deno.errors.PermissionDenied) {
throw e;
}
return false;
}
}

get isDir(): boolean {
try {
return Deno.statSync(this.toString()).isDirectory;
} catch (e) {
// do not hide permission errors from the user
if (e instanceof Deno.errors.PermissionDenied) {
throw e;
}
return false;
}
}

get isSymlink(): boolean {
try {
return Deno.statSync(this.toString()).isSymlink;
} catch (e) {
// do not hide permission errors from the user
if (e instanceof Deno.errors.PermissionDenied) {
throw e;
}
return false;
}
return true
}

/**
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ Better path handling for deno. This lib handles paths in a more dynamic and prac
examples:
```ts
import Path from "https://deno.land/x/path/mod.ts";
import { WINDOWS_SEPS } from "https://deno.land/x/path/Path.ts";

const winPath = new Path("C:\\Users\\Test\\Documents/myFile.v1.txt", ["\\", "/"]);
const winPath = new Path("C:\\Users\\Test\\Documents/myFile.v1.txt", WINDOWS_SEPS);
console.log(winPath.elements);
console.log(winPath.toString());
console.log(winPath.ext);
console.log(winPath.exists);

const nixPath = new Path("/etc/passwd");
console.log(nixPath.elements);
console.log(nixPath.toString());
console.log(nixPath.ext);
console.log(nixPath.exists);
```

# Features
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copryright 2020 Matheus Xavier all rights reserved. MIT licensed

import {Path} from "./Path.ts";
import { Path, LINUX_SEPS, WINDOWS_SEPS } from "./Path.ts";
export default Path;
11 changes: 9 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Path from "https://deno.land/x/path/mod.ts";
import Path from "./mod.ts";
import { WINDOWS_SEPS } from "./Path.ts";

const winPath = new Path("C:\\Users\\Test\\Documents/myFile.v1.txt", ["\\", "/"]);
const winPath = new Path("C:\\Users\\Test\\Documents/myFile.v1.txt", WINDOWS_SEPS);
console.log(winPath.elements);
console.log(winPath.toString());
console.log(winPath.ext);
console.log(winPath.exists);

const nixPath = new Path("/etc/passwd");
console.log(nixPath.elements);
console.log(nixPath.toString());
console.log(nixPath.ext);
console.log(nixPath.exists);

0 comments on commit 4770d16

Please sign in to comment.