Skip to content

Commit

Permalink
add filterData & validSign.
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Nov 3, 2020
1 parent 27f9e08 commit 2583f6e
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 21 deletions.
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ $ yarn add parse-string

- parse string into Map
- format data
- filter and verify data.
- verify signature.

## API

Expand Down Expand Up @@ -53,9 +55,25 @@ Parse the msbody to the specified result.
- `customize` - map of custom function
- `msbody` - msbody of need parse

### filterData (options: FilterData.options[], customize?: Record<string, Function>): (data: Record<string, any>) => Record<string, any>

Filter and verify data.

- `options` - filter options
- `customize` - map of custom function
- `data` - data of need filter

### validSign (options: string, sign: string = 'sign'): (data: Record<string, any>) => boolean

Verify signature.

- `options` - style of signature
- `sign` - feild of signature
- `data` - data of submit

## Usages

Example:
Example: Parse string

```js
import { parseData } from 'parse-string'
Expand Down Expand Up @@ -157,6 +175,63 @@ parseData(options, customize)(data)
// }
```

Example: Filter and verify data

```js
import { filterData, validSign } from 'parse-string'

const customize = {
isPassword: value => /^(?=.*[A-Za-z])[A-Za-z0-9$@$!%*#?&]/.test(value)
}

const options = [
{
key: 'username',
type: 'string',
rules: [
{ required: true, message: '用户名不能为空' },
{ min: 4, max: 12, message: '用户名长度不能小于4或大于12(字符)' },
{ pattern: '^[a-zA-Z]{1}[a-zA-Z0-9\_\-]', message: '用户名格式错误' }
]
},
{
key: 'password',
type: 'string',
rules: [
{ required: true, message: '密码不能为空' },
{ min: 6, max: 15, message: '密码长度不能小于6或大于15(字符)' },
{ validator: 'isPassword', message: '密码格式错误' }
]
},
{
key: 'items',
type: 'string[]',
defaultValue: []
},
{
key: 'sign',
type: 'string',
md5: '${password}${username}'
}
]

const data = { username: 'thondery', password: 'a123456', items: '1001,1002,1003' }

try {
let result = filterData(options, customize)(data)
// {
// username: 'thondery',
// password: 'a123456',
// items: ['1001', '1002', '1003'],
// sign: '61a0375131b33b72b56e4e244d0b2f29'
// }
} catch (error) {
console.error(error.message)
}

validSign('${password}${username}', 'sign')({ username: 'thondery', password: 'a123456', sign: '61a0375131b33b72b56e4e244d0b2f29' })
// true or false
```

## License

