UNPKG

@onesy/utils

Version:
26 lines (24 loc) 921 B
import isValid from './isValid'; import isEnvironment from './isEnvironment'; import cleanValue from './cleanValue'; const optionsDefault = {}; export const getURL = function (value) { let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const options = { ...optionsDefault, ...options_ }; let url; try { if (isValid('url', value)) url = new URL(value);else if (isEnvironment('localhost', value)) url = new URL(value.indexOf('http') > -1 ? value : "https://".concat(value));else { url = new URL(isEnvironment('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 : cleanValue(url.href, { url: true }); } catch (error) {} return ''; }; export default getURL;