Skip to content

Commit c7a8ba7

Browse files
committed
0.6.0
1 parent 632e2b7 commit c7a8ba7

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 变更日志
22

3+
## 0.6.0 / 2019-3-19
4+
5+
- feat: parse的decode增加isKey参数
6+
- feat: stringify的encode增加isKey参数
7+
- feat: stringify新增addQueryPrefix参数
8+
39
## 0.5.0 / 2019-3-19
410

511
- feat: parse新增filter和reduce参数

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![](https://img.shields.io/badge/Powered%20by-jslib%20querystring-brightgreen.svg)](https://github.com/yanhaijing/jslib-querystring)
44
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jsmini/querystring/blob/master/LICENSE)
55
[![Build Status](https://travis-ci.org/jsmini/querystring.svg?branch=master)](https://travis-ci.org/jsmini/querystring)
6-
[![npm](https://img.shields.io/badge/npm-0.5.0-orange.svg)](https://www.npmjs.com/package/@jsmini/querystring)
6+
[![npm](https://img.shields.io/badge/npm-0.6.0-orange.svg)](https://www.npmjs.com/package/@jsmini/querystring)
77
[![NPM downloads](http://img.shields.io/npm/dm/@jsmini/querystring.svg?style=flat-square)](http://www.npmtrends.com/@jsmini/querystring)
88
[![Percentage of issues still open](http://isitmaintained.com/badge/open/jsmini/querystring.svg)](http://isitmaintained.com/project/jsmini/querystring "Percentage of issues still open")
99

doc/api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ url参数处理库
1111
- param {string} [option.sep='&'] 参数分隔符
1212
- param {string} [option.eq='='] 参数赋值符
1313
- param {boolean} [option.ignoreQueryPrefix=false] 忽略参数中的`?`
14-
- param {function} [option.decode=decodeURIComponent] 对参数进行解码的函数
14+
- param {function} [option.decode=(x, isKey)] 对参数进行解码的函数
1515
- param {function} [option.filter=(v, k) => true] 对数据进行过滤
1616
- param {function} [option.convert=(v, k)] 对数据进行转换
1717
- param {function} [option.reduce=(prev, v, k)] 自定义数据处理流程
@@ -70,7 +70,8 @@ parse('a=1&a=2&a=3', {
7070
- param {object} [option] 可选参数
7171
- param {string} [option.sep='&'] 参数分隔符
7272
- param {string} [option.eq='='] 参数赋值符
73-
- param {function} [option.encode=encodeURIComponent] 对参数进行编码的函数
73+
- param {boolean} [option.addQueryPrefix=false] 是否在前面添加?
74+
- param {function} [option.encode=(x, isKey)] 对参数进行编码的函数
7475
- param {function} [option.filter=(v, k) => true] 对数据进行过滤
7576
- param {function} [option.convert=(v, k) => /*undefined null -> ''*/] 对数据进行转换
7677
- param {function} [option.reduce=(prev, v, k)] 自定义数据处理流程

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface parseOption {
44
sep?: string;
55
eq?: string;
66
ignoreQueryPrefix?: boolean;
7-
decode?: (x: string) => string;
7+
decode?: (x: string, isKey?: boolean) => string;
88
filter?: (v: string, k?: string) => boolean;
99
convert?: (v: string, k?: string) => any;
1010
reduce?: (prev: object, v: string, k?: string) => object;
@@ -13,7 +13,8 @@ interface parseOption {
1313
interface stringifyOption {
1414
sep?: string;
1515
eq?: string;
16-
encode?: (x: string) => string;
16+
addQueryPrefix?: boolean;
17+
encode?: (x: string, isKey?: boolean) => string;
1718
filter?: (v: any, k?: string) => boolean;
1819
convert?: (v: any, k?: string) => any;
1920
reduce?: (prev: object[], v: any, k?: string) => object[];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsmini/querystring",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "最好用的js第三方库脚手架",
55
"main": "dist/index.js",
66
"jsnext:main": "dist/index.esm.js",

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function parse(str, option = {}) {
1111
sep: '&',
1212
eq: '=',
1313
ignoreQueryPrefix: false,
14-
decode: decodeURIComponent,
14+
decode: (x, iskey) => decodeURIComponent(x),
1515
filter: (v, k) => true,
1616
convert: (v, k) => v,
1717
reduce: false, // (prev, v, k) => prev
@@ -31,7 +31,7 @@ export function parse(str, option = {}) {
3131

3232
for (let i = 0; i < arr.length; i++) {
3333
const arr2 = arr[i].split(opt.eq);
34-
const k = isDecode ? opt.decode(arr2[0]) : arr2[0];
34+
const k = isDecode ? opt.decode(arr2[0], true) : arr2[0];
3535
const v = isDecode ? opt.decode(arr2[1]) : arr2[1];
3636

3737
if (opt.filter(v, k)) {
@@ -58,7 +58,8 @@ export function stringify(obj, option = {}) {
5858
const opt = extend({
5959
sep: '&',
6060
eq: '=',
61-
encode: encodeURIComponent,
61+
addQueryPrefix: false,
62+
encode: (x, isKey) => encodeURIComponent(x),
6263
filter: (v, k) => true,
6364
// undefined or null > ''
6465
convert: (v, k) => typeof v === 'undefined' || v === null ? '' : v,
@@ -81,9 +82,9 @@ export function stringify(obj, option = {}) {
8182

8283
let str = '';
8384
for (let i = 0; i < res.length; i++) {
84-
const k = isEncode ? opt.encode(res[i].k) : res[i].k;
85+
const k = isEncode ? opt.encode(res[i].k, true) : res[i].k;
8586
const v = isEncode ? opt.encode(res[i].v) : res[i].v;
8687
str = str + (str === '' ? '' : opt.sep) + k + opt.eq + v;
8788
}
88-
return str;
89+
return opt.addQueryPrefix ? '?' + str : str;
8990
}

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ describe('单元测试', function() {
3838

3939
var a = parse('a=%3D', { decode: function (x) {return x} })
4040
expect(a).to.eql({ a: '%3D'});
41+
42+
var a = parse('a=%3D', { decode: function (x, isKey) {return isKey ? 1 : 2} })
43+
expect(a).to.eql({ 1: 2 });
4144
})
4245

4346
it('option.filter', function() {
@@ -81,6 +84,14 @@ describe('单元测试', function() {
8184
expect(a).to.eql('a=1&b=2&c=3&d=4');
8285
});
8386

87+
it('addQueryPrefix', function() {
88+
var a = stringify({ a: '1'});
89+
expect(a).to.eql('a=1');
90+
91+
var a = stringify({ a: '1'}, { addQueryPrefix: true });
92+
expect(a).to.eql('?a=1');
93+
});
94+
8495
it('option', function() {
8596
var a = stringify({ a: '1', b: '2', c: '3'}, {sep: '+', eq: ':'});
8697
expect(a).to.eql('a:1+b:2+c:3');
@@ -92,6 +103,9 @@ describe('单元测试', function() {
92103

93104
var a = stringify({ a: '='}, { encode: function (x) {return x} })
94105
expect(a).to.eql('a==');
106+
107+
var a = stringify({ a: '='}, { encode: function (x, isKey) {return isKey ? 1 : 2} })
108+
expect(a).to.eql('1=2');
95109
})
96110

97111
it('option.filter', function() {

0 commit comments

Comments
 (0)