
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.
Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.
Calculates correctly from:
Install with npm
$ npm i relative --save
var relative = require('relative');
relative(from, to);
relative('a/b/c.txt', 'd');
//=> '../../d'
relative('d', 'a/b/c.txt');
//=> '../a/b/c.txt'
Relative to process.cwd()
relative('a/b/c.txt');
//=> 'a/b/c.txt'
relative(process.cwd(), 'a/b/c.txt');
//=> 'a/b/c.txt'
relative('a/b/c.txt', process.cwd());
//=> '..'
There are cases where it is impossible to tell if a path is a file or a directory without more information.
Examples
If assume that a.b.c
is a directory, we have no way of know that without hitting the filesystem, which is impossible if the path doesn't actually exist.
So the result would be:
relative('fixtures/a.b.c', 'fixtures');
//=> '.'
If the path exists
Pass true
as the last argument, or pass the stat object from fs.stat()
/fs.statSync()
and you will get the correct result.
relative('fixtures/a.b.c', 'fixtures', true);
//=> '..'
If the path does not exist
End directory names with trailing slash. If you can't or don't want to do that, you may get incorrect results from time to time, but there isn't much we can do about it.
One very bad idea I had was to create a whitelist of filenames that look like directories, and directories that look like file names so that when those paths are encountered the logic would be adjusted accordingly. Anyway, it is what it is.
Get the relative path to the given base
.
relative.toBase(base, filepath);
Example:
relative.toBase('a/b', 'a/b/c/d/file.txt');
//=> 'c/d/file.txt'
true
if the path appears to be relative.Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright © 2014-2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb-cli on July 09, 2015.
FAQs
Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.
The npm package relative receives a total of 0 weekly downloads. As such, relative popularity was classified as not popular.
We found that relative demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.