@quanxiaoxiao/datav 0.2.0 → 0.3.1

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.
@@ -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.2.0",
3
+ "version": "0.3.1",
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);
@@ -83,7 +83,7 @@ function select(express) {
83
83
  const [pathname] = express.properties;
84
84
  if (!Array.isArray(arr)) {
85
85
  if (pathname.startsWith('$')) {
86
- const ret = walk(getValueOfPathname(root, pathname.slice(1)), root);
86
+ const ret = walk(getValueOfPathname(pathname.slice(1))(root), root);
87
87
  if (ret == null) {
88
88
  return [];
89
89
  }
@@ -95,7 +95,7 @@ function select(express) {
95
95
  if (pathname === '' || pathname === '.') {
96
96
  return walk(d, root);
97
97
  }
98
- return walk(getValueOfPathname(d, pathname), root);
98
+ return walk(getValueOfPathname(pathname)(d), root);
99
99
  });
100
100
  };
101
101
  }
@@ -271,6 +271,7 @@ test('select > index, type with object', () => {
271
271
  age: {
272
272
  type: 'number',
273
273
  },
274
+ ddd: ['obj.name', { type: 'string' }],
274
275
  sub: ['obj', {
275
276
  type: 'object',
276
277
  properties: {
@@ -295,6 +296,7 @@ test('select > index, type with object', () => {
295
296
  }), {
296
297
  name: 'quan',
297
298
  age: 22.5,
299
+ ddd: 'xxx',
298
300
  sub: {
299
301
  name: 'xxx',
300
302
  age: 33,
@@ -687,6 +689,26 @@ test('select > index, resolve', () => {
687
689
  age: 34,
688
690
  },
689
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
+ );
690
712
  assert.deepEqual(
691
713
  select({
692
714
  type: 'object',
@@ -837,3 +859,262 @@ test('select > index, resolve pathList', () => {
837
859
  ],
838
860
  });
839
861
  });
862
+
863
+ test('select > index, resolve pathList 2', () => {
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
+ });
1031
+
1032
+ test('select 777', () => {
1033
+ const ret = select({
1034
+ type: 'object',
1035
+ properties: {
1036
+ key: {
1037
+ type: 'string',
1038
+ },
1039
+ params: {
1040
+ type: 'array',
1041
+ properties: {
1042
+ task: ['$taskId', { type: 'number' }],
1043
+ date: ['$dateName', { type: 'string' }],
1044
+ },
1045
+ },
1046
+ },
1047
+ })({
1048
+ key: '123',
1049
+ taskId: '999',
1050
+ dateName: '2024-06-06',
1051
+ });
1052
+ assert.deepEqual(ret, {
1053
+ key: '123',
1054
+ params: [
1055
+ {
1056
+ task: '999',
1057
+ date: '2024-06-06',
1058
+ },
1059
+ ],
1060
+ });
1061
+ });
1062
+
1063
+ test('select 888', () => {
1064
+ const ret = select({
1065
+ type: 'object',
1066
+ properties: {
1067
+ dir: ['.data.0.dir', { type: 'string' }],
1068
+ name: ['.data.0.name', { type: 'string' }],
1069
+ },
1070
+ })({
1071
+ data: [
1072
+ {
1073
+ dir: 'QzpcVmlkZW9ccXExMjM0XDIwMTctMDYtMTlccmVjb3JkXDE=',
1074
+ name: 'qq1234-170619-000000-002000-01p401000000.264'
1075
+ },
1076
+ {
1077
+ dir: 'QzpcVmlkZW9ccXExMjM0XDIwMTctMDYtMTlccmVjb3JkXDE=',
1078
+ name: 'qq1234-170619-000000-002000-01p401000000.mp4'
1079
+ }
1080
+ ],
1081
+ errorcode: 200
1082
+ });
1083
+ assert.deepEqual(ret, {
1084
+ dir: 'QzpcVmlkZW9ccXExMjM0XDIwMTctMDYtMTlccmVjb3JkXDE=',
1085
+ name: 'qq1234-170619-000000-002000-01p401000000.264'
1086
+ });
1087
+ });
1088
+
1089
+ test('select object empty properties', () => {
1090
+ const data = {
1091
+ code: 0,
1092
+ data: {
1093
+ 'name': 'data111',
1094
+ '/aaa': {
1095
+ name: '123',
1096
+ '/ccc': {
1097
+ name: 'ccc',
1098
+ },
1099
+ },
1100
+ '/sss': {
1101
+ name: '999',
1102
+ },
1103
+ },
1104
+ };
1105
+ const ret = select({
1106
+ type: 'object',
1107
+ properties: {
1108
+ route: ['.data', {
1109
+ type: 'object',
1110
+ properties: {},
1111
+ }],
1112
+ },
1113
+ })(data);
1114
+ assert.deepEqual(
1115
+ {
1116
+ route: data.data,
1117
+ },
1118
+ ret,
1119
+ );
1120
+ });
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
- });