resolve-pathname Travis npm package

resolve-pathname resolves URL pathnames identical to the way browsers resolve the pathname of an <a href> value. The goals are:

  • 100% compatibility with browser pathname resolution
  • Pure JavaScript implementation (no DOM dependency)

Installation

Using npm:

  1. $ npm install --save resolve-pathname

Then, use as you would anything else:

  1. // using ES6 modules
  2. import resolvePathname from 'resolve-pathname'
  3. // using CommonJS modules
  4. var resolvePathname = require('resolve-pathname')

The UMD build is also available on unpkg:

  1. <script src="https://unpkg.com/resolve-pathname/umd/resolve-pathname.min.js"></script>

You can find the library on window.resolvePathname.

Usage

  1. import resolvePathname from 'resolve-pathname'
  2. // Simply pass the pathname you'd like to resolve. Second
  3. // argument is the path we're coming from, or the current
  4. // pathname. It defaults to "/".
  5. resolvePathname('about', '/company/jobs') // /company/about
  6. resolvePathname('../jobs', '/company/team/ceo') // /company/jobs
  7. resolvePathname('about') // /about
  8. resolvePathname('/about') // /about
  9. // Index paths (with a trailing slash) are also supported and
  10. // work the same way as browsers.
  11. resolvePathname('about', '/company/info/') // /company/info/about
  12. // In browsers, it's easy to resolve a URL pathname relative to
  13. // the current page. Just use window.location! e.g. if
  14. // window.location.pathname == '/company/team/ceo' then
  15. resolvePathname('cto', window.location.pathname) // /company/team/cto
  16. resolvePathname('../jobs', window.location.pathname) // /company/jobs

Prior Work

  • url.resolve - node’s url.resolve implementation for full URLs
  • resolve-url - A DOM-dependent implementation of the same algorithm