Skip to content

Commit 2583f6e

Browse files
committed
add filterData & validSign.
1 parent 27f9e08 commit 2583f6e

File tree

6 files changed

+416
-21
lines changed

6 files changed

+416
-21
lines changed

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ $ yarn add parse-string
1919

2020
- parse string into Map
2121
- format data
22+
- filter and verify data.
23+
- verify signature.
2224

2325
## API
2426

@@ -53,9 +55,25 @@ Parse the msbody to the specified result.
5355
- `customize` - map of custom function
5456
- `msbody` - msbody of need parse
5557

58+
### filterData (options: FilterData.options[], customize?: Record<string, Function>): (data: Record<string, any>) => Record<string, any>
59+
60+
Filter and verify data.
61+
62+
- `options` - filter options
63+
- `customize` - map of custom function
64+
- `data` - data of need filter
65+
66+
### validSign (options: string, sign: string = 'sign'): (data: Record<string, any>) => boolean
67+
68+
Verify signature.
69+
70+
- `options` - style of signature
71+
- `sign` - feild of signature
72+
- `data` - data of submit
73+
5674
## Usages
5775

58-
Example:
76+
Example: Parse string
5977

6078
```js
6179
import { parseData } from 'parse-string'
@@ -157,6 +175,63 @@ parseData(options, customize)(data)
157175
// }
158176
```
159177

178+
Example: Filter and verify data
179+
180+
```js
181+
import { filterData, validSign } from 'parse-string'
182+
183+
const customize = {
184+
isPassword: value => /^(?=.*[A-Za-z])[A-Za-z0-9$@$!%*#?&]/.test(value)
185+
}
186+
187+
const options = [
188+
{
189+
key: 'username',
190+
type: 'string',
191+
rules: [
192+
{ required: true, message: '用户名不能为空' },
193+
{ min: 4, max: 12, message: '用户名长度不能小于4或大于12(字符)' },
194+
{ pattern: '^[a-zA-Z]{1}[a-zA-Z0-9\_\-]', message: '用户名格式错误' }
195+
]
196+
},
197+
{
198+
key: 'password',
199+
type: 'string',
200+
rules: [
201+
{ required: true, message: '密码不能为空' },
202+
{ min: 6, max: 15, message: '密码长度不能小于6或大于15(字符)' },
203+
{ validator: 'isPassword', message: '密码格式错误' }
204+
]
205+
},
206+
{
207+
key: 'items',
208+
type: 'string[]',
209+
defaultValue: []
210+
},
211+
{
212+
key: 'sign',
213+
type: 'string',
214+
md5: '${password}${username}'
215+
}
216+
]
217+
218+
const data = { username: 'thondery', password: 'a123456', items: '1001,1002,1003' }
219+
220+
try {
221+
let result = filterData(options, customize)(data)
222+
// {
223+
// username: 'thondery',
224+
// password: 'a123456',
225+
// items: ['1001', '1002', '1003'],
226+
// sign: '61a0375131b33b72b56e4e244d0b2f29'
227+
// }
228+
} catch (error) {
229+
console.error(error.message)
230+
}
231+
232+
validSign('${password}${username}', 'sign')({ username: 'thondery', password: 'a123456', sign: '61a0375131b33b72b56e4e244d0b2f29' })
233+
// true or false
234+
```
160235

161236
## License
162237

