relative-file-path

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i relative-file-path

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

6.58 kB

Total Files

7

Last publish

Collaborators

  • hhhjin