Skip to content

Commit

Permalink
feat: add isAbsolute and isRelative booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-forster committed Dec 31, 2019
1 parent 450873d commit 60f970d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class FilePath {
public filename: string | undefined;

/** An ordered array of folder names. folders[0] represents the root of the path.
* If absolute, it will be empty '' (required), if relative, it will be the folder reference '.' or '..'
* If absolute, it will be an empty string (required), if relative, it will be the folder reference '.' or '..'
*/
public folders: string[] = [];

Expand Down Expand Up @@ -58,6 +58,14 @@ class FilePath {
public get extension(): string | undefined {
return this._extension;
}

public get isAbsolute(): boolean {
return this.folders[0] === '';
}

public get isRelative(): boolean {
return this.folders[0] !== '';
}
}

export default FilePath;
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,37 @@ console.log(path.folders, path.path);
> ['..', 'this', 'is', 'relative']
> '../this/is/relative/file.jpeg;
```
### isAbsolute and isRelative
Booleans indicating if the path is absolute or relative. Absolute paths only start with '/',
and the `path.folders[0] === ''`;
Ex.
```js
const path = new FilePath('/this/is/a/file.jpeg');
console.log(path.isAbsolute, path.isRelative)
> true
> false
path.path = 'this/is/a/file.jpeg';
console.log(path.isAbsolute, path.isRelative)
> false
> true
path.path = './this/is/a/file.jpeg';
console.log(path.isAbsolute, path.isRelative)
> false
> true
path.path = '../this/is/a/file.jpeg';
console.log(path.isAbsolute, path.isRelative)
> false
> true
```
18 changes: 18 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ describe('FilePath', () => {
expect(path.file).toEqual(undefined);
expect(path.path).toEqual('/this/is/the/');
});

it('isAbsolute and isRelative', () => {
const path = new FilePath('/this/is/a/file.jpeg');
expect(path.isAbsolute).toEqual(true);
expect(path.isRelative).toEqual(false);

path.path = 'this/is/a/file.jpeg';
expect(path.isAbsolute).toEqual(false);
expect(path.isRelative).toEqual(true);

path.path = './this/is/a/file.jpeg';
expect(path.isAbsolute).toEqual(false);
expect(path.isRelative).toEqual(true);

path.path = '../this/is/a/file.jpeg';
expect(path.isAbsolute).toEqual(false);
expect(path.isRelative).toEqual(true);
});
});

0 comments on commit 60f970d

Please sign in to comment.