@quanxiaoxiao/datav 0.3.2 → 0.5.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.
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 +53 -0
  37. package/dist/validateExpressSchema.js.map +1 -0
  38. package/package.json +50 -10
  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 +62 -0
  56. package/.editorconfig +0 -13
  57. package/eslint.config.mjs +0 -40
  58. package/src/checkout.mjs +0 -134
  59. package/src/checkout.test.mjs +0 -143
  60. package/src/index.mjs +0 -7
  61. package/src/select/check.mjs +0 -63
  62. package/src/select/check.test.mjs +0 -75
  63. package/src/select/index.mjs +0 -116
  64. package/src/select/index.test.mjs +0 -1120
@@ -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,75 +0,0 @@
1
- import assert from 'node:assert';
2
- import test from 'node:test';
3
- import check from './check.mjs';
4
-
5
- test('select > check', () => {
6
- assert.throws(() => {
7
- check([]);
8
- });
9
- assert.throws(() => {
10
- check('number');
11
- });
12
- assert.throws(() => {
13
- check('integer');
14
- });
15
- assert.throws(() => {
16
- check({});
17
- });
18
- assert.throws(() => {
19
- check({
20
- type: 'object',
21
- properties: [],
22
- });
23
- });
24
- assert.throws(() => {
25
- check({
26
- type: 'xxx',
27
- });
28
- });
29
- assert.throws(() => {
30
- check({
31
- type: 'json',
32
- });
33
- });
34
- assert.throws(() => {
35
- check({
36
- type: 'array',
37
- properties: [],
38
- });
39
- });
40
- assert.throws(() => {
41
- check({
42
- type: 'array',
43
- properties: ['dataKey', []],
44
- });
45
- });
46
- check({
47
- type: 'string',
48
- });
49
- check({
50
- type: 'string',
51
- properties: ['dataKey'],
52
- });
53
- check({
54
- type: 'string',
55
- properties: ['dataKey', []],
56
- });
57
- check({
58
- type: 'string',
59
- properties: ['dataKey', {}],
60
- });
61
- check({
62
- type: 'object',
63
- properties: {
64
- },
65
- });
66
- check({
67
- type: 'array',
68
- properties: {
69
- },
70
- });
71
- check({
72
- type: 'array',
73
- properties: ['dataKey', {}],
74
- });
75
- });
@@ -1,116 +0,0 @@
1
- /* eslint no-use-before-define: 0 */
2
- import _ from 'lodash';
3
- import { getValueOfPathname } from '@quanxiaoxiao/utils';
4
- import check from './check.mjs';
5
- import checkout from '../checkout.mjs';
6
-
7
- function walkWithObject(properties) {
8
- const keys = Object.keys(properties);
9
- const list = [];
10
- for (let i = 0; i < keys.length; i++) {
11
- const dataKey = keys[i];
12
- const express = properties[dataKey];
13
- const handler = {
14
- dataKey,
15
- express,
16
- fn: select(express),
17
- };
18
- list.push(handler);
19
- }
20
- return (d, _root) => {
21
- const root = _root == null ? d : _root;
22
- return list.reduce((acc, cur) => {
23
- if (Array.isArray(cur.express)) {
24
- return {
25
- ...acc,
26
- [cur.dataKey]: cur.fn(d, root),
27
- };
28
- }
29
- return {
30
- ...acc,
31
- [cur.dataKey]: cur.fn(d == null ? d : d[cur.dataKey], root),
32
- };
33
- }, {});
34
- };
35
- }
36
-
37
- function select(express) {
38
- if (Array.isArray(express)) {
39
- const [pathname] = express;
40
- if (typeof pathname !== 'string'
41
- || !_.isPlainObject(express[1])
42
- ) {
43
- throw new Error(`\`${JSON.stringify(express)}\` express invalid`);
44
- }
45
- const walk = select(express[1]);
46
- return (obj, _root) => {
47
- const root = _root == null ? obj : _root;
48
- if (pathname.startsWith('$')) {
49
- return walk(getValueOfPathname(pathname.slice(1))(root), root);
50
- }
51
- return walk(getValueOfPathname(pathname)(obj), root);
52
- };
53
- }
54
- check(express);
55
- if (['string', 'number', 'boolean', 'integer'].includes(express.type)) {
56
- return (v, _root) => {
57
- const root = _root == null ? v : _root;
58
- let value = v;
59
- if (express.resolve) {
60
- value = express.resolve(value, root);
61
- }
62
- return checkout(value, express.type);
63
- };
64
- }
65
- if (express.resolve) {
66
- console.warn('data type `array` or `object` unspport resolve');
67
- }
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
- }
77
- return walkWithObject(express.properties);
78
- }
79
- if (Array.isArray(express.properties)) {
80
- const walk = select(express.properties[1]);
81
- return (arr, _root) => {
82
- const root = _root == null ? arr : _root;
83
- const [pathname] = express.properties;
84
- if (!Array.isArray(arr)) {
85
- if (pathname.startsWith('$')) {
86
- const ret = walk(getValueOfPathname(pathname.slice(1))(root), root);
87
- if (ret == null) {
88
- return [];
89
- }
90
- return [ret];
91
- }
92
- return [];
93
- }
94
- return arr.map((d) => {
95
- if (pathname === '' || pathname === '.') {
96
- return walk(d, root);
97
- }
98
- return walk(getValueOfPathname(pathname)(d), root);
99
- });
100
- };
101
- }
102
- const walk = walkWithObject(express.properties);
103
- return (arr, _root) => {
104
- const root = _root == null ? arr : _root;
105
- if (!Array.isArray(arr)) {
106
- if (_.isEmpty(express.properties)) {
107
- return [];
108
- }
109
- const ret = walk(arr, root);
110
- return [ret];
111
- }
112
- return arr.map((d) => walk(d, root));
113
- };
114
- }
115
-
116
- export default select;