index.js 387 B

123456789101112131415
  1. 'use strict';
  2. module.exports = (url, opts) => {
  3. if (typeof url !== 'string') {
  4. throw new TypeError(`Expected \`url\` to be of type \`string\`, got \`${typeof url}\``);
  5. }
  6. url = url.trim();
  7. opts = Object.assign({https: false}, opts);
  8. if (/^\.*\/|^(?!localhost)\w+:/.test(url)) {
  9. return url;
  10. }
  11. return url.replace(/^(?!(?:\w+:)?\/\/)/, opts.https ? 'https://' : 'http://');
  12. };