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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@just4/querystring

v2.0.0

Published

Just for querystring manipulation.

Downloads

14

Readme

@just4/querystring

提供 URL 查询字符串(query string)的操作接口。

安装

npm i @just4/querystring

调用

parse

parse 函数可以把一段查询字符串解析为对象:

import { parse } from '@just4/querystring';
parse('id=0&str=hello'); // { id: '0', str: 'hello' }

一般情况下,解析结果的 keyvalue 都是字符串。然而,如果查询字符串中含有重复的 key,那么解析结果中该 key 对应的 value 就是字符串数组。

parse('id=0&id=1'); // { id: ['0', '1'] }

stringify

stringify 函数可以把对象序列化为查询字符串:

import { stringify } from '@just4/querystring';
stringify({ id: '0', str: 'hello' }); // 'id=0&str=hello'

如果值是数组,则会遍历数组里面的每一项,均以对应的 key 进行序列化。

stringify({ id: ['0', '1'] }); // 'id=0&id=1'

注意:如果值是数组以外的引用类型,其序列化结果将是空字符串。例如:

stringify({ obj: { id: 0 } }); // 'obj='

concat

concat 用于给指定 URL 或路径追加查询字符串。即使 URL 或路径中包含查询字符串或者锚点,也可以正常追加:

import { concat } from '@just4/querystring';
concat('https://abc.com/?a=1#hash', {
  b: 2,
  c: 3
}); // 'https://abc.com/?a=1&b=2&c=3#hash'

replace

replace 用于替换查询字符串中指定参数的值。它的行为与字符串的同名方法类似,只有匹配上才会替换:

import { replace } from '@just4/querystring';
replace('abc?a=1&b=2', { a: 2 }); // 'abc?a=2&b=2'
replace('abc?a=1&b=2', { c: 3 }); // 'abc?a=1&b=2'

相关文档

Changelog

v2.0.0

  • 新增 replace 方法。
  • appendToURL 方法改名为 concat
  • 优化 parsestringify 的内部实现。
  • stringifyconcatdata 参数调整为 any 类型。
  • stringify 序列化数组以外的引用类型时,结果为空字符串。
  • parse 的第一个参数不为字符串类型时,返回空对象而不是抛出异常。

v1.0.1

  • package.json 中增加 mainminiprogram 两个字段。