npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

relative

v3.0.2

Published

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.

Downloads

1,614,483

Readme

relative NPM version

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:

  • file-to-directory
  • file-to-file
  • directory-to-file
  • directory-to-directory

Install with npm

$ npm i relative --save

Usage

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());
//=> '..'

stat

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.

relative.toBase()

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'

Other useful libs

  • cwd: Node.js util for easily getting the current working directory of a project based on package.json… more
  • export-files: node.js utility for exporting a directory of files as modules.
  • global-prefix: Get the npm global path prefix.
  • is-absolute: Return true if a file path is absolute.
  • is-relative: Returns true if the path appears to be relative.
  • is-dotfile: Return true if a file path is (or has) a dotfile.

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright © 2014-2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on July 09, 2015.