relative-file-path
v1.0.1
Published
Return a file path relative to a given file
Downloads
2
Maintainers
Readme
relative-file-path
Return a file path relative to a given file
Installation
$ npm install relative-file-path
Usage
import { relativeFilePath, RelativeFilePathError } from "relative-file-path";
console.log(relativeFilePath("/a.js", "/b.js"));
// ./b.js
console.log(relativeFilePath("a.js", "b.js"));
// ./b.js
console.log(relativeFilePath("/a/b.js", "/c/d.js"));
// ../c/d.js
console.log(relativeFilePath("/a.js", "b.js"));
// PathTypeError: Both parameter values must begin with '/' or not
console.log(relativeFilePath("/a.js/", "/b.js"));
// NotFilePathError: Both parameter values must not end with '/'
// Error Handling
try {
relativeFilePath("/a.js", "b.js");
} catch(e) {
if (e instanceof RelativeFilePathError) {
if (e.name === "PathTypeError") // ...
else if (e.name === "NotFilePathError") // ...
}
}