Expand All @@ -169,4 +244,4 @@ this repo is released under the [MIT License](https://github.com/kenote/parse-st
[travis-image]: https://travis-ci.com/kenote/parse-string.svg?branch=main
[travis-url]: https://travis-ci.com/kenote/parse-string
[licensed-image]: https://img.shields.io/badge/license-MIT-blue.svg
[licensed-url]: https://github.com/kenote/parse-string/blob/master/LICENSE
[licensed-url]: https://github.com/kenote/parse-string/blob/main/LICENSE
144 changes: 135 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,144 @@ var __spread = (this && this.__spread) || function () {
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = void 0;
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = exports.checkLength = exports.validSign = exports.filterData = void 0;
var lodash_1 = require("lodash");
var rule_judgment_1 = require("rule-judgment");
function parseData(options, customize) {
var MD5 = require("md5.js");
function filterData(options, customize) {
return function (data) {
var e_1, _a;
var values = {};
try {
for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
var item = options_1_1.value;
var key = item.key, type = item.type, rules = item.rules, format = item.format, defaultValue = item.defaultValue, md5 = item.md5, separator = item.separator;
var value = data[key];
if (/\[\]$/.test(type) && !lodash_1.isArray(value)) {
value = toValue('string')(value || '').split(separator || /\,/);
}
if (/\[\]$/.test(type) && lodash_1.isArray(value)) {
var _b = __read(type.match(/(\S+)(\[\])$/), 2), itype = _b[1];
value = lodash_1.compact(value).map(toValue(itype));
rules && value.forEach(validateRule(rules, customize));
if (defaultValue && value.length === 0) {
value = defaultValue;
}
if (format) {
value = value.map(formatData(format, customize));
}
}
else {
value = toValue(type)(value);
rules && validateRule(rules, customize)(value);
value = value || defaultValue;
if (format) {
value = formatData(format, customize)(value);
}
if (md5) {
value = new MD5().update(lodash_1.template(md5)(values)).digest('hex');
}
}
lodash_1.set(values, key, value);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (options_1_1 && !options_1_1.done && (_a = options_1.return)) _a.call(options_1);
}
finally { if (e_1) throw e_1.error; }
}
return values;
};
}
exports.filterData = filterData;
function validSign(options, sign) {
if (sign === void 0) { sign = 'sign'; }
return function (data) {
var md5 = new MD5().update(lodash_1.template(options)(data)).digest('hex');
return data[sign] === md5;
};
}
exports.validSign = validSign;
function validateRule(rules, customize) {
return function (value) {
var e_2, _a;
try {
for (var rules_1 = __values(rules), rules_1_1 = rules_1.next(); !rules_1_1.done; rules_1_1 = rules_1.next()) {
var rule = rules_1_1.value;
var required = rule.required, message = rule.message, min = rule.min, max = rule.max, pattern = rule.pattern, validator = rule.validator;
if (required && (lodash_1.isUndefined(value) || value === '')) {
throw new Error(message);
}
if (lodash_1.isString(value)) {
if (min && checkLength(value) < min) {
throw new Error(message);
}
if (max && checkLength(value) > max) {
throw new Error(message);
}
if (pattern) {
var reg = getRegexp(pattern);
if (!reg.test(value)) {
throw new Error(message);
}
}
if (validator && lodash_1.isString(validator)) {
if (customize && Object.keys(customize).includes(validator)) {
validator = customize[validator];
}
}
if (validator && lodash_1.isFunction(validator)) {
if (!validator(value)) {
throw new Error(message);
}
}
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (rules_1_1 && !rules_1_1.done && (_a = rules_1.return)) _a.call(rules_1);
}
finally { if (e_2) throw e_2.error; }
}
};
}
function checkLength(str) {
var e_3, _a;
var size = 0;
if (lodash_1.isNull(str))
return size;
var arr = str.split('');
try {
for (var arr_1 = __values(arr), arr_1_1 = arr_1.next(); !arr_1_1.done; arr_1_1 = arr_1.next()) {
var word = arr_1_1.value;
size++;
(/[^\x00-\xff]/g.test(word)) && size++;
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) _a.call(arr_1);
}
finally { if (e_3) throw e_3.error; }
}
return size;
}
exports.checkLength = checkLength;
function parseData(options, customize) {
return function (data) {
var e_4, _a;
if (!options)
return data;
var separator = options.separator, collection = options.collection, omits = options.omits;
var list = data.split(separator);
var notResults = collection.filter(rule_judgment_1.default({ result: { $exists: false } }));
var values = list.map(function (v, i) {
var _a = collection[i] || {}, type = _a.type, format = _a.format;
var _a = notResults[i] || {}, type = _a.type, format = _a.format;
var value = formatData(format, customize)(toValue(type)(v));
return value;
});
Expand All @@ -54,12 +180,12 @@ function parseData(options, customize) {
lodash_1.set(obj, item.key, formatData(item.format, customize)(getResultValue(item.result, customize)(obj)));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (results_1_1 && !results_1_1.done && (_a = results_1.return)) _a.call(results_1);
}
finally { if (e_1) throw e_1.error; }
finally { if (e_4) throw e_4.error; }
}
return lodash_1.omit(obj, omits || []);
};
Expand Down Expand Up @@ -93,7 +219,7 @@ function parseBody(options, customize) {
exports.parseBody = parseBody;
function formatData(formats, customize) {
return function (value) {
var e_2, _a;
var e_5, _a;
formats = lodash_1.isArray(formats) ? formats : lodash_1.compact([formats]);
if (formats.length === 0)
return value;
Expand All @@ -103,12 +229,12 @@ function formatData(formats, customize) {
value = formatUtil(format, customize)(value);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (formats_1_1 && !formats_1_1.done && (_a = formats_1.return)) _a.call(formats_1);
}
finally { if (e_2) throw e_2.error; }
finally { if (e_5) throw e_5.error; }
}
return value;
};
Expand Down Expand Up @@ -224,7 +350,7 @@ function toValue(type) {
}
}
else {
if (type === 'string') {
if (type === 'string' && !lodash_1.isUndefined(value)) {
val = lodash_1.isPlainObject(value) ? JSON.stringify(value) : String(value);
}
else if (type === 'date' && lodash_1.isNumber(value)) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-string",
"version": "1.1.0",
"version": "1.2.0",
"description": "Parse the string into a Map.",
"main": "dist/index.js",
"typings": "types/index.d.ts",
Expand All @@ -12,6 +12,7 @@
"build": "rm -rf ./dist && tsc"
},
"dependencies": {
"md5.js": "^1.3.5",
"rule-judgment": "^1.1.3"
},
"devDependencies": {
Expand All @@ -33,6 +34,8 @@
"parser",
"format",
"map",
"msgbody"
"msgbody",
"filter",
"validator"
]
}
Loading

0 comments on commit 2583f6e

Please sign in to comment.