@@ -169,4 +244,4 @@ this repo is released under the [MIT License](https://github.com/kenote/parse-st
169244
[travis-image]: https://travis-ci.com/kenote/parse-string.svg?branch=main
170245
[travis-url]: https://travis-ci.com/kenote/parse-string
171246
[licensed-image]: https://img.shields.io/badge/license-MIT-blue.svg
172-
[licensed-url]: https://github.com/kenote/parse-string/blob/master/LICENSE
247+
[licensed-url]: https://github.com/kenote/parse-string/blob/main/LICENSE

dist/index.js

Lines changed: 135 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,144 @@ var __spread = (this && this.__spread) || function () {
3131
return ar;
3232
};
3333
Object.defineProperty(exports, "__esModule", { value: true });
34-
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = void 0;
34+
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = exports.checkLength = exports.validSign = exports.filterData = void 0;
3535
var lodash_1 = require("lodash");
3636
var rule_judgment_1 = require("rule-judgment");
37-
function parseData(options, customize) {
37+
var MD5 = require("md5.js");
38+
function filterData(options, customize) {
3839
return function (data) {
3940
var e_1, _a;
41+
var values = {};
42+
try {
43+
for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
44+
var item = options_1_1.value;
45+
var key = item.key, type = item.type, rules = item.rules, format = item.format, defaultValue = item.defaultValue, md5 = item.md5, separator = item.separator;
46+
var value = data[key];
47+
if (/\[\]$/.test(type) && !lodash_1.isArray(value)) {
48+
value = toValue('string')(value || '').split(separator || /\,/);
49+
}
50+
if (/\[\]$/.test(type) && lodash_1.isArray(value)) {
51+
var _b = __read(type.match(/(\S+)(\[\])$/), 2), itype = _b[1];
52+
value = lodash_1.compact(value).map(toValue(itype));
53+
rules && value.forEach(validateRule(rules, customize));
54+
if (defaultValue && value.length === 0) {
55+
value = defaultValue;
56+
}
57+
if (format) {
58+
value = value.map(formatData(format, customize));
59+
}
60+
}
61+
else {
62+
value = toValue(type)(value);
63+
rules && validateRule(rules, customize)(value);
64+
value = value || defaultValue;
65+
if (format) {
66+
value = formatData(format, customize)(value);
67+
}
68+
if (md5) {
69+
value = new MD5().update(lodash_1.template(md5)(values)).digest('hex');
70+
}
71+
}
72+
lodash_1.set(values, key, value);
73+
}
74+
}
75+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
76+
finally {
77+
try {
78+
if (options_1_1 && !options_1_1.done && (_a = options_1.return)) _a.call(options_1);
79+
}
80+
finally { if (e_1) throw e_1.error; }
81+
}
82+
return values;
83+
};
84+
}
85+
exports.filterData = filterData;
86+
function validSign(options, sign) {
87+
if (sign === void 0) { sign = 'sign'; }
88+
return function (data) {
89+
var md5 = new MD5().update(lodash_1.template(options)(data)).digest('hex');
90+
return data[sign] === md5;
91+
};
92+
}
93+
exports.validSign = validSign;
94+
function validateRule(rules, customize) {
95+
return function (value) {
96+
var e_2, _a;
97+
try {
98+
for (var rules_1 = __values(rules), rules_1_1 = rules_1.next(); !rules_1_1.done; rules_1_1 = rules_1.next()) {
99+
var rule = rules_1_1.value;
100+
var required = rule.required, message = rule.message, min = rule.min, max = rule.max, pattern = rule.pattern, validator = rule.validator;
101+
if (required && (lodash_1.isUndefined(value) || value === '')) {
102+
throw new Error(message);
103+
}
104+
if (lodash_1.isString(value)) {
105+
if (min && checkLength(value) < min) {
106+
throw new Error(message);
107+
}
108+
if (max && checkLength(value) > max) {
109+
throw new Error(message);
110+
}
111+
if (pattern) {
112+
var reg = getRegexp(pattern);
113+
if (!reg.test(value)) {
114+
throw new Error(message);
115+
}
116+
}
117+
if (validator && lodash_1.isString(validator)) {
118+
if (customize && Object.keys(customize).includes(validator)) {
119+
validator = customize[validator];
120+
}
121+
}
122+
if (validator && lodash_1.isFunction(validator)) {
123+
if (!validator(value)) {
124+
throw new Error(message);
125+
}
126+
}
127+
}
128+
}
129+
}
130+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
131+
finally {
132+
try {
133+
if (rules_1_1 && !rules_1_1.done && (_a = rules_1.return)) _a.call(rules_1);
134+
}
135+
finally { if (e_2) throw e_2.error; }
136+
}
137+
};
138+
}
139+
function checkLength(str) {
140+
var e_3, _a;
141+
var size = 0;
142+
if (lodash_1.isNull(str))
143+
return size;
144+
var arr = str.split('');
145+
try {
146+
for (var arr_1 = __values(arr), arr_1_1 = arr_1.next(); !arr_1_1.done; arr_1_1 = arr_1.next()) {
147+
var word = arr_1_1.value;
148+
size++;
149+
(/[^\x00-\xff]/g.test(word)) && size++;
150+
}
151+
}
152+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
153+
finally {
154+
try {
155+
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) _a.call(arr_1);
156+
}
157+
finally { if (e_3) throw e_3.error; }
158+
}
159+
return size;
160+
}
161+
exports.checkLength = checkLength;
162+
function parseData(options, customize) {
163+
return function (data) {
164+
var e_4, _a;
40165
if (!options)
41166
return data;
42167
var separator = options.separator, collection = options.collection, omits = options.omits;
43168
var list = data.split(separator);
169+
var notResults = collection.filter(rule_judgment_1.default({ result: { $exists: false } }));
44170
var values = list.map(function (v, i) {
45-
var _a = collection[i] || {}, type = _a.type, format = _a.format;
171+
var _a = notResults[i] || {}, type = _a.type, format = _a.format;
46172
var value = formatData(format, customize)(toValue(type)(v));
47173
return value;
48174
});
@@ -54,12 +180,12 @@ function parseData(options, customize) {
54180
lodash_1.set(obj, item.key, formatData(item.format, customize)(getResultValue(item.result, customize)(obj)));
55181
}
56182
}
57-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
183+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
58184
finally {
59185
try {
60186
if (results_1_1 && !results_1_1.done && (_a = results_1.return)) _a.call(results_1);
61187
}
62-
finally { if (e_1) throw e_1.error; }
188+
finally { if (e_4) throw e_4.error; }
63189
}
64190
return lodash_1.omit(obj, omits || []);
65191
};
@@ -93,7 +219,7 @@ function parseBody(options, customize) {
93219
exports.parseBody = parseBody;
94220
function formatData(formats, customize) {
95221
return function (value) {
96-
var e_2, _a;
222+
var e_5, _a;
97223
formats = lodash_1.isArray(formats) ? formats : lodash_1.compact([formats]);
98224
if (formats.length === 0)
99225
return value;
@@ -103,12 +229,12 @@ function formatData(formats, customize) {
103229
value = formatUtil(format, customize)(value);
104230
}
105231
}
106-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
232+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
107233
finally {
108234
try {
109235
if (formats_1_1 && !formats_1_1.done && (_a = formats_1.return)) _a.call(formats_1);
110236
}
111-
finally { if (e_2) throw e_2.error; }
237+
finally { if (e_5) throw e_5.error; }
112238
}
113239
return value;
114240
};
@@ -224,7 +350,7 @@ function toValue(type) {
224350
}
225351
}
226352
else {
227-
if (type === 'string') {
353+
if (type === 'string' && !lodash_1.isUndefined(value)) {
228354
val = lodash_1.isPlainObject(value) ? JSON.stringify(value) : String(value);
229355
}
230356
else if (type === 'date' && lodash_1.isNumber(value)) {

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parse-string",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Parse the string into a Map.",
55
"main": "dist/index.js",
66
"typings": "types/index.d.ts",
@@ -12,6 +12,7 @@
1212
"build": "rm -rf ./dist && tsc"
1313
},
1414
"dependencies": {
15+
"md5.js": "^1.3.5",
1516
"rule-judgment": "^1.1.3"
1617
},
1718
"devDependencies": {
@@ -33,6 +34,8 @@
3334
"parser",
3435
"format",
3536
"map",
36-
"msgbody"
37+
"msgbody",
38+
"filter",
39+
"validator"
3740
]
3841
}

0 commit comments

Comments
 (0)