@quanxiaoxiao/datav 0.1.2 → 0.3.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 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
+ ```
@@ -0,0 +1,40 @@
1
+ import js from '@eslint/js';
2
+ import globals from 'globals';
3
+
4
+ export default [
5
+ {
6
+ ignores: [
7
+ '_resources/*',
8
+ 'static/*',
9
+ 'node_modules/*',
10
+ ],
11
+ },
12
+ js.configs.recommended,
13
+ {
14
+ files: [
15
+ 'src/**/*.mjs',
16
+ '_index.mjs',
17
+ 'scripts/**/*.mjs',
18
+ 'eslint.config.mjs',
19
+ ],
20
+ languageOptions: {
21
+ ecmaVersion: 2022,
22
+ sourceType: 'module',
23
+ globals: {
24
+ ...globals.node,
25
+ },
26
+ },
27
+ rules: {
28
+ 'no-console': 0,
29
+ 'max-len': 0,
30
+ 'no-continue': 0,
31
+ 'no-bitwise': 0,
32
+ 'no-mixed-operators': 0,
33
+ 'no-underscore-dangle': 0,
34
+ 'import/prefer-default-export': 0,
35
+ 'class-methods-use-this': 0,
36
+ 'no-plusplus': 0,
37
+ 'global-require': 0,
38
+ },
39
+ },
40
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quanxiaoxiao/datav",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test"
@@ -11,16 +11,9 @@
11
11
  "./package.json": "./package.json"
12
12
  },
13
13
  "devDependencies": {
14
- "@babel/eslint-parser": "^7.23.10",
15
- "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
16
- "@babel/plugin-proposal-optional-chaining": "^7.21.0",
17
- "@babel/plugin-syntax-import-attributes": "^7.23.3",
18
- "eslint": "^8.56.0",
19
- "eslint-config-airbnb": "^19.0.4",
20
- "eslint-plugin-import": "^2.29.1",
21
- "eslint-plugin-jsx-a11y": "^6.8.0",
22
- "eslint-plugin-react": "^7.33.2",
23
- "eslint-plugin-react-hooks": "^4.6.0"
14
+ "@eslint/js": "^9.3.0",
15
+ "eslint": "^9.3.0",
16
+ "globals": "^15.3.0"
24
17
  },
25
18
  "publishConfig": {
26
19
  "registry": "https://registry.npmjs.org"
@@ -29,6 +22,7 @@
29
22
  "node": ">= 20.0.0"
30
23
  },
31
24
  "dependencies": {
25
+ "@quanxiaoxiao/utils": "^0.9.0",
32
26
  "ajv": "^8.12.0",
33
27
  "lodash": "^4.17.21"
34
28
  }
package/src/checkout.mjs CHANGED
@@ -57,7 +57,7 @@ const map = {
57
57
  [DATA_TYPE_JSON]: (v) => {
58
58
  try {
59
59
  return JSON.parse(v);
60
- } catch (error) {
60
+ } catch (error) { // eslint-disable-line
61
61
  return null;
62
62
  }
63
63
  },
@@ -71,7 +71,7 @@ const map = {
71
71
  return null;
72
72
  }
73
73
  return d;
74
- } catch (error) {
74
+ } catch (error) { // eslint-disable-line
75
75
  return null;
76
76
  }
77
77
  },
@@ -82,7 +82,7 @@ const map = {
82
82
  return d;
83
83
  }
84
84
  return [];
85
- } catch (error) {
85
+ } catch (error) { // eslint-disable-line
86
86
  return [];
87
87
  }
88
88
  },
@@ -69,6 +69,9 @@ test('checkout with number', () => {
69
69
  assert.equal(checkout('-1.5', 'number'), -1.5);
70
70
  assert.equal(checkout('-2.5', 'number'), -2.5);
71
71
  assert.equal(checkout('2.5', 'number'), 2.5);
72
+ assert.equal(checkout('2.5a', 'number'), null);
73
+ assert.equal(checkout('2.5.', 'number'), null);
74
+ assert.equal(checkout('2.5.8', 'number'), null);
72
75
  assert.equal(checkout(1, 'number'), 1);
73
76
  assert(Number.isNaN(checkout(NaN, 'number')));
74
77
  });
package/src/index.mjs CHANGED
@@ -1,9 +1,7 @@
1
1
  import checkout from './checkout.mjs';
2
- import getValueOfPathname from './getValueOfPathname.mjs';
3
2
  import select from './select/index.mjs';
4
3
 
5
4
  export {
6
5
  select,
7
6
  checkout,
8
- getValueOfPathname,
9
7
  };
@@ -1,8 +1,8 @@
1
1
  /* eslint no-use-before-define: 0 */
2
2
  import _ from 'lodash';
3
+ import { getValueOfPathname } from '@quanxiaoxiao/utils';
3
4
  import check from './check.mjs';
4
5
  import checkout from '../checkout.mjs';
5
- import getValueOfPathname from '../getValueOfPathname.mjs';
6
6
 
7
7
  function walkWithObject(properties) {
8
8
  const keys = Object.keys(properties);
@@ -46,9 +46,9 @@ function select(express) {
46
46
  return (obj, _root) => {
47
47
  const root = _root == null ? obj : _root;
48
48
  if (pathname.startsWith('$')) {
49
- return walk(getValueOfPathname(root, pathname.slice(1)), root);
49
+ return walk(getValueOfPathname(pathname.slice(1))(root), root);
50
50
  }
51
- return walk(getValueOfPathname(obj, pathname), root);
51
+ return walk(getValueOfPathname(pathname)(obj), root);
52
52
  };
53
53
  }
54
54
  check(express);
@@ -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)) {
@@ -75,7 +83,7 @@ function select(express) {
75
83
  const [pathname] = express.properties;
76
84
  if (!Array.isArray(arr)) {
77
85
  if (pathname.startsWith('$')) {
78
- const ret = walk(getValueOfPathname(root, pathname.slice(1)), root);
86
+ const ret = walk(getValueOfPathname(pathname.slice(1))(root), root);
79
87
  if (ret == null) {
80
88
  return [];
81
89
  }
@@ -87,7 +95,7 @@ function select(express) {
87
95
  if (pathname === '' || pathname === '.') {
88
96
  return walk(d, root);
89
97
  }
90
- return walk(getValueOfPathname(d, pathname), root);
98
+ return walk(getValueOfPathname(pathname)(d), root);
91
99
  });
92
100
  };
93
101
  }
@@ -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
  {
@@ -243,6 +271,7 @@ test('select > index, type with object', () => {
243
271
  age: {
244
272
  type: 'number',
245
273
  },
274
+ ddd: ['obj.name', { type: 'string' }],
246
275
  sub: ['obj', {
247
276
  type: 'object',
248
277
  properties: {
@@ -267,6 +296,7 @@ test('select > index, type with object', () => {
267
296
  }), {
268
297
  name: 'quan',
269
298
  age: 22.5,
299
+ ddd: 'xxx',
270
300
  sub: {
271
301
  name: 'xxx',
272
302
  age: 33,
@@ -659,6 +689,26 @@ test('select > index, resolve', () => {
659
689
  age: 34,
660
690
  },
661
691
  );
692
+ assert.deepEqual(
693
+ select({
694
+ type: 'object',
695
+ properties: {
696
+ name: {
697
+ type: 'string',
698
+ },
699
+ age: {
700
+ type: 'integer',
701
+ resolve: () => 99,
702
+ },
703
+ },
704
+ })({
705
+ name: 'quan',
706
+ }),
707
+ {
708
+ name: 'quan',
709
+ age: 99,
710
+ },
711
+ );
662
712
  assert.deepEqual(
663
713
  select({
664
714
  type: 'object',
@@ -759,3 +809,222 @@ test('select > index, resolve', () => {
759
809
  { name: 'aaa', resolve: 'resolve' },
760
810
  );
761
811
  });
812
+
813
+ test('select > index, resolve pathList', () => {
814
+ const ret = select(
815
+ {
816
+ type: 'object',
817
+ properties: {
818
+ count: {
819
+ type: 'integer',
820
+ },
821
+ list: {
822
+ type: 'array',
823
+ properties: {
824
+ token: ['.', {
825
+ type: 'string',
826
+ resolve: (d) => `${d.name}_${d.age}`,
827
+ }],
828
+ name: {
829
+ type: 'string',
830
+ },
831
+ },
832
+ },
833
+ },
834
+ },
835
+ )({
836
+ count: 20,
837
+ list: [
838
+ {
839
+ name: 'big',
840
+ age: 11,
841
+ },
842
+ {
843
+ name: 'bar',
844
+ age: 22,
845
+ },
846
+ ],
847
+ });
848
+ assert.deepEqual(ret, {
849
+ count: 20,
850
+ list: [
851
+ {
852
+ name: 'big',
853
+ token: 'big_11',
854
+ },
855
+ {
856
+ name: 'bar',
857
+ token: 'bar_22',
858
+ },
859
+ ],
860
+ });
861
+ });
862
+
863
+ test('select > index, resolve pathList 2', { only: true }, () => {
864
+ const ret = select(
865
+ {
866
+ type: 'object',
867
+ properties: {
868
+ count: {
869
+ type: 'integer',
870
+ },
871
+ list: {
872
+ type: 'array',
873
+ properties: ['.name', {
874
+ type: 'string',
875
+ }],
876
+ },
877
+ },
878
+ },
879
+ )({
880
+ count: 20,
881
+ list: [
882
+ {
883
+ name: 'big',
884
+ age: 11,
885
+ },
886
+ {
887
+ name: 'bar',
888
+ age: 22,
889
+ },
890
+ ],
891
+ });
892
+ assert.deepEqual(ret, {
893
+ count: 20,
894
+ list: ['big', 'bar'],
895
+ });
896
+ });
897
+
898
+ test('select array array array', () => {
899
+ const array = [[['11', 22], ['33', 44]], [[1], [2]]];
900
+ const ret = select({
901
+ type: 'array',
902
+ properties: ['.', {
903
+ type: 'array',
904
+ properties: ['.', {
905
+ type: 'array',
906
+ properties: ['.', { type: 'number' }],
907
+ }],
908
+ }],
909
+ })(array);
910
+ assert.deepEqual(ret, [[[11, 22], [33, 44]], [[1], [2]]]);
911
+ });
912
+
913
+ test('select object array array array', () => {
914
+ const obj = {
915
+ name: 'xxx',
916
+ arr: [[['11', 22], ['33', 44]], [[1], [2]]],
917
+ };
918
+ const ret = select({
919
+ type: 'object',
920
+ properties: {
921
+ name: {
922
+ type: 'string',
923
+ },
924
+ arr: ['arr', {
925
+ type: 'array',
926
+ properties: ['.', {
927
+ type: 'array',
928
+ properties: ['.', {
929
+ type: 'array',
930
+ properties: ['.', { type: 'number' }],
931
+ }],
932
+ }],
933
+ }],
934
+ },
935
+ })(obj);
936
+ assert.deepEqual(ret.arr, [[[11, 22], [33, 44]], [[1], [2]]]);
937
+ });
938
+
939
+ test('select 222', () => {
940
+ const obj = {
941
+ data: {
942
+ key: 'aaaabbb',
943
+ },
944
+ };
945
+ assert.equal(select(['.data.key', { type: 'string' }])(obj), obj.data.key);
946
+ });
947
+
948
+ test('select 333', () => {
949
+ const obj = {
950
+ data: ['222', '333'],
951
+ };
952
+ assert.deepEqual(select(['.data', { type: 'array', properties: ['.', { type: 'string' }] }])(obj), obj.data);
953
+ });
954
+
955
+ test('select 444', () => {
956
+ const key = '111222';
957
+
958
+ const ret = select({
959
+ type: 'object',
960
+ properties: {
961
+ name: ['.', {
962
+ type: 'string',
963
+ }],
964
+ },
965
+ })(key);
966
+ assert.deepEqual(ret, { name: key });
967
+ });
968
+
969
+ test('select 5555', () => {
970
+ const data = {
971
+ deviceId: '101007351946',
972
+ channelId: '2',
973
+ sn: '286329',
974
+ name: null,
975
+ sumNum: 4,
976
+ count: 4,
977
+ lastTime: null,
978
+ recordList: [
979
+ {
980
+ deviceId: '101007351946',
981
+ startTime: '2024-06-25 10:09:37',
982
+ endTime: '2024-06-25 13:45:32',
983
+ },
984
+ {
985
+ deviceId: '101007351946',
986
+ startTime: '2024-06-25 13:47:16',
987
+ endTime: '2024-06-25 17:01:19',
988
+ },
989
+ {
990
+ deviceId: '101007351946',
991
+ startTime: '2024-06-25 17:01:19',
992
+ endTime: '2024-06-25 17:01:21',
993
+ },
994
+ {
995
+ deviceId: '101007351946',
996
+ startTime: '2024-06-25 17:01:21',
997
+ endTime: '2024-06-25 18:11:39',
998
+ },
999
+ ],
1000
+ };
1001
+ const ret = select(['.recordList', {
1002
+ type: 'array',
1003
+ properties: {
1004
+ dateTimeNameStart: ['.startTime', { type: 'string' }],
1005
+ dateTimeNameEnd: ['.endTime', { type: 'string' }],
1006
+ },
1007
+ }])(data);
1008
+ assert.deepEqual(
1009
+ ret,
1010
+ data.recordList.map((d) => ({
1011
+ dateTimeNameStart: d.startTime,
1012
+ dateTimeNameEnd: d.endTime,
1013
+ })),
1014
+ );
1015
+ });
1016
+
1017
+ test('select 666', () => {
1018
+ const ret = select({
1019
+ type: 'object',
1020
+ properties: {
1021
+ chl: ['.', {
1022
+ type: 'array',
1023
+ properties: ['$channel', { type: 'string' }],
1024
+ }],
1025
+ },
1026
+ })({
1027
+ channel: '1',
1028
+ });
1029
+ assert.deepEqual(ret, { chl: ['1'] });
1030
+ });
package/.babelrc DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "plugins": [
3
- "@babel/plugin-proposal-optional-chaining",
4
- "@babel/plugin-proposal-nullish-coalescing-operator",
5
- "@babel/plugin-syntax-import-attributes"
6
- ]
7
- }
package/.eslintrc DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "parser": "@babel/eslint-parser",
3
- "extends": "eslint-config-airbnb",
4
- "env": {
5
- "node": true
6
- },
7
- "rules": {
8
- "no-console": 0,
9
- "max-len": 0,
10
- "no-continue": 0,
11
- "no-bitwise": 0,
12
- "import/no-extraneous-dependencies": [
13
- "error",
14
- {
15
- "devDependencies": true,
16
- "optionalDependencies": false,
17
- "peerDependencies": false
18
- }
19
- ],
20
- "no-param-reassign": 0,
21
- "no-mixed-operators": 0,
22
- "no-underscore-dangle": 0,
23
- "import/prefer-default-export": 0,
24
- "class-methods-use-this": 0,
25
- "no-plusplus": 0,
26
- "global-require": 0
27
- },
28
- "globals": {}
29
- }
@@ -1,42 +0,0 @@
1
- const walk = (obj, nameList) => {
2
- const [dataKey, ...other] = nameList;
3
- if (Array.isArray(obj)) {
4
- const n = parseInt(dataKey, 10);
5
- if (Number.isNaN(n) || `${n}` !== dataKey) {
6
- return null;
7
- }
8
- const len = obj.length;
9
- if (n > len) {
10
- return null;
11
- }
12
- if (other.length === 0) {
13
- return obj[n];
14
- }
15
- return walk(obj[n], other);
16
- }
17
- if (!Object.hasOwnProperty.call(obj, dataKey)) {
18
- return null;
19
- }
20
- const value = obj[dataKey];
21
- if (other.length === 0) {
22
- return value;
23
- }
24
- return walk(value, other);
25
- };
26
-
27
- export default (obj, pathname) => {
28
- if (typeof pathname !== 'string' || (/\.$/.test(pathname) && pathname !== '.')) {
29
- throw new Error('pathname invalid');
30
- }
31
- if (!obj) {
32
- return null;
33
- }
34
- let str = pathname;
35
- if (pathname.startsWith('.')) {
36
- str = pathname.slice(1);
37
- }
38
- if (str === '') {
39
- return obj;
40
- }
41
- return walk(obj, str.split(/(?<!\\)\./).map((d) => d.replace(/\\\./g, '.')));
42
- };
@@ -1,29 +0,0 @@
1
- import assert from 'node:assert';
2
- import test from 'node:test';
3
- import getValueOfPathname from './getValueOfPathname.mjs';
4
-
5
- test('getValueOfPathname', () => {
6
- assert.throws(() => {
7
- getValueOfPathname({}, 11);
8
- });
9
- assert.throws(() => {
10
- getValueOfPathname({}, 'name.');
11
- });
12
- assert.deepEqual(getValueOfPathname({}, '.'), {});
13
- assert.equal(getValueOfPathname([], 'name'), null);
14
- assert.equal(getValueOfPathname(null, 'xxx'), null);
15
- assert.equal(getValueOfPathname({ name: 'quan' }, 'quan'), null);
16
- assert.equal(getValueOfPathname({ name: 'quan' }, 'name'), 'quan');
17
- assert.equal(getValueOfPathname({ name: 'quan' }, '.name'), 'quan');
18
- assert.equal(getValueOfPathname({ '.name': 'quan', name: 'cqq' }, '\\.name'), 'quan');
19
- assert.deepEqual(getValueOfPathname({ name: 'quan' }, ''), { name: 'quan' });
20
- assert.deepEqual(getValueOfPathname({ name: 'quan', obj: { foo: 'bar' } }, 'obj'), { foo: 'bar' });
21
- assert.equal(getValueOfPathname({ name: 'quan', obj: { name: 'bar' } }, 'obj.name'), 'bar');
22
- assert.equal(getValueOfPathname({ 'obj.name': 'xxx', name: 'quan', obj: { name: 'bar' } }, 'obj\\.name'), 'xxx');
23
- });
24
-
25
- test('getValueOfPathname of array', () => {
26
- assert.equal(getValueOfPathname([{ name: 'aa' }, { name: 'bb' }], '0.name'), 'aa');
27
- assert.equal(getValueOfPathname([{ name: 'aa' }, { name: 'bb' }], '5.name'), null);
28
- assert.equal(getValueOfPathname([{ name: 'aa' }, { name: 'bb' }], '5'), null);
29
- });