@quanxiaoxiao/datav 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -0
- package/package.json +1 -1
- package/src/getValueOfPathname.mjs +1 -3
- package/src/select/index.test.mjs +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
类似mongo,aggregate的$project,借助表达式,可以自由地选择性地保留或过滤输入数据中的某些字段,并可以按需对数据进行转换处理。
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```shell
|
|
6
|
+
npm install @quanxiaoxiao/datav
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Quick Start
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
import { select } from '@quanxiaoxiao/datav';
|
|
13
|
+
|
|
14
|
+
select({ type: 'number' })('33.3'); // 33.3
|
|
15
|
+
select({ type: 'integer' })('33.3'); // 33
|
|
16
|
+
select({ type: 'boolean' })('true'); // true
|
|
17
|
+
select({ type: 'boolean' })('false'); // false
|
|
18
|
+
|
|
19
|
+
select({
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: {
|
|
22
|
+
name: {
|
|
23
|
+
type: 'string',
|
|
24
|
+
},
|
|
25
|
+
age: {
|
|
26
|
+
type: 'integer',
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
})({ name: 'quan', age: '22.2', foo: 'bar' }); // { name: "quan", age: 22 }
|
|
30
|
+
|
|
31
|
+
select(['age', { type: 'integer' }])({ age: '33.3' }); // 33
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Express
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"type": "object",
|
|
39
|
+
"anyOf": [
|
|
40
|
+
{
|
|
41
|
+
"properties": {
|
|
42
|
+
"type": {
|
|
43
|
+
"enum": [
|
|
44
|
+
"object"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"properties": {
|
|
48
|
+
"type": "object"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"required": [
|
|
52
|
+
"type",
|
|
53
|
+
"properties"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"properties": {
|
|
58
|
+
"type": {
|
|
59
|
+
"enum": [
|
|
60
|
+
"array"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"properties": {
|
|
64
|
+
"anyOf": [
|
|
65
|
+
{
|
|
66
|
+
"type": "object"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"type": "array",
|
|
70
|
+
"items": [
|
|
71
|
+
{
|
|
72
|
+
"type": "string"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"type": "object"
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
"additionalItems": false,
|
|
79
|
+
"minItems": 2,
|
|
80
|
+
"maxItems": 2
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"required": [
|
|
86
|
+
"type",
|
|
87
|
+
"properties"
|
|
88
|
+
]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"properties": {
|
|
92
|
+
"type": {
|
|
93
|
+
"enum": [
|
|
94
|
+
"string",
|
|
95
|
+
"number",
|
|
96
|
+
"boolean",
|
|
97
|
+
"integer"
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"required": [
|
|
102
|
+
"type"
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
当输入为数组时,数组的第一个元素表示字段路径
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
select(['sub.age', { type: 'integer' }])({
|
|
113
|
+
name: 'quan',
|
|
114
|
+
sub: { age: 33.3 },
|
|
115
|
+
}); // 33
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### With resolve
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
select({ type: 'integer', resolve: (v) => v + 1})(88); // 89
|
|
122
|
+
|
|
123
|
+
select({
|
|
124
|
+
type: 'object',
|
|
125
|
+
properties: {
|
|
126
|
+
name: {
|
|
127
|
+
type: 'string',
|
|
128
|
+
resolve: (a, b) => `${a}_${b.aa}`,
|
|
129
|
+
},
|
|
130
|
+
age: {
|
|
131
|
+
type: 'integer',
|
|
132
|
+
resolve: (a) => a + 1,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
})({
|
|
136
|
+
name: 'quan',
|
|
137
|
+
aa: 'xx',
|
|
138
|
+
age: 33,
|
|
139
|
+
}); // { "name": "quan_xx", "age": 34 }
|
|
140
|
+
```
|
package/package.json
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
|
|
3
1
|
const walk = (obj, nameList) => {
|
|
4
2
|
const [dataKey, ...other] = nameList;
|
|
5
3
|
if (Array.isArray(obj)) {
|
|
@@ -30,7 +28,7 @@ export default (obj, pathname) => {
|
|
|
30
28
|
if (typeof pathname !== 'string' || (/\.$/.test(pathname) && pathname !== '.')) {
|
|
31
29
|
throw new Error('pathname invalid');
|
|
32
30
|
}
|
|
33
|
-
if (!
|
|
31
|
+
if (!obj) {
|
|
34
32
|
return null;
|
|
35
33
|
}
|
|
36
34
|
let str = pathname;
|
|
@@ -20,7 +20,26 @@ test('select > index', () => {
|
|
|
20
20
|
assert.equal(select({ type: 'number' })(1.1), 1.1);
|
|
21
21
|
assert.equal(select({ type: 'integer' })('1.1'), 1);
|
|
22
22
|
assert.equal(select({ type: 'boolean' })('true'), true);
|
|
23
|
+
assert.equal(select({ type: 'boolean' })('false'), false);
|
|
23
24
|
assert.equal(select({ type: 'boolean' })('true1'), null);
|
|
25
|
+
assert.equal(select({ type: 'number' })('33.3'), 33.3);
|
|
26
|
+
assert.equal(select({ type: 'integer' })('33.3'), 33);
|
|
27
|
+
assert.deepEqual(
|
|
28
|
+
select({
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
name: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
},
|
|
34
|
+
age: {
|
|
35
|
+
type: 'integer',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
})({ name: 'quan', age: '22.2', foo: 'bar' }),
|
|
39
|
+
{ name: 'quan', age: 22 },
|
|
40
|
+
);
|
|
41
|
+
assert.equal(select(['age', { type: 'integer' }])({ age: '33.3' }), 33);
|
|
42
|
+
assert.equal(select(['sub.age', { type: 'integer' }])({ name: 'quan', sub: { age: 33.3 } }), 33);
|
|
24
43
|
});
|
|
25
44
|
|
|
26
45
|
test('select > index, pathname', () => {
|