# @just4/querystring

> Just for querystring manipulation.

Latest version **2.0.0** (published 2023-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @just4/querystring
pnpm add @just4/querystring
yarn add @just4/querystring
bun add @just4/querystring
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-09-13 |
| First published | 2021-04-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 1 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Mr.Luo |
| Maintainers | heeroluo |
| Keywords | querystring, stringify, parse |

## Links

- npm: https://www.npmjs.com/package/@just4/querystring
- Repository: https://github.com/heeroluo/just4
- Homepage: https://github.com/heeroluo/just4#readme
- Issues: https://github.com/heeroluo/just4/issues
- npm.io page: https://npm.io/package/@just4/querystring

## Dependencies (1)

- [@just4/util](https://npm.io/package/@just4/util.md) ^1.1.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2023-09-13
- 0.1.0-beta.2 (beta) — 2021-04-06
- 1.0.1 — 2023-07-10
- 1.0.0 — 2023-01-27
- 0.2.0-beta.2 — 2022-08-25
- 0.2.0-beta.1 — 2022-05-27
- 0.1.0-beta.4 — 2021-12-07
- 0.1.0-beta.3 — 2021-04-10
- 0.1.0-beta.1 — 2021-04-06

## README

# @just4/querystring

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

## 安装

```bash
npm i @just4/querystring
```

## 调用

### parse

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

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

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

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

### stringify

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

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

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

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

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

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

### concat

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

```javascript
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` 用于替换查询字符串中指定参数的值。它的行为与字符串的同名方法类似，只有匹配上才会替换：

 ```javascript
 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'
 ```

## 相关文档

- [API 文档](https://heeroluo.github.io/just4/querystring/modules/index.html)

## Changelog

### v2.0.0

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

### v1.0.1

- package.json 中增加 `main` 和 `miniprogram` 两个字段。

---
_Source: https://npm.io/package/@just4/querystring · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
