All Projects → jonschlinkert → relative

jonschlinkert / relative

Licence: MIT license
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.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to relative

is-relative
Returns `true` if a file path appears to be relative. Moved from the "relative" lib.
Stars: ✭ 13 (-61.76%)
Mutual labels:  path, relative
svg-path-bbox
SVG paths bounding box calculator
Stars: ✭ 17 (-50%)
Mutual labels:  path
SonogramView
Audio visualisation of song
Stars: ✭ 65 (+91.18%)
Mutual labels:  path
path-that-svg
Path that SVG!
Stars: ✭ 45 (+32.35%)
Mutual labels:  path
groupoid.space
🧊 Інститут Формальної Математики
Stars: ✭ 35 (+2.94%)
Mutual labels:  path
get-bin-path
Get the current package's binary path
Stars: ✭ 25 (-26.47%)
Mutual labels:  path
path-replace-loader
Path replace loader for webpack
Stars: ✭ 14 (-58.82%)
Mutual labels:  path
is-valid-glob
Return true if a value is a valid glob pattern string, or array of glob patterns.
Stars: ✭ 21 (-38.24%)
Mutual labels:  path
file-icon-cli
Get the icon of a file or app as a PNG image (macOS)
Stars: ✭ 73 (+114.71%)
Mutual labels:  path
react-svg-pathline
React component for drawing SVG path through set of points, smoothing the corners
Stars: ✭ 42 (+23.53%)
Mutual labels:  path
llvm-epp
Efficient Path Profiling using LLVM
Stars: ✭ 16 (-52.94%)
Mutual labels:  path
STPathTextField
Subclass of NSTextField that supports shell and browser-style path autocompletion
Stars: ✭ 17 (-50%)
Mutual labels:  path
BezierKit
Bezier curves and paths in Swift for building vector applications
Stars: ✭ 190 (+458.82%)
Mutual labels:  path
array-keyed-map
JS datastructure, like Map, but the keys are arrays
Stars: ✭ 29 (-14.71%)
Mutual labels:  path
Pathslider
Numerical slider that follows a Bezier path
Stars: ✭ 15 (-55.88%)
Mutual labels:  path
NavigationRouter
A router implementation designed for complex modular apps, written in Swift
Stars: ✭ 89 (+161.76%)
Mutual labels:  path
path-absolutize
A library for extending `Path` and `PathBuf` in order to get an absolute path and remove the containing dots.
Stars: ✭ 37 (+8.82%)
Mutual labels:  path
XRadarView
A highly customizable radar view for Android
Stars: ✭ 106 (+211.76%)
Mutual labels:  path
monopati
Well-typed paths
Stars: ✭ 20 (-41.18%)
Mutual labels:  path
plugins
Elder.js plugins and community plugins.
Stars: ✭ 80 (+135.29%)
Mutual labels:  path

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].