url-join-dual
is a library that joins all arguments together and normalizes the resulting URL. This library has been enhanced to be used with both ESM and CommonJS. It is based on the url-join library.
npm install url-join-dual
import urlJoin from 'url-join-dual';
const fullUrl = urlJoin('http://www.google.com', 'a', '/b/cd', '?foo=123', '&bar=456', '#heading-1');
console.log(fullUrl.toString()); // 'http://www.google.com/a/b/cd?foo=123&bar=456#heading-1'
For usage with CommonJS:
const urlJoin = require('url-join-dual');
const fullUrl = urlJoin('http://www.google.com', 'a', '/b/cd', '?foo=123', '&bar=456', '#heading-1');
console.log(fullUrl.toString()); // 'http://www.google.com/a/b/cd?foo=123&bar=456#heading-1'
This library was originally created before the URL API was standardized and widely available in popular runtimes such as browsers and Node.js. Depending on your use-case you might want to consider using the standardized API over this library.
For example, the equivalent code for the above example would look as follows when using the URL API:
const fullUrl = new URL('http://www.google.com');
KATO_Yuumin
MIT