UNPKG

@onesy/utils

Version:
34 lines (33 loc) 1.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getURL = void 0; const isValid_1 = __importDefault(require("./isValid")); const isEnvironment_1 = __importDefault(require("./isEnvironment")); const cleanValue_1 = __importDefault(require("./cleanValue")); const optionsDefault = {}; const getURL = (value, options_ = {}) => { const options = Object.assign(Object.assign({}, optionsDefault), options_); let url; try { if ((0, isValid_1.default)('url', value)) url = new URL(value); else if ((0, isEnvironment_1.default)('localhost', value)) url = new URL(value.indexOf('http') > -1 ? value : `https://${value}`); else { url = new URL((0, isEnvironment_1.default)('browser') ? window.location.href : 'https://localhost'); const [pathname, search] = value.split('?').filter(Boolean); if (pathname) url.pathname = pathname; if (search) url.search = search; } return options.URL ? url : (0, cleanValue_1.default)(url.href, { url: true }); } catch (error) { } return ''; }; exports.getURL = getURL; exports.default = exports.getURL;