GithubHelp home page GithubHelp logo

timopulkkinen / relative Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonschlinkert/relative

0.0 1.0 0.0 32 KB

Easily calculate the relative path from file A to file B in Node.js project. Will calculate correctly from a file to a directory, file to file, directory to file, and directory to directory.

Home Page: https://github.com/jonschlinkert

License: MIT License

JavaScript 100.00%

relative's Introduction

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.

relative's People

Contributors

jonschlinkert avatar doowb avatar dulacp avatar popomore avatar

Watchers

Timo Pulkkinen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.