xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

pure js function merge URL objects All In One

pure js function merge URL objects All In One

Question

image

// ??? 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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-01-12 13:48  xgqfrms  阅读(26)  评论(1编辑  收藏  举报