1.0.1 • Published 6 months ago

relative-file-path v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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") // ...
  }
}
1.0.1

6 months ago

1.0.0

6 months ago