@googlicius/build-url
Advanced tools
Comparing version 1.0.3 to 1.0.7
export interface UrlOptions { | ||
queryParams: { | ||
queryParams?: { | ||
[x: string]: any; | ||
}; | ||
hash?: string; | ||
path?: string; | ||
returnAbsoluteUrl?: boolean; | ||
} | ||
export default function buildUrl(strUrl: string | UrlOptions, options?: UrlOptions): string; | ||
export default function buildUrl(inputUrl: string | UrlOptions, options?: UrlOptions): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function buildUrl(strUrl, options) { | ||
function buildUrl(inputUrl, options) { | ||
let url; | ||
let isValidUrl = false; | ||
if (typeof strUrl === 'string') { | ||
try { | ||
url = new URL(strUrl); | ||
} | ||
catch (error) { | ||
isValidUrl = true; | ||
let isValidInputUrl = false; | ||
try { | ||
url = new URL(inputUrl); | ||
} | ||
catch (error) { | ||
isValidInputUrl = true; | ||
if (typeof inputUrl === 'string') { | ||
const host = typeof window === 'undefined' | ||
? 'http://example.com' | ||
: window.location.origin; | ||
url = new URL(`${host}/${strUrl}`); | ||
url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, '')}`); | ||
} | ||
else { | ||
url = | ||
typeof window === 'undefined' | ||
? new URL('http://example.com') | ||
: new URL(window.location.href); | ||
} | ||
} | ||
else { | ||
isValidUrl = true; | ||
url = | ||
typeof window === 'undefined' | ||
? new URL('http://example.com') | ||
: new URL(window.location.href); | ||
} | ||
const _options = typeof strUrl === 'string' ? options : strUrl; | ||
const _options = typeof inputUrl === 'string' ? options : inputUrl; | ||
if (_options === null || _options === void 0 ? void 0 : _options.queryParams) { | ||
@@ -39,7 +38,16 @@ for (const key in _options.queryParams) { | ||
} | ||
if (isValidUrl) { | ||
return url.pathname + url.search; | ||
if (_options === null || _options === void 0 ? void 0 : _options.path) { | ||
url.pathname = _options.path; | ||
} | ||
if ((_options === null || _options === void 0 ? void 0 : _options.path) === null) { | ||
url.pathname = ''; | ||
} | ||
if (_options === null || _options === void 0 ? void 0 : _options.hash) { | ||
url.hash = _options.hash; | ||
} | ||
if (isValidInputUrl && !(_options === null || _options === void 0 ? void 0 : _options.returnAbsoluteUrl)) { | ||
return url.pathname + url.search + url.hash; | ||
} | ||
return url.toString(); | ||
} | ||
exports.default = buildUrl; |
{ | ||
"name": "@googlicius/build-url", | ||
"version": "1.0.3", | ||
"version": "1.0.7", | ||
"description": "Build url helper", | ||
@@ -16,5 +16,3 @@ "main": "dist/index.js", | ||
"test": "jest", | ||
"prepare": "husky install", | ||
"publish": "yarn build && npm publish --access-public", | ||
"publish:dry-run": "yarn build && npm publish --access-public --dry-run" | ||
"prepare": "husky install" | ||
}, | ||
@@ -21,0 +19,0 @@ "devDependencies": { |
4242
61