@ctrl/url-join
v2.0.3
Published
Join strings together and normalize the resulting url
Downloads
6,458
Maintainers
Readme
url-join
A typescript fork of jfromaniello/url-join with additional options.
Install
npm install @ctrl/url-join
Use
import { urlJoin } from '@ctrl/url-join';
const url = urlJoin('http://www.example.com', 'a', '/b/cd', '?foo=123')
// http://www.example.com/a/b/cd?foo=123
Preserve trailing slash
Preserves trailing slashes between each provided string. Not recommended with query parameters.
import { customUrlJoin } from '@ctrl/url-join';
const trailingUrlJoin = customUrlJoin({ trailingSlash: true });
trailingUrlJoin('https://example.com', 'results', '/')
// https://example.com/results/
trailingUrlJoin('https://example.com', '#foobar')
// https://example.com/#foobar
trailingUrlJoin('https://example.com', '#', 'foobar')
// https://example.com/#/foobar
trailingUrlJoin('https://example.com', 'results', '?q=foo', '&page=1')
// https://example.com/results/?q=foo/&page=1 - Probably not what you want
See Also
- jfromaniello/url-join original source
- sindresorhus/query-string properly parsing and formatting query strings
- sindresorhus/normalize-url