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
Like path.join
but for a URL.
Installation
$ npm install proper-url-join
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, e.g.: //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 totrue
)trailingSlash
: Add a trailing/
(defaults tofalse
)protocolRelative
: Enables support for protocol relative URLs (defaults tofalse
)
Tests
$ npm test
$ npm test -- --watch
during development