pure js function merge URL objects All In One
pure js function merge URL objects All In One
Question
// ??? OCR 识别 code
https://twitter.com/wesbos/status/1613223775796924417/photo/1
Solutions
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-01-12
* @modified
*
* @description merge URL objects
* @difficulty Easy
* @time_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/17046451.html
* @link https://twitter.com/wesbos/status/1613223775796924417
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
const uo = new URL(`http://user:pass@example.com:8080/pathname?=query=xgqfrms&limit=10#hash`);
const ug = new URL(`https://google.com`);
function mergeURLs(uo, ug) {
const {
hash,
password,
pathname,
port,
search,
username,
} = uo;
const {
hostname,
origin,
protocol,
} = ug;
return {
hostname,
host: `${hostname}:${port}`,
origin: `${origin}:${port}`,
protocol,
hash,
href: `${origin}:${port}${pathname}${search}`,
password,
pathname,
port,
search,
username,
};
}
const test = mergeURLs(uo, ug);
console.log(`test =`, test);
// export default mergeURLs;
// export {
// mergeURLs,
// };
/*
$ node merge-urls.js
test = {
hostname: 'google.com',
host: 'google.com:8080',
origin: 'https://google.com:8080',
protocol: 'https:',
hash: '#hash',
href: 'https://google.com:8080/pathname?=query=xgqfrms&limit=10',
password: 'pass',
pathname: '/pathname',
port: '8080',
search: '?=query=xgqfrms&limit=10',
username: 'user'
}
*/
lib
// try ...
https://www.npmjs.com/package/immurl
https://github.com/tom-sherman/immurl
TypeScript
https://github.com/learning-js-by-reading-source-codes/immurl
refs
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17046451.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2022-01-12 kaggle All In One
2022-01-12 leetcode online interview All In One
2022-01-12 游戏玩家 专有名词 All In One
2022-01-12 leetcode 面试必刷的算法 100 题 All In One
2021-01-12 PostCSS All In One
2021-01-12 zsh terminal set infinity scroll height
2021-01-12 npm version ^ meaning