@quanxiaoxiao/datav 0.1.1 → 0.2.0
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 +50 -0
- package/package.json +1 -1
- package/src/getValueOfPathname.mjs +1 -3
- package/src/select/index.mjs +8 -0
- package/src/select/index.test.mjs +80 -2
package/README.md
CHANGED
|
@@ -138,3 +138,53 @@ select({
|
|
|
138
138
|
age: 33,
|
|
139
139
|
}); // { "name": "quan_xx", "age": 34 }
|
|
140
140
|
```
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
const ret = select(
|
|
144
|
+
{
|
|
145
|
+
type: 'object',
|
|
146
|
+
properties: {
|
|
147
|
+
count: {
|
|
148
|
+
type: 'integer',
|
|
149
|
+
},
|
|
150
|
+
list: {
|
|
151
|
+
type: 'array',
|
|
152
|
+
properties: {
|
|
153
|
+
token: ['.', {
|
|
154
|
+
type: 'string',
|
|
155
|
+
resolve: (d) => `${d.name}_${d.age}`,
|
|
156
|
+
}],
|
|
157
|
+
name: {
|
|
158
|
+
type: 'string',
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
)({
|
|
165
|
+
count: 20,
|
|
166
|
+
list: [
|
|
167
|
+
{
|
|
168
|
+
name: 'big',
|
|
169
|
+
age: 11,
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
name: 'bar',
|
|
173
|
+
age: 22,
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
});
|
|
177
|
+
assert.deepEqual(ret, {
|
|
178
|
+
count: 20,
|
|
179
|
+
list: [
|
|
180
|
+
{
|
|
181
|
+
name: 'big',
|
|
182
|
+
token: 'big_11',
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
name: 'bar',
|
|
186
|
+
token: 'bar_22',
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
});
|
|
190
|
+
```
|
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;
|
package/src/select/index.mjs
CHANGED
|
@@ -66,6 +66,14 @@ function select(express) {
|
|
|
66
66
|
console.warn('data type `array` or `object` unspport resolve');
|
|
67
67
|
}
|
|
68
68
|
if (express.type === 'object') {
|
|
69
|
+
if (_.isEmpty(express.properties)) {
|
|
70
|
+
return (v) => {
|
|
71
|
+
if (!_.isPlainObject(v)) {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
return v;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
69
77
|
return walkWithObject(express.properties);
|
|
70
78
|
}
|
|
71
79
|
if (Array.isArray(express.properties)) {
|
|
@@ -123,8 +123,7 @@ test('select > index, type with object', () => {
|
|
|
123
123
|
'obj',
|
|
124
124
|
{
|
|
125
125
|
type: 'object',
|
|
126
|
-
properties: {
|
|
127
|
-
},
|
|
126
|
+
properties: {},
|
|
128
127
|
},
|
|
129
128
|
])({
|
|
130
129
|
name: 'quan',
|
|
@@ -134,7 +133,36 @@ test('select > index, type with object', () => {
|
|
|
134
133
|
age: '33.3',
|
|
135
134
|
big: 'foo',
|
|
136
135
|
},
|
|
136
|
+
}), {
|
|
137
|
+
name: 'xxx',
|
|
138
|
+
age: '33.3',
|
|
139
|
+
big: 'foo',
|
|
140
|
+
});
|
|
141
|
+
assert.deepEqual(select([
|
|
142
|
+
'obj',
|
|
143
|
+
{
|
|
144
|
+
type: 'object',
|
|
145
|
+
properties: {},
|
|
146
|
+
},
|
|
147
|
+
])({
|
|
148
|
+
name: 'quan',
|
|
149
|
+
age: '22.5',
|
|
150
|
+
obj: 'aaa',
|
|
137
151
|
}), {});
|
|
152
|
+
assert.deepEqual(select(
|
|
153
|
+
{
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: {},
|
|
156
|
+
},
|
|
157
|
+
)({
|
|
158
|
+
name: 'quan',
|
|
159
|
+
age: '22.5',
|
|
160
|
+
obj: 'aaa',
|
|
161
|
+
}), {
|
|
162
|
+
name: 'quan',
|
|
163
|
+
age: '22.5',
|
|
164
|
+
obj: 'aaa',
|
|
165
|
+
});
|
|
138
166
|
assert.deepEqual(select([
|
|
139
167
|
'obj',
|
|
140
168
|
{
|
|
@@ -759,3 +787,53 @@ test('select > index, resolve', () => {
|
|
|
759
787
|
{ name: 'aaa', resolve: 'resolve' },
|
|
760
788
|
);
|
|
761
789
|
});
|
|
790
|
+
|
|
791
|
+
test('select > index, resolve pathList', () => {
|
|
792
|
+
const ret = select(
|
|
793
|
+
{
|
|
794
|
+
type: 'object',
|
|
795
|
+
properties: {
|
|
796
|
+
count: {
|
|
797
|
+
type: 'integer',
|
|
798
|
+
},
|
|
799
|
+
list: {
|
|
800
|
+
type: 'array',
|
|
801
|
+
properties: {
|
|
802
|
+
token: ['.', {
|
|
803
|
+
type: 'string',
|
|
804
|
+
resolve: (d) => `${d.name}_${d.age}`,
|
|
805
|
+
}],
|
|
806
|
+
name: {
|
|
807
|
+
type: 'string',
|
|
808
|
+
},
|
|
809
|
+
},
|
|
810
|
+
},
|
|
811
|
+
},
|
|
812
|
+
},
|
|
813
|
+
)({
|
|
814
|
+
count: 20,
|
|
815
|
+
list: [
|
|
816
|
+
{
|
|
817
|
+
name: 'big',
|
|
818
|
+
age: 11,
|
|
819
|
+
},
|
|
820
|
+
{
|
|
821
|
+
name: 'bar',
|
|
822
|
+
age: 22,
|
|
823
|
+
},
|
|
824
|
+
],
|
|
825
|
+
});
|
|
826
|
+
assert.deepEqual(ret, {
|
|
827
|
+
count: 20,
|
|
828
|
+
list: [
|
|
829
|
+
{
|
|
830
|
+
name: 'big',
|
|
831
|
+
token: 'big_11',
|
|
832
|
+
},
|
|
833
|
+
{
|
|
834
|
+
name: 'bar',
|
|
835
|
+
token: 'bar_22',
|
|
836
|
+
},
|
|
837
|
+
],
|
|
838
|
+
});
|
|
839
|
+
});
|