Skip to content

Commit deb59bc

Browse files
committed
0.2.0
1 parent b1e60ef commit deb59bc

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

CHANGELOG.md

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

3+
## 0.2.0 / 2019-3-2
4+
5+
- `stringify`新增`filter``convert`参数
6+
37
## 0.1.0 / 2018-11-1
48

59
- 添加 `parse()`

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.1.0-orange.svg)](https://www.npmjs.com/package/@jsmini/querystring)
6+
[![npm](https://img.shields.io/badge/npm-0.2.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

config/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
"error",
2626
"always"
2727
],
28-
"no-console": 0
28+
"no-console": 0,
29+
"no-unused-vars": 0,
2930
}
3031
};

doc/api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,26 @@ parse('a=1&b=2') // { a: 1, b: 2 }
3030
- param {string} [option.sep='&'] 参数分隔符
3131
- param {string} [option.eq='='] 参数赋值符
3232
- param {function} [option.encode=encodeURIComponent] 对参数进行编码的函数
33+
- param {function} [option.filter=(v, k) => true] 对数据进行过滤
34+
- param {function} [option.convert=(v, k) => /*undefined null -> ''*/] 对数据进行转换
3335
- return {string} 序列化字符串
3436

3537
举个例子(要包含代码用例)
3638

3739
```js
3840
stringify({ a: 1, b: 2 }) // 'a=1&b=2'
3941
```
42+
43+
filter默认不过滤任何数据,但有时候想把空字符串过滤的话,会非常有用
44+
45+
```js
46+
stringify({ a: null}) // 'a='
47+
stringify({ a: null}, {filter: v => v !== null }) // ''
48+
```
49+
50+
convert用来对数据过滤,默认行为是将undefined和null转换为空字符
51+
52+
```js
53+
stringify({ a: null}) // 'a='
54+
stringify({ a: null}, {convert: v => v }) // 'a=null'
55+
```

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.1.0",
3+
"version": "0.2.0",
44
"description": "最好用的js第三方库脚手架",
55
"main": "dist/index.js",
66
"jsnext:main": "dist/index.esm.js",

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,19 @@ export function stringify(obj, option = {}) {
6767
sep: '&',
6868
eq: '=',
6969
encode: encodeURIComponent,
70+
filter: (v, k) => true,
71+
// undefined or null > ''
72+
convert: (v, k) => typeof v === 'undefined' || v === null ? '' : v,
7073
}, option);
7174

7275
let res = '';
7376

7477
const isEncode = type(opt.encode) === 'function';
7578

7679
for (let key in obj) {
77-
if (hasOwnProp(obj, key)) {
78-
const str = isPrimitive(obj[key]) ? String(obj[key]) : toString(obj[key]);
80+
if (hasOwnProp(obj, key) && opt.filter(obj[key], key)) {
81+
const val = opt.convert(obj[key], key);
82+
const str = isPrimitive(val) ? String(val) : toString(val);
7983

8084
const k = isEncode ? opt.encode(key) : key;
8185
const v = isEncode ? opt.encode(str) : str;

0 commit comments

Comments
 (0)