@quanxiaoxiao/datav 0.4.0 → 0.5.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.
Files changed (64) hide show
  1. package/README.md +680 -165
  2. package/dist/createArrayAccessor.d.ts +2 -0
  3. package/dist/createArrayAccessor.d.ts.map +1 -0
  4. package/dist/createArrayAccessor.js +38 -0
  5. package/dist/createArrayAccessor.js.map +1 -0
  6. package/dist/createDataAccessor.d.ts +2 -0
  7. package/dist/createDataAccessor.d.ts.map +1 -0
  8. package/dist/createDataAccessor.js +23 -0
  9. package/dist/createDataAccessor.js.map +1 -0
  10. package/dist/createDataTransformer.d.ts +14 -0
  11. package/dist/createDataTransformer.d.ts.map +1 -0
  12. package/dist/createDataTransformer.js +124 -0
  13. package/dist/createDataTransformer.js.map +1 -0
  14. package/dist/createPathAccessor.d.ts +2 -0
  15. package/dist/createPathAccessor.d.ts.map +1 -0
  16. package/dist/createPathAccessor.js +38 -0
  17. package/dist/createPathAccessor.js.map +1 -0
  18. package/dist/index.d.ts +5 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +5 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/parseDotPath.d.ts +2 -0
  23. package/dist/parseDotPath.d.ts.map +1 -0
  24. package/dist/parseDotPath.js +14 -0
  25. package/dist/parseDotPath.js.map +1 -0
  26. package/dist/parseValueByType.d.ts +11 -0
  27. package/dist/parseValueByType.d.ts.map +1 -0
  28. package/dist/parseValueByType.js +122 -0
  29. package/dist/parseValueByType.js.map +1 -0
  30. package/dist/utils.d.ts +3 -0
  31. package/dist/utils.d.ts.map +1 -0
  32. package/dist/utils.js +22 -0
  33. package/dist/utils.js.map +1 -0
  34. package/dist/validateExpressSchema.d.ts +7 -0
  35. package/dist/validateExpressSchema.d.ts.map +1 -0
  36. package/dist/validateExpressSchema.js +50 -0
  37. package/dist/validateExpressSchema.js.map +1 -0
  38. package/package.json +46 -8
  39. package/src/createArrayAccessor.test.ts +181 -0
  40. package/src/createArrayAccessor.ts +48 -0
  41. package/src/createDataAccessor.test.ts +220 -0
  42. package/src/createDataAccessor.ts +26 -0
  43. package/src/createDataTransformer.test.ts +847 -0
  44. package/src/createDataTransformer.ts +173 -0
  45. package/src/createPathAccessor.test.ts +217 -0
  46. package/src/createPathAccessor.ts +45 -0
  47. package/src/index.ts +11 -0
  48. package/src/parseDotPath.test.ts +132 -0
  49. package/src/parseDotPath.ts +13 -0
  50. package/src/parseValueByType.test.ts +342 -0
  51. package/src/parseValueByType.ts +165 -0
  52. package/src/utils.test.ts +85 -0
  53. package/src/utils.ts +22 -0
  54. package/src/validateExpressSchema.test.ts +295 -0
  55. package/src/validateExpressSchema.ts +59 -0
  56. package/.editorconfig +0 -13
  57. package/eslint.config.mjs +0 -89
  58. package/src/checkout.mjs +0 -131
  59. package/src/checkout.test.mjs +0 -144
  60. package/src/index.mjs +0 -7
  61. package/src/select/check.mjs +0 -63
  62. package/src/select/check.test.mjs +0 -76
  63. package/src/select/index.mjs +0 -117
  64. package/src/select/index.test.mjs +0 -1145
