|
1 |
| -# parse-string |
| 1 | +# parse-string |
| 2 | + |
| 3 | +Parse the string into a Map. |
| 4 | + |
| 5 | +[![NPM Version][npm-image]][npm-url] |
| 6 | +[![NPM Downloads][downloads-image]][downloads-url] |
| 7 | +[![Build Status][travis-image]][travis-url] |
| 8 | +[![Gratipay][licensed-image]][licensed-url] |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +$ npm install parse-string |
| 14 | +# |
| 15 | +$ yarn add parse-string |
| 16 | +``` |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- parse string into Map |
| 21 | +- format data |
| 22 | + |
| 23 | +## API |
| 24 | + |
| 25 | +### toValue (type: 'string' | 'number' | 'date' | 'map' = 'string'): (value: any) => any |
| 26 | + |
| 27 | +Convert specified type value. |
| 28 | + |
| 29 | +- `type` - target type |
| 30 | +- `value` - value of need convert |
| 31 | + |
| 32 | +### formatData (formats?: ParseData.format | ParseData.format[], customize?: Record<string, Function>): (value: any) => any |
| 33 | + |
| 34 | +Format Data. |
| 35 | + |
| 36 | +- `formats` - formatting options |
| 37 | +- `customize` - map of custom function |
| 38 | +- `value` - value of need format |
| 39 | + |
| 40 | +### parseData (options: ParseData.options, customize?: Record<string, Function>): (data: string) => Record<string, any> |
| 41 | + |
| 42 | +Parse the string to the specified result。 |
| 43 | + |
| 44 | +- `options` - parsing options |
| 45 | +- `customize` - map of custom function |
| 46 | +- `data` - data of need parse |
| 47 | + |
| 48 | +## Usages |
| 49 | + |
| 50 | +Example: |
| 51 | + |
| 52 | +```js |
| 53 | +import { parseData } from 'parse-string' |
| 54 | + |
| 55 | +const customize = { |
| 56 | + add: (a, b) => a + b |
| 57 | +} |
| 58 | + |
| 59 | +const options = { |
| 60 | + separator: /\;/, |
| 61 | + collection: [ |
| 62 | + { |
| 63 | + key: 'date', |
| 64 | + type: 'string', |
| 65 | + format: [ |
| 66 | + { |
| 67 | + type: 'string', |
| 68 | + regexp: /^(\d{4})(\d{2})(\d{2})$/, |
| 69 | + substr: '$1-$2-$3' |
| 70 | + }, |
| 71 | + { |
| 72 | + type: 'date' |
| 73 | + } |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + key: 'amount', |
| 78 | + type: 'number' |
| 79 | + }, |
| 80 | + { |
| 81 | + key: 'user', |
| 82 | + type: 'map' |
| 83 | + }, |
| 84 | + { |
| 85 | + key: 'username', |
| 86 | + result: { |
| 87 | + defaultValue: '$__user' |
| 88 | + }, |
| 89 | + format: { |
| 90 | + type: 'map', |
| 91 | + maps: 'username' |
| 92 | + } |
| 93 | + }, |
| 94 | + { |
| 95 | + key: 'group', |
| 96 | + result: { |
| 97 | + defaultValue: '$__user' |
| 98 | + }, |
| 99 | + format: { |
| 100 | + type: 'map', |
| 101 | + maps: 'group.name' |
| 102 | + } |
| 103 | + }, |
| 104 | + { |
| 105 | + key: 'level', |
| 106 | + result: { |
| 107 | + defaultValue: '$__user' |
| 108 | + }, |
| 109 | + format: { |
| 110 | + type: 'map', |
| 111 | + maps: 'group.level' |
| 112 | + } |
| 113 | + }, |
| 114 | + { |
| 115 | + key: 'money1', |
| 116 | + result: { |
| 117 | + defaultValue: '$__amount', |
| 118 | + formula: { |
| 119 | + exec: (a, b) => a + b, |
| 120 | + opts: [ '$__amount', '$__level' ] |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + { |
| 125 | + key: 'money2', |
| 126 | + result: { |
| 127 | + defaultValue: '$__amount', |
| 128 | + formula: { |
| 129 | + exec: 'add', |
| 130 | + opts: [ '$__amount', '$__level' ] |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + ], |
| 135 | + omits: [ 'user' ] |
| 136 | +} |
| 137 | + |
| 138 | +const data = '20201027;39554;{username:\'thondery\',group:{name:\'管理员\',level:9999}}' |
| 139 | + |
| 140 | +parseData(options, customize)(data) |
| 141 | +// { |
| 142 | +// date: 2020-10-27T00:00:00.000Z, |
| 143 | +// amount: 39554, |
| 144 | +// username: 'thondery', |
| 145 | +// group: '管理员', |
| 146 | +// level: 9999, |
| 147 | +// money1: 49553, |
| 148 | +// money2: 49553 |
| 149 | +// } |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | +## License |
| 154 | + |
| 155 | +this repo is released under the [MIT License](https://github.com/kenote/parse-string/blob/main/LICENSE). |
| 156 | + |
| 157 | +[npm-image]: https://img.shields.io/npm/v/parse-string.svg |
| 158 | +[npm-url]: https://www.npmjs.com/package/parse-string |
| 159 | +[downloads-image]: https://img.shields.io/npm/dm/parse-string.svg |
| 160 | +[downloads-url]: https://www.npmjs.com/package/parse-string |
| 161 | +[travis-image]: https://travis-ci.com/kenote/parse-string.svg?branch=main |
| 162 | +[travis-url]: https://travis-ci.com/kenote/parse-string |
| 163 | +[licensed-image]: https://img.shields.io/badge/license-MIT-blue.svg |
| 164 | +[licensed-url]: https://github.com/kenote/parse-string/blob/master/LICENSE |
0 commit comments