Skip to content

Commit

Permalink
feat: add url util function
Browse files Browse the repository at this point in the history
- Add `urlJoin` for join url
  • Loading branch information
Chinlinlee committed Apr 25, 2022
1 parent ce0853e commit 4971704
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions utils/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path');
const _ = require('lodash');
const { URL } = require('url');

function urlJoin(subPath, baseUrl) {
let baseUrlSplit = _.compact(baseUrl.split('/'));
if ( baseUrlSplit.length > 2 ) {
let subPathInBaseUrl = baseUrlSplit.slice(2).join("/");
let newSubPath = path.join(subPathInBaseUrl, subPath);
let joinURL = new URL(newSubPath, baseUrl);
return joinURL.href;
} else {
let joinURL = new URL(subPath, baseUrl);
return joinURL.href;
}
}


module.exports.urlJoin = urlJoin;

0 comments on commit 4971704

Please sign in to comment.