# @billjs/query-string 中文文档

规范地址：https://npmcn.com/package/%40billjs/query-string/

版本：1\.0\.2
npm 元数据更新时间：2022-04-04T18:49:50.016Z
中文译文更新时间：2026-11-08T02:32:02.903Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40billjs/query-string
原文对照：https://npmcn.com/package/%40billjs/query-string/?lang=original
项目主页：https://github.com/billjs/query-string#readme
源码仓库：https://github.com/billjs/query-string

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：15.9 kB，5 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2018-12-11T11:26:11.552Z
- 最新版本发布：2018-12-13T09:02:54.665Z
- 近 30 天下载量：3228（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

由TypeScript为Node.js或浏览器制作的简单而轻量级的查询串.

## 安装

```sh
npm install @billjs/query-string
```

也可以使用：`pnpm add @billjs/query-string`、`yarn add @billjs/query-string`、`bun add @billjs/query-string`

## README

# 查询字符串 [![Travis-ci Status](https://api.travis-ci.org/billjs/query-string.svg?branch=master)](https://travis-ci.org/billjs/query-string) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/billjs/query-string/blob/master/LICENSE) ![typescript | javascript | node.js](https://img.shields.io/badge/language-typescript%20%7C%20javascript%20%7C%20node.js-yellow.svg) [![Npm Version](https://img.shields.io/npm/v/@billjs/query-string.svg)](https://www.npmjs.com/package/@billjs/query-string)

由TypeScript为Node.js或浏览器制作的简单而轻量级的查询串.

## 安装

安装使用 `npm install` 命令 :

```shell
npm install -S @billjs/query-string
```

## 概览

-   [剖析](https://npmcn.com/package/%40billjs/query-string/#readme-parse)
-   [字符串](https://npmcn.com/package/%40billjs/query-string/#readme-stringify)

## API 密码

### 剖析

将字符串分析为对象。 支持解析多个同名密钥,并将其分解为数组. 解码的密钥和值由 `decodeURIComponent` 函数. 如果您指定了 `fn` 参数,这意味着您可以将其用于自定义值.

-   **字符串** (单位:千美元)`string`源字符串
-   _**\[声\]**_ (单位:千美元)`string` | `null` `optional`组分隔符,默认值 `&`
-   _**\[eq\]**_ (单位:千美元)`string` | `null` `optional`)键值分隔符,默认值 `=`
-   _**\[芬**_ (单位:千美元)[解析函数](https://npmcn.com/package/%40billjs/query-string/#readme-parsefunction))一个函数,可用于自定义返回值.
-   **返回时** (单位:千美元)`object`(中文(简体) )

分析一个正常的查询字符串 .

```typescript
const data = parse('a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
```

从 URL 解析搜索 .

```typescript
const data = parse('http://foo.com?a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
```

当密钥或值被编码时解析 .

```typescript
const data = parse('test%3D=test%20%26*%20test');
console.log(data); // ==> { 'test=': 'test &* test' }
```

如果指定了 sep 和 eq , 请解析 .

```typescript
const data = parse('a#1|b#s|b#s2', '|', '#');
console.log(data); // ==> { a: '1', b: ['s', 's2'] }
```

分析与数组相同的名称密钥 .

```typescript
const data = parse('a=1&b=s1&b=s2');
console.log(data); // ==> { a: '1', b: ['s1', 's2'] }
```

解析用于自定义值 .

```typescript
const fn = (key: string, value: string) => {
  if (key === 'b') return +value;
  if (key === 'c') return { on: true, off: false }[value];
  return value;
};
const data = parse('a=test&b=1&c=on', null, null, fn);
console.log(data); // ==> { a: 'test', b: 1, c: true }
```

### 字符串

将对象绑入字符串。 支持将数组串入同名密钥. 字符串密钥和值由 `encodeURIComponent` 函数. 如果您指定了 `fn` 参数,这意味着您可以将其用于自定义值.

-   **目标** (单位:千美元)`object`源对象
-   _**\[声\]**_ (单位:千美元)`string` | `null` `optional`组分隔符,默认值 `&`
-   _**\[eq\]**_ (单位:千美元)`string` | `null` `optional`)键值分隔符,默认值 `=`
-   _**\[芬**_ (单位:千美元)[字符串功能](https://npmcn.com/package/%40billjs/query-string/#readme-stringifyfunction))一个函数,可用于自定义返回值.
-   **返回时** (单位:千美元)`string`(中文(简体) )

字符串对象.

```typescript
const query = stringify({ a: '1', b: 's' });
console.log(query); // ==> 'a=1&b=s'
```

将数组字符串为同名密钥 .

```typescript
const query = stringify({ a: '1', b: ['s1', 's2'] });
console.log(query); // ==> 'a=1&b=s1&b=s2'
```

字符串布尔值 .

```typescript
const query = stringify({ a: '1', b: 's', c: false });
console.log(query); // ==> 'a=1&b=s&c=false'
```

如果密钥或值需要编码, 请将其字符串 .

```typescript
const query = stringify({ 'test=': 'test &* test' });
console.log(query); // ==> 'test%3D=test%20%26*%20test'
```

如果指定了 sep 和 eq , 则将其固定 .

```typescript
const query = stringify({ a: '1', b: ['s', 's2'] }, '|', '#');
console.log(query); // ==> 'a#1|b#s|b#s2'
```

为自定义值而绑定它 .

```typescript
const fn = (key, value) => {
  if (key === 'c') return value ? 'on' : 'off';
  return value;
};
const obj = { a: 'test', b: 1, c: true };
const query = stringify(obj, null, null, fn);
console.log(query); // ==> 'a=test&b=1&c=on'
```

### 解析函数

用于自定义剖析的函数 .

宣言是这样的:

```typescript
type ParseFunction = (key: string, value: string) => any;
```

### 字符串功能

自定义字符串的函数 .

宣言是这样的:

```typescript
type StringifyFunction = (key: string, value: any) => any;
```

## 许可证

[MIT 许可证](https://npmcn.com/package/%40billjs/query-string/LICENSE)

## 常见问题

### @billjs/query-string 是什么？

由TypeScript为Node\.js或浏览器制作的简单而轻量级的查询串\.

### 如何安装 @billjs/query-string？

在项目目录运行 npm install @billjs/query-string，或使用 pnpm add / yarn add / bun add。

### @billjs/query-string 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @billjs/query-string 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @billjs/query-string 可以商用吗？

@billjs/query-string 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @billjs/query-string 的下载量有多少？

根据 npm 官方下载统计，@billjs/query-string 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,228 次，日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @billjs/query-string 还在维护吗？

最新版本 1\.0\.2 发布于 2018/12/13（7 年前）；至今共发布 3 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/12/11。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

