z-util-page
Version:
46 lines (45 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseUrl = parseUrl;
/**
* 解析URL
* @category 辅助函数
* @example
* ```ts
* const url = 'https://www.baidu.com/s?wd=hello#world'
* const result = parseUrl(url)
* ```
* @param url 统一资源定位符
*/
function parseUrl(url) {
let Url = null;
const param = {};
try {
const temp = new URL(url);
Url = {
hash: temp.hash,
host: temp.host,
hostname: temp.hostname,
href: temp.href,
origin: temp.origin,
password: temp.password,
pathname: temp.pathname,
port: temp.port,
protocol: temp.protocol,
search: temp.search,
username: temp.username,
searchParams: param
};
}
catch (error) {
console.log(error);
}
if (Url === null)
return null;
const search = Url.search.slice(1);
const paramList = search.split('&').map(item => item.split('='));
paramList.forEach(item => {
param[item[0]] = item[1];
});
return Url;
}