npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@simplex2-sdk-util/url

v0.0.2

Published

此模块提供了一系列用于解析和操作 URL 的工具函数。这些函数可以帮助开发者轻松提取 URL 的各个部分(如查询参数、端口、主机名等),并进行相应的处理。所有函数都经过了详细的注释和示例说明,确保易于使用和理解。

Readme

simplex2-sdk-util url

URL 工具库

简介

此模块提供了一系列用于解析和操作 URL 的工具函数。这些函数可以帮助开发者轻松提取 URL 的各个部分(如查询参数、端口、主机名等),并进行相应的处理。所有函数都经过了详细的注释和示例说明,确保易于使用和理解。

安装

npm install @simplex2-sdk-util/url

使用方法

解析 URL 查询参数

getUrlParams(url: string)

将 URL 中的查询参数转换为键值对对象。

示例:

const url = 'https://www.baidu.com?name=张三&age=18';
const params = getUrlParams(url);
console.log(params); // { name: '张三', age: '18' }

合并 URL 查询参数

mixUrlParams(url: string, params?: Record<string, string>)

从给定的 URL 中提取查询参数,并将其与可选的参数对象合并。

示例:

const url = 'https://www.localhost.com?name=张三';
const additionalParams = { age: '18' };
const mergedParams = mixUrlParams(url, additionalParams);
console.log(mergedParams); // { name: '张三', age: '18' }

提取 URL 端口号

getPort(url: string)

从给定的 URL 中提取端口号。

示例:

const url = 'https://www.localhost.com:8080';
const port = getPort(url);
console.log(port); // '8080'

获取 URL 主机部分

getHost(url: string)

获取 URL 的主机部分,不包括协议和路径。

示例:

const url = 'https://www.localhost.com/path';
const host = getHost(url);
console.log(host); // 'www.baidu.com'

获取 URL 主机名

getHostname(url: string)

从给定的 URL 中提取主机名。

示例:

const url = 'https://www.localhost.com/path';
const hostname = getHostname(url);
console.log(hostname); // 'www.baidu.com'

获取 URL 协议类型

getProtocol(url: string)

从给定的 URL 中提取协议类型。

示例:

const url = 'https://www.localhost.com';
const protocol = getProtocol(url);
console.log(protocol); // 'https:'

获取 URL 原始地址

getOrigin(url: string)

获取 URL 的原始地址部分,即协议、主机名和端口号。

示例:

const url = 'https://www.localhost.com:8080/path';
const origin = getOrigin(url);
console.log(origin); // 'https://www.baidu.com:8080'

获取当前页面的 URL 参数

getCurrentUrlParams()

获取当前页面的 URL 参数。

示例:

const params = getCurrentUrlParams();
console.log(params); // 根据当前页面URL解析出的参数对象

获取当前页面的 URL 端口号

getCurrentUrlPort()

获取当前页面的 URL 端口号。

示例:

const port = getCurrentUrlPort();
console.log(port); // 当前页面的端口号

获取当前页面的 URL 主机名

getCurrentUrlHost()

获取当前页面的 URL 主机名。

示例:

const host = getCurrentUrlHost();
console.log(host); // 当前页面的主机名

获取当前页面的 URL 主机名

getCurrentUrlHostname()

获取当前页面的 URL 主机名。

示例:

const hostname = getCurrentUrlHostname();
console.log(hostname); // 当前页面的主机名

获取当前页面的 URL 协议类型

getCurrentUrlProtocol()

获取当前页面的 URL 协议类型。

示例:

const protocol = getCurrentUrlProtocol();
console.log(protocol); // 当前页面的协议类型

获取当前页面的 URL 源点

getCurrentUrlOrigin()

获取当前页面的 URL 源点。

示例:

const origin = getCurrentUrlOrigin();
console.log(origin); // 当前页面的源点

获取当前页面的完整 URL

getCurrentUrl()

获取当前页面的完整 URL。

示例:

const currentUrl = getCurrentUrl();
console.log(currentUrl); // 当前页面的完整URL

获取 URL 锚点部分

getUrlAnchor(url: string)

从给定的 URL 中提取锚点部分。

示例:

const url = 'https://www.baidu.com/path#section1';
const anchor = getUrlAnchor(url);
console.log(anchor); // '#section1'

获取当前页面的 URL 锚点

getCurrentUrlAnchor()

获取当前页面的 URL 锚点。

示例:

const anchor = getCurrentUrlAnchor();
console.log(anchor); // 当前页面的锚点

API 列表

| 函数名称 | 描述 | | --- | --- | | getUrlParams | 将 URL 中的查询参数转换为键值对对象 | | mixUrlParams | 从给定的 URL 中提取查询参数并与其提供的参数对象合并 | | getPort | 从给定的 URL 中提取端口号 | | getHost | 获取 URL 的主机部分,不包括协议和路径 | | getHostname | 从给定的 URL 中提取主机名 | | getProtocol | 从给定的 URL 中提取协议类型 | | getOrigin | 获取 URL 的原始地址部分,即协议、主机名和端口号 | | getCurrentUrlParams | 获取当前页面的 URL 参数 | | getCurrentUrlPort | 获取当前页面的 URL 端口号 | | getCurrentUrlHost | 获取当前页面的 URL 主机名 | | getCurrentUrlHostname | 获取当前页面的 URL 主机名 | | getCurrentUrlProtocol | 获取当前页面的 URL 协议类型 | | getCurrentUrlOrigin | 获取当前页面的 URL 源点 | | getCurrentUrl | 获取当前页面的完整 URL | | getUrlAnchor | 从给定的 URL 中提取锚点部分 | | getCurrentUrlAnchor | 获取当前页面的 URL 锚点 |

注意事项

  • 所有函数均依赖于浏览器环境中的 URL 构造函数。
  • 如果在非浏览器环境中使用,请确保已安装相应的 polyfill 或兼容库。