Create a relative URL with minify options.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Latest commit ec81352 Feb 26, 2017
Permalink
Failed to load latest commit information.
test v1.0.0-alpha Jul 7, 2017
.gitignore v1.0.0-alpha Jul 7, 2017
.travis.yml v1.0.0-alpha Jul 7, 2017
README.md v1.0.0-alpha Jul 7, 2017
index.js v1.0.0-alpha Jul 7, 2017
license v1.0.0-alpha Jul 7, 2017
package.json v1.0.0-alpha Jul 7, 2017

README.md

relateurl NPM Version File Size Build Status Coverage Status Dependency Monitor

Create a relative URL with minify options.

With http://domain.com/dir1/dir1-1/ as a base URL, you can produce:

Before After
http://domain.com/dir1/dir1-2/index.html ../dir1-2/
http://domain.com/dir2/dir2-1/ /dir2/dir2-1/
http://domain.com/dir1/dir1-1/
httpS://domain.com/dir1/dir1-1/ https://domain.com/dir1/dir1-1/
../../../../../../../../#anchor /#anchor

Installation

Node.js >= 6 is required. To install, type this at the command line:

npm install relateurl

Usage

Inputs must be URL instances.

const relateURL = require('relateurl');

const base = new URL('http://domain.com/dir1/dir1-1/');
const url  = new URL('//domain.com/dir1/dir1-2/index.html', base);

relateURL(url, base, options);
//-> ../dir1-2/

Options

It is simplest to use an option profile, but custom configurations are still possible.

output

Type: constant / Number
Default value: relateURL.SHORTEST
The limit of how far the resulting URL should be related. Possible values:

  • PROTOCOL_RELATIVE: will try to produce something like //domain.com/path/to/file.html.
  • ROOT_PATH_RELATIVE: will try to produce something like /child-of-root/etc/.
  • PATH_RELATIVE: will try to produce something like ../child-of-parent/etc/.
  • SHORTEST: will try to choose whichever is shortest between PATH_RELATIVE and ROOT_PATH_RELATIVE.

Minify Options

Any other defined option will be passed to minurl. Avoid setting stringify to false, as it will prevent any operations performed by this library from being outputted.

Option Profiles

There're two profiles: CAREFUL_PROFILE and COMMON_PROFILE.