@ctrl/url-join

Join strings together and normalize the resulting url

Usage no npm install needed!

<script type="module">
  import ctrlUrlJoin from 'https://cdn.skypack.dev/@ctrl/url-join';
</script>

README

url-join npm CircleCI coverage bundlesize

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