Skip to content

Commit 9eda099

Browse files
committed
create project.
1 parent 0dc64bd commit 9eda099

File tree

12 files changed

+1028
-2
lines changed

12 files changed

+1028
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ build/Release
4040
# Dependency directories
4141
node_modules/
4242
jspm_packages/
43+
package-lock.json
44+
yarn.lock
4345

4446
# TypeScript v1 declaration files
4547
typings/
@@ -80,7 +82,6 @@ typings/
8082

8183
# Nuxt.js build / generate output
8284
.nuxt
83-
dist
8485

8586
# Gatsby files
8687
.cache/

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- 8
5+
- 10
6+
- 12
7+
- 14
8+
9+
cache:
10+
directories:
11+
- node_modules
12+
13+
branches:
14+
only:
15+
- main
16+
17+
script:
18+
npm run test

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
all: install
3+
4+
clear:
5+
@rm -rf node_modules
6+
7+
publish:
8+
@rm -rf node_modules
9+
@npm set registry https://registry.npmjs.org
10+
@npm publish
11+
12+
install:
13+
@npm install
14+
15+
update:
16+
@npm update
17+
18+
reinstall:
19+
@make clear
20+
@make install

README.md

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,164 @@
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

Comments
 (0)