项目作者: kenote

项目描述 :
Parse the string into a Map.
高级语言: TypeScript
项目地址: git://github.com/kenote/parse-string.git
创建时间: 2020-10-24T02:43:51Z
项目社区:https://github.com/kenote/parse-string

开源协议:MIT License

下载


parse-string

Parse the string into a Map.

NPM Version
NPM Downloads
Build Status
Gratipay

Installation

  1. $ npm install parse-string
  2. #
  3. $ yarn add parse-string

Features

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

API

toValue (type: ‘string’ | ‘number’ | ‘date’ | ‘map’ = ‘string’): (value: any) => any

Convert specified type value.

  • type - target type
  • value - value of need convert

formatData (formats?: ParseData.format | ParseData.format[], customize?: Record): (value: any) => any

Format Data.

  • formats - formatting options
  • customize - map of custom function
  • value - value of need format

parseData (options: ParseData.options, customize?: Record): (data: string) => Record

Parse the string to the specified result.

  • options - parsing options
  • customize - map of custom function
  • data - data of need parse

parseBody (options: ParseData.parse[], customize?: Record): (msgbody: Record) => Record

Parse the msbody to the specified result.

  • options - parsing options
  • customize - map of custom function
  • msbody - msbody of need parse

filterData (options: FilterData.options[], customize?: Record): (data: Record) => Record

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) => boolean

Verify signature.

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

Usages

Example: Parse string

  1. import { parseData } from 'parse-string'
  2. const customize = {
  3. add: (a, b) => a + b
  4. }
  5. const options = {
  6. separator: /\;/,
  7. collection: [
  8. {
  9. key: 'date',
  10. type: 'string',
  11. format: [
  12. {
  13. type: 'string',
  14. regexp: /^(\d{4})(\d{2})(\d{2})$/,
  15. substr: '$1-$2-$3'
  16. },
  17. {
  18. type: 'date'
  19. }
  20. ]
  21. },
  22. {
  23. key: 'amount',
  24. type: 'number'
  25. },
  26. {
  27. key: 'user',
  28. type: 'map'
  29. },
  30. {
  31. key: 'username',
  32. result: {
  33. defaultValue: '$__user'
  34. },
  35. format: {
  36. type: 'map',
  37. maps: 'username'
  38. }
  39. },
  40. {
  41. key: 'group',
  42. result: {
  43. defaultValue: '$__user'
  44. },
  45. format: {
  46. type: 'map',
  47. maps: 'group.name'
  48. }
  49. },
  50. {
  51. key: 'level',
  52. result: {
  53. defaultValue: '$__user'
  54. },
  55. format: {
  56. type: 'map',
  57. maps: 'group.level'
  58. }
  59. },
  60. {
  61. key: 'money1',
  62. result: {
  63. defaultValue: '$__amount',
  64. formula: {
  65. exec: (a, b) => a + b,
  66. opts: [ '$__amount', '$__level' ]
  67. }
  68. }
  69. },
  70. {
  71. key: 'money2',
  72. result: {
  73. defaultValue: '$__amount',
  74. formula: {
  75. exec: 'add',
  76. opts: [ '$__amount', '$__level' ]
  77. }
  78. }
  79. }
  80. ],
  81. omits: [ 'user' ]
  82. }
  83. const data = '20201027;39554;{username:\'thondery\',group:{name:\'管理员\',level:9999}}'
  84. parseData(options, customize)(data)
  85. // {
  86. // date: 2020-10-27T00:00:00.000Z,
  87. // amount: 39554,
  88. // username: 'thondery',
  89. // group: '管理员',
  90. // level: 9999,
  91. // money1: 49553,
  92. // money2: 49553
  93. // }

Example: Filter and verify data

  1. import { filterData, validSign } from 'parse-string'
  2. const customize = {
  3. isPassword: value => /^(?=.*[A-Za-z])[A-Za-z0-9$@$!%*#?&]/.test(value)
  4. }
  5. const options = [
  6. {
  7. key: 'username',
  8. type: 'string',
  9. rules: [
  10. { required: true, message: '用户名不能为空' },
  11. { min: 4, max: 12, message: '用户名长度不能小于4或大于12(字符)' },
  12. { pattern: '^[a-zA-Z]{1}[a-zA-Z0-9\_\-]', message: '用户名格式错误' }
  13. ]
  14. },
  15. {
  16. key: 'password',
  17. type: 'string',
  18. rules: [
  19. { required: true, message: '密码不能为空' },
  20. { min: 6, max: 15, message: '密码长度不能小于6或大于15(字符)' },
  21. { validator: 'isPassword', message: '密码格式错误' }
  22. ]
  23. },
  24. {
  25. key: 'items',
  26. type: 'string[]',
  27. defaultValue: []
  28. },
  29. {
  30. key: 'sign',
  31. type: 'string',
  32. md5: '${password}${username}'
  33. }
  34. ]
  35. const data = { username: 'thondery', password: 'a123456', items: '1001,1002,1003' }
  36. try {
  37. let result = filterData(options, customize)(data)
  38. // {
  39. // username: 'thondery',
  40. // password: 'a123456',
  41. // items: ['1001', '1002', '1003'],
  42. // sign: '61a0375131b33b72b56e4e244d0b2f29'
  43. // }
  44. } catch (error) {
  45. console.error(error.message)
  46. }
  47. validSign('${password}${username}', 'sign')({ username: 'thondery', password: 'a123456', sign: '61a0375131b33b72b56e4e244d0b2f29' })
  48. // true or false

License

this repo is released under the MIT License.