@@ -1,144 +0,0 @@
1
- import assert from 'node:assert';
2
- import test from 'node:test';
3
-
4
- import checkout from './checkout.mjs';
5
-
6
- test('checkout invalid data type', () => {
7
- assert.throws(() => {
8
- checkout('aaa', 'bbb');
9
- });
10
- assert.throws(() => {
11
- checkout('aaa');
12
- });
13
- });
14
-
15
- test('checkout with data value null', () => {
16
- assert.deepEqual(checkout(null, 'array'), []);
17
- assert.equal(checkout(null, 'object'), null);
18
- assert.equal(checkout(null, 'string'), null);
19
- assert.equal(checkout(null, 'number'), null);
20
- assert.equal(checkout(null, 'integer'), null);
21
- assert.equal(checkout(null, 'boolean'), null);
22
- assert.equal(checkout(null, 'json'), null);
23
- });
24
-
25
- test('checkout with string', () => {
26
- assert.equal(checkout(1, 'string'), '1');
27
- assert.equal(checkout(null, 'string'), null);
28
- assert.equal(checkout(true, 'string'), 'true');
29
- assert.equal(checkout(false, 'string'), 'false');
30
- assert.equal(checkout(' 1', 'string'), ' 1');
31
- assert.equal(checkout([1, 2, 3], 'string'), '1,2,3');
32
- assert.equal(checkout({ name: 'cqq' }, 'string'), '[object Object]');
33
- assert.equal(checkout({
34
- name: 'quan',
35
- toString: () => 'cqq',
36
- }, 'string'), 'cqq');
37
- });
38
-
39
- test('checkout with integer', () => {
40
- assert.equal(checkout(null, 'integer'), null);
41
- assert.equal(checkout('', 'integer'), null);
42
- assert.equal(checkout(true, 'integer'), null);
43
- assert.equal(checkout(false, 'integer'), null);
44
- assert.equal(checkout([], 'integer'), null);
45
- assert.equal(checkout({}, 'integer'), null);
46
- assert.equal(checkout('aaa', 'integer'), null);
47
- assert.equal(checkout('1', 'integer'), 1);
48
- assert.equal(checkout('01', 'integer'), null);
49
- assert.equal(checkout(' 1', 'integer'), null);
50
- assert.equal(checkout('1.1', 'integer'), 1);
51
- assert.equal(checkout('-3.1', 'integer'), -3);
52
- assert.equal(checkout(3.1, 'integer'), 3);
53
- assert.equal(checkout(1, 'integer'), 1);
54
- assert(Number.isNaN(checkout(NaN, 'integer')));
55
- });
56
-
57
- test('checkout with number', () => {
58
- assert.equal(checkout('1', 'number'), 1);
59
- assert.equal(checkout('01', 'number'), null);
60
- assert.equal(checkout(true, 'number'), null);
61
- assert.equal(checkout(false, 'number'), null);
62
- assert.equal(checkout([], 'number'), null);
63
- assert.equal(checkout({}, 'number'), null);
64
- assert.equal(checkout('', 'number'), null);
65
- assert.equal(checkout('a', 'number'), null);
66
- assert.equal(checkout('1a', 'number'), null);
67
- assert.equal(checkout('0', 'number'), 0);
68
- assert.equal(checkout('-0', 'number'), null);
69
- assert.equal(checkout('-1', 'number'), -1);
70
- assert.equal(checkout('-1.5', 'number'), -1.5);
71
- assert.equal(checkout('-2.5', 'number'), -2.5);
72
- assert.equal(checkout('2.5', 'number'), 2.5);
73
- assert.equal(checkout('2.5a', 'number'), null);
74
- assert.equal(checkout('2.5.', 'number'), null);
75
- assert.equal(checkout('2.5.8', 'number'), null);
76
- assert.equal(checkout(1, 'number'), 1);
77
- assert(Number.isNaN(checkout(NaN, 'number')));
78
- });
79
-
80
- test('checkout with boolean', () => {
81
- assert.equal(checkout('', 'boolean'), null);
82
- assert.equal(checkout('false', 'boolean'), false);
83
- assert.equal(checkout(' false', 'boolean'), null);
84
- assert.equal(checkout('false ', 'boolean'), null);
85
- assert.equal(checkout('true', 'boolean'), true);
86
- assert.equal(checkout(' true', 'boolean'), null);
87
- assert.equal(checkout('true ', 'boolean'), null);
88
- assert.equal(checkout(true, 'boolean'), true);
89
- assert.equal(checkout(false, 'boolean'), false);
90
- assert.equal(checkout(1, 'boolean'), null);
91
- assert.equal(checkout({}, 'boolean'), null);
92
- assert.equal(checkout([], 'boolean'), null);
93
- assert.equal(checkout('aaa', 'boolean'), null);
94
- });
95
-
96
- test('checkout with json', () => {
97
- assert.equal(checkout('1', 'json'), 1);
98
- assert.equal(checkout(' 1', 'json'), 1);
99
- assert.equal(checkout('"1"', 'json'), '1');
100
- assert.equal(checkout('\'1\'', 'json'), null);
101
- assert.equal(checkout('null', 'json'), null);
102
- assert.equal(checkout('aa', 'json'), null);
103
- assert.deepEqual(checkout('{}', 'json'), {});
104
- assert.deepEqual(checkout('{fail}', 'json'), null);
105
- assert.deepEqual(checkout('{"name":"cqq"}', 'json'), { name: 'cqq' });
106
- assert.deepEqual(checkout('[]', 'json'), []);
107
- assert.deepEqual(checkout([], 'json'), []);
108
- assert.deepEqual(checkout({}, 'json'), {});
109
- assert.deepEqual(checkout(2, 'json'), null);
110
- });
111
-
112
- test('checkout with array', () => {
113
- assert.deepEqual(checkout(null, 'array'), []);
114
- assert.deepEqual(checkout('[]', 'array'), []);
115
- assert.deepEqual(checkout('[xxx]', 'array'), []);
116
- assert.deepEqual(checkout([], 'array'), []);
117
- assert.deepEqual(checkout([1, 2, 3], 'array'), [1, 2, 3]);
118
- assert.deepEqual(checkout(1, 'array'), []);
119
- assert.deepEqual(checkout({}, 'array'), []);
120
- assert.deepEqual(checkout('1', 'array'), []);
121
- assert.deepEqual(checkout('{}', 'array'), []);
122
- assert.deepEqual(checkout(['12345'], 'array'), ['12345']);
123
- assert.deepEqual(checkout(true, 'array'), []);
124
- assert.deepEqual(checkout(false, 'array'), []);
125
- assert.deepEqual(checkout([{ name: 'cqq' }], 'array'), [{ name: 'cqq' }]);
126
- assert.deepEqual(checkout(JSON.stringify([{ name: 'cqq' }]), 'array'), [{ name: 'cqq' }]);
127
- });
128
-
129
- test('checkout with object', () => {
130
- assert.equal(checkout(null, 'object'), null);
131
- assert.equal(checkout(1, 'object'), null);
132
- assert.equal(checkout('aa', 'object'), null);
133
- assert.equal(checkout('1', 'object'), null);
134
- assert.equal(checkout(JSON.stringify('aa'), 'object'), null);
135
- assert.equal(checkout(true, 'object'), null);
136
- assert.equal(checkout('true', 'object'), null);
137
- assert.equal(checkout('false', 'object'), null);
138
- assert.equal(checkout(false, 'object'), null);
139
- assert.equal(checkout([], 'object'), null);
140
- assert.equal(checkout(JSON.stringify([]), 'object'), null);
141
- assert.deepEqual(checkout({ name: 'cqq' }, 'object'), { name: 'cqq' });
142
- assert.deepEqual(checkout(JSON.stringify({ name: 'cqq' }), 'object'), { name: 'cqq' });
143
- assert.deepEqual(checkout('{fail}', 'object'), null);
144
- });
package/src/index.mjs DELETED
@@ -1,7 +0,0 @@
1
- import checkout from './checkout.mjs';
2
- import select from './select/index.mjs';
3
-
4
- export {
5
- checkout,
6
- select,
7
- };
@@ -1,63 +0,0 @@
1
- import Ajv from 'ajv';
2
-
3
- const ajv = new Ajv();
4
-
5
- const validate = ajv.compile({
6
- type: 'object',
7
- anyOf: [
8
- {
9
- properties: {
10
- type: {
11
- enum: ['object'],
12
- },
13
- properties: {
14
- type: 'object',
15
- },
16
- },
17
- required: ['type', 'properties'],
18
- },
19
- {
20
- properties: {
21
- type: {
22
- enum: ['array'],
23
- },
24
- properties: {
25
- anyOf: [
26
- {
27
- type: 'object',
28
- },
29
- {
30
- type: 'array',
31
- items: [
32
- {
33
- type: 'string',
34
- },
35
- {
36
- type: 'object',
37
- },
38
- ],
39
- additionalItems: false,
40
- minItems: 2,
41
- maxItems: 2,
42
- },
43
- ],
44
- },
45
- },
46
- required: ['type', 'properties'],
47
- },
48
- {
49
- properties: {
50
- type: {
51
- enum: ['string', 'number', 'boolean', 'integer'],
52
- },
53
- },
54
- required: ['type'],
55
- },
56
- ],
57
- });
58
-
59
- export default (express) => {
60
- if (!validate(express)) {
61
- throw new Error(`\`${JSON.stringify(express)}\` ${JSON.stringify(validate.errors)}`);
62
- }
63
- };
@@ -1,76 +0,0 @@
1
- import assert from 'node:assert';
2
- import test from 'node:test';
3
-
4
- import check from './check.mjs';
5
-
6
- test('select > check', () => {
7
- assert.throws(() => {
8
- check([]);
9
- });
10
- assert.throws(() => {
11
- check('number');
12
- });
13
- assert.throws(() => {
14
- check('integer');
15
- });
16
- assert.throws(() => {
17
- check({});
18
- });
19
- assert.throws(() => {
20
- check({
21
- type: 'object',
22
- properties: [],
23
- });
24
- });
25
- assert.throws(() => {
26
- check({
27
- type: 'xxx',
28
- });
29
- });
30
- assert.throws(() => {
31
- check({
32
- type: 'json',
33
- });
34
- });
35
- assert.throws(() => {
36
- check({
37
- type: 'array',
38
- properties: [],
39
- });
40
- });
41
- assert.throws(() => {
42
- check({
43
- type: 'array',
44
- properties: ['dataKey', []],
45
- });
46
- });
47
- check({
48
- type: 'string',
49
- });
50
- check({
51
- type: 'string',
52
- properties: ['dataKey'],
53
- });
54
- check({
55
- type: 'string',
56
- properties: ['dataKey', []],
57
- });
58
- check({
59
- type: 'string',
60
- properties: ['dataKey', {}],
61
- });
62
- check({
63
- type: 'object',
64
- properties: {
65
- },
66
- });
67
- check({
68
- type: 'array',
69
- properties: {
70
- },
71
- });
72
- check({
73
- type: 'array',
74
- properties: ['dataKey', {}],
75
- });
76
- });
@@ -1,117 +0,0 @@
1
- /* eslint no-use-before-define: 0 */
2
- import { getValueOfPathname } from '@quanxiaoxiao/utils';
3
- import _ from 'lodash';
4
-
5
- import checkout from '../checkout.mjs';
6
- import check from './check.mjs';
7
-
8
- function walkWithObject(properties) {
9
- const keys = Object.keys(properties);
10
- const list = [];
11
- for (let i = 0; i < keys.length; i++) {
12
- const dataKey = keys[i];
13
- const express = properties[dataKey];
14
- const handler = {
15
- dataKey,
16
- express,
17
- fn: select(express),
18
- };
19
- list.push(handler);
20
- }
21
- return (d, _root) => {
22
- const root = _root == null ? d : _root;
23
- return list.reduce((acc, cur) => {
24
- if (Array.isArray(cur.express)) {
25
- return {
26
- ...acc,
27
- [cur.dataKey]: cur.fn(d, root),
28
- };
29
- }
30
- return {
31
- ...acc,
32
- [cur.dataKey]: cur.fn(d == null ? d : d[cur.dataKey], root),
33
- };
34
- }, {});
35
- };
36
- }
37
-
38
- function select(express) {
39
- if (Array.isArray(express)) {
40
- const [pathname] = express;
41
- if (typeof pathname !== 'string'
42
- || !_.isPlainObject(express[1])
43
- ) {
44
- throw new Error(`\`${JSON.stringify(express)}\` express invalid`);
45
- }
46
- const walk = select(express[1]);
47
- return (obj, _root) => {
48
- const root = _root == null ? obj : _root;
49
- if (pathname.startsWith('$')) {
50
- return walk(getValueOfPathname(pathname.slice(1))(root), root);
51
- }
52
- return walk(getValueOfPathname(pathname)(obj), root);
53
- };
54
- }
55
- check(express);
56
- if (['string', 'number', 'boolean', 'integer'].includes(express.type)) {
57
- return (v, _root) => {
58
- const root = _root == null ? v : _root;
59
- let value = v;
60
- if (express.resolve) {
61
- value = express.resolve(value, root);
62
- }
63
- return checkout(value, express.type);
64
- };
65
- }
66
- if (express.resolve) {
67
- console.warn('data type `array` or `object` unspport resolve');
68
- }
69
- if (express.type === 'object') {
70
- if (_.isEmpty(express.properties)) {
71
- return (v) => {
72
- if (!_.isPlainObject(v)) {
73
- return {};
74
- }
75
- return v;
76
- };
77
- }
78
- return walkWithObject(express.properties);
79
- }
80
- if (Array.isArray(express.properties)) {
81
- const walk = select(express.properties[1]);
82
- return (arr, _root) => {
83
- const root = _root == null ? arr : _root;
84
- const [pathname] = express.properties;
85
- if (!Array.isArray(arr)) {
86
- if (pathname.startsWith('$')) {
87
- const ret = walk(getValueOfPathname(pathname.slice(1))(root), root);
88
- if (ret == null) {
89
- return [];
90
- }
91
- return [ret];
92
- }
93
- return [];
94
- }
95
- return arr.map((d) => {
96
- if (pathname === '' || pathname === '.') {
97
- return walk(d, root);
98
- }
99
- return walk(getValueOfPathname(pathname)(d), root);
100
- });
101
- };
102
- }
103
- const walk = walkWithObject(express.properties);
104
- return (arr, _root) => {
105
- const root = _root == null ? arr : _root;
106
- if (!Array.isArray(arr)) {
107
- if (_.isEmpty(express.properties)) {
108
- return [];
109
- }
110
- const ret = walk(arr, root);
111
- return [ret];
112
- }
113
- return arr.map((d) => walk(d, root));
114
- };
115
- }
116
-
117
- export default select;