首页 > 其他分享 >pure js function merge URL objects All In One

pure js function merge URL objects All In One

时间:2023-01-12 13:55:18浏览次数:58  
标签:function const URL js pathname https xgqfrms com

pure js function merge URL objects All In One

Question

image

// ??? OCR 识别 code

https://twitter.com/wesbos/status/1613223775796924417/photo/1

Solution

"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:[email protected]: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'
}

*/

(

标签:function,const,URL,js,pathname,https,xgqfrms,com
From: https://www.cnblogs.com/xgqfrms/p/17046451.html

相关文章

  • JS_2_运算符
    与Java大抵是类似的。 一、算术运算符+、-、*、/、%。适用于:number类型与number类型。number类型与boolean类型(boolean自动转:true--1,false-0)。n......
  • uni-app Pages.json配置
    https://uniapp.dcloud.net.cn/collocation/pages.htmlpages.json 文件用来对uni-app进行全局配置,决定页面文件的路径、窗口样式、原生的导航栏、底部的原生tabbar等......
  • js: 获取标签元素data-*属性值的方法
    js:获取标签元素data-*属性值的方法彭世瑜于2022-05-2309:59:50发布2165收藏1文章标签:javascript前端jquery版权标签上有两个属性data-id和data-use......
  • JS_1_引入js、js变量
    JavaScript,脚本语言,基于HTML,使得网页能够实现与用户的互动。 一、引入js代码1、在html中声明js代码域:<scripttype="text/javascript">js代码...</script>2......
  • js数组去重常见的七种方法
      参考:https://blog.csdn.net/Lguorong/article/details/124301325......
  • node -e "require('nan')" npm 安装 tty.js 时报错 ,解决方法
    ubuntu14.04上报错解决办法:sudoapt-getupdate&&sudoapt-getinstallnodejs-legacy......
  • js的面向对象
    前言说起起面向对象的概念,大家大部分的印象都是与后端相关的。  其实并不是这样,我觉得面-向对象这种思想适合在任何的场景,甚至在现实的场景中。因为后端相关语言在实......
  • rollupjs
    掉落神坛的webpackwebpack诞生之初的根本原因就是处理前端js模块化的工具。如果浏览器本身慢慢的已经支持了模块化。那么webpack存在的意义就不大了。webpack的其它瑕......
  • NGINX配置之二: nginx location proxy_pass 后面的url 加与不加/的区别.
    先给出结果(1)^~开头是前缀匹配,location后面加/也是前缀匹配,只不过匹配范围比不加/要小(2)proxy_pass端口后面没有/,则会将location及其后缀的内容完全拼接到proxy_p......
  • 【javascript】关于 canvas.toDataURL()
    在工作中遇到了奇怪的问题,在此记录。 一、定义canvas.toDataURL()方法是返回一个包含图片展示的数据URL。可以使用 type 参数其类型,默认为PNG格式,图片的分辨率为9......