@billjs/query-string

中文文档
1.0.2发布于 2018/12/13MITTypeScriptCommonJS零依赖近 30 天下载 3228

A simple and lightweight QueryString by TypeScript for Node.js or Browsers.

原始使用文档
文档目录8 节
  1. Installation
  2. Overview
  3. API
  4. parse
  5. stringify
  6. ParseFunction
  7. StringifyFunction
  8. License

query-string Travis-ci Status GitHub license typescript | javascript | node.js Npm Version

A simple and lightweight QueryString by TypeScript for Node.js or Browsers.

Installation

Installation is done using the npm install command:

npm install -S @billjs/query-string

Overview

API

parse

Parse the string to an object. Support the parsing of multiple keys with the same name, and parse them into arrays. The parsed keys and values are decoded by decodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • str (string) source string
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (ParseFunction) a function, it can be used to customize return values.
  • return (object)

Parse a normal query-string.

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

Parse the search from URL.

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

Parse it when if key or value is encoded.

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

Parse it when if sep and eq are specified.

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

Parse the same name key as an array.

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

Parse it for customize value.

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 }

stringify

Stringify the object to a string. Support the stringifying of arrays into keys of the same name. The stringified keys and values are encoded by encodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • obj (object) source object
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (StringifyFunction) a function, it can be used to customize return values.
  • return (string)

Stringify an object.

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

Stringify the array to same name key.

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

Stringify the boolean value.

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

Stringify it when if the key or value need to encoded.

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

Stringify it when if sep and eq are specified.

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

Stringify it for customize value.

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'

ParseFunction

A function for customize parse.

The declaration is like this:

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

StringifyFunction

A function for customize stringify.

The declaration is like this:

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

License

MIT License

版本与发布记录

@billjs/query-string 共发布 3 个版本,首次发布于 2018/12/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2018/12/137 年前
1.0.02018/12/117 年前
1.0.12018/12/117 年前

关于 @billjs/query-string 的常见问题

@billjs/query-string 是什么?

A simple and lightweight QueryString by TypeScript for Node.js or Browsers.

如何安装 @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 和提交记录判断。