JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32581
  • Score
    100M100P100Q149949F
  • License MIT

Like `path.join` but for a URL

Package Exports

  • proper-url-join

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (proper-url-join) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

proper-url-join

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge

Like path.join but for a URL.

Installation

$ npm install proper-url-join --save-dev

Motivation

There are a lot of packages that attempt to provide this functionality but they all have issues.
This package exists with the hope to do it right:

  • Consistent behavior
  • Support adding/removing leading and trailing slashes
  • Supports absolute URLs, e.g.: http//google.com
  • Supports protocol relative URLs: //google.com
  • Supports query strings

Usage

import urlJoin from 'proper-url-join';

urlJoin('foo', 'bar');  // /foo/bar
urlJoin('/foo/', '/bar/');  // /foo/bar
urlJoin('foo', '', 'bar');  // /foo/bar
urlJoin('foo', undefined, 'bar');  // /foo/bar
urlJoin('foo', null, 'bar');  // /foo/bar

// With leading & trailing slash options
urlJoin('foo', 'bar', { leadingSlash: false });  // foo/bar
urlJoin('foo', 'bar', { trailingSlash: true });  // /foo/bar/
urlJoin('foo', 'bar', { leadingSlash: false, trailingSlash: true });  // foo/bar/

// Absolute URLs
urlJoin('http://google.com', 'foo');  // http://google.com/foo

// Protocol relative URLs
urlJoin('//google.com', 'foo', { protocolRelative: true });  // //google.com/foo

// With query string
urlJoin('foo', 'bar?queryString');  // /foo/bar?queryString
urlJoin('foo', 'bar?queryString', { trailingSlash: true });  // /foo/bar/?queryString

Available options:

  • leadingSlash: Add a leading / (defaults to true)
  • trailingSlash: Add a trailing / (defaults to false)
  • protocolRelative: Enables support for protocol relative URLs (defaults to false)

Tests

$ npm test
$ npm test -- --watch during development

License

MIT License