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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ruoguo/k-uri

v1.0.0

Published

URI/URL 工具:查询参数解析、拼接、setUrl、getPathFromState 等

Readme

k-uri

URI/URL 工具:查询参数解析与拼接、URL 修改、跳转封装、高德地图/拨号等。ES Module,无依赖(浏览器环境使用 URL / URLSearchParams)。


安装

npm i k-uri

使用

import { transFun, uri } from 'k-uri';

// 对象转查询字符串
transFun({ a: '1', b: '2' }); // 'a=1&b=2'

// 从 URL 取单个查询参数
uri.getValue('https://example.com?foo=bar', 'foo'); // 'bar'

// 修改当前 URL 的查询参数(设置或删除)
uri.setUrl('https://example.com?x=1', { type: 'set', params: 'y', value: '2' });
uri.setUrl('https://example.com?x=1', { type: 'delete', params: 'x' });

// 从路由 state 解析跳转路径(登录回调等)
uri.getPathFromState({ state: { path: '/order?id=1' }, defaultPath: '/home' });

API

transFun(data?)

将对象转为 URL 查询字符串 key=value&key2=value2

| 参数 | 类型 | 说明 | |------|------|------| | data | Record<string, string> | 键值对,默认 {} |

返回 string


uri.getValue(url, val)

从指定 URL 的查询参数中取出某个 key 的值。

| 参数 | 类型 | 说明 | |------|------|------| | url | string | 完整 URL 或相对路径 | | val | string | 参数名 |

返回 string | null


uri.push({ res, nav, storage? })

根据菜单/资源项拼接查询并跳转,支持站内路由与外链(外链可附带 token)。

| 参数 | 类型 | 说明 | |------|------|------| | res | { url?: string, name?: string } | 资源项 | | nav | (url: string) => void | 跳转函数 | | storage | 可选 | 兼容 getItem 的存储,默认 localStorage,用于读 token |

  • res.url?redirect=jump 时,若存在全局 $axios,会请求该 URL 取 data.url 并整页跳转。
  • 外链(http(s)://)会在 URL 后追加 &token=...(从 storage 读)。

uri.setUrl(url, options)

修改当前 URL 的单个查询参数(设置或删除)。

| 参数 | 类型 | 说明 | |------|------|------| | url | string | 当前完整 URL | | options.type | 'set' \| 'delete' | 默认 'set' | | options.params | string | 参数名 | | options.value | string | 参数值(type='set' 时必填) |

返回 string 新 URL


uri.location(address)

打开高德地图 URI 搜索指定地址(window.location.href 跳转)。

| 参数 | 类型 | 说明 | |------|------|------| | address | string | 关键词/地址 |


uri.tel(phone)

拨打电话(移动端唤起拨号,tel: 协议)。

| 参数 | 类型 | 说明 | |------|------|------| | phone | string | 电话号码 |


uri.getPathFromState({ state, pathParam?, defaultPath? })

从路由 state 或 path 参数中解析出最终跳转路径(用于登录回调等)。

| 参数 | 类型 | 说明 | |------|------|------| | state | 任意 | 路由 state,可为 JSON 字符串或对象(需含 path) | | pathParam | string | 显式 path,优先使用 | | defaultPath | string | 解析失败时的默认路径,默认 '/home' |

返回 string 路径(可含查询串)。state 中除 path 外的字段会合并进查询参数。


License

ISC