查询字符串

由TypeScript为Node.js或浏览器制作的简单而轻量级的查询串.
安装
安装使用 npm install 命令 :
npm install -S @billjs/query-string
概览
API 密码
剖析
将字符串分析为对象。 支持解析多个同名密钥,并将其分解为数组.
解码的密钥和值由 decodeURIComponent 函数.
如果您指定了 fn 参数,这意味着您可以将其用于自定义值.
- 字符串 (单位:千美元)
string源字符串 - [声] (单位:千美元)
string|nulloptional组分隔符,默认值& - [eq] (单位:千美元)
string|nulloptional)键值分隔符,默认值= - [芬 (单位:千美元)解析函数)一个函数,可用于自定义返回值.
- 返回时 (单位:千美元)
object(中文(简体) )
分析一个正常的查询字符串 .
const data = parse('a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
从 URL 解析搜索 .
const data = parse('http://foo.com?a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
当密钥或值被编码时解析 .
const data = parse('test%3D=test%20%26*%20test');
console.log(data); // ==> { 'test=': 'test &* test' }
如果指定了 sep 和 eq , 请解析 .
const data = parse('a#1|b#s|b#s2', '|', '#');
console.log(data); // ==> { a: '1', b: ['s', 's2'] }
分析与数组相同的名称密钥 .
const data = parse('a=1&b=s1&b=s2');
console.log(data); // ==> { a: '1', b: ['s1', 's2'] }
解析用于自定义值 .
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|nulloptional组分隔符,默认值& - [eq] (单位:千美元)
string|nulloptional)键值分隔符,默认值= - [芬 (单位:千美元)字符串功能)一个函数,可用于自定义返回值.
- 返回时 (单位:千美元)
string(中文(简体) )
字符串对象.
const query = stringify({ a: '1', b: 's' });
console.log(query); // ==> 'a=1&b=s'
将数组字符串为同名密钥 .
const query = stringify({ a: '1', b: ['s1', 's2'] });
console.log(query); // ==> 'a=1&b=s1&b=s2'
字符串布尔值 .
const query = stringify({ a: '1', b: 's', c: false });
console.log(query); // ==> 'a=1&b=s&c=false'
如果密钥或值需要编码, 请将其字符串 .
const query = stringify({ 'test=': 'test &* test' });
console.log(query); // ==> 'test%3D=test%20%26*%20test'
如果指定了 sep 和 eq , 则将其固定 .
const query = stringify({ a: '1', b: ['s', 's2'] }, '|', '#');
console.log(query); // ==> 'a#1|b#s|b#s2'
为自定义值而绑定它 .
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'
解析函数
用于自定义剖析的函数 .
宣言是这样的:
type ParseFunction = (key: string, value: string) => any;
字符串功能
自定义字符串的函数 .
宣言是这样的:
type StringifyFunction = (key: string, value: any) => any;