@based/schema 1.1.1 → 1.1.3
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/dist/set/fields/array.js +33 -11
- package/dist/set/fields/array.js.map +1 -1
- package/package.json +1 -1
- package/src/set/fields/array.ts +46 -12
- package/test/walker.ts +23 -8
package/dist/set/fields/array.js
CHANGED
|
@@ -1,23 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.array = void 0;
|
|
4
|
+
const utils_1 = require("@saulx/utils");
|
|
4
5
|
const error_1 = require("../../error");
|
|
5
|
-
const
|
|
6
|
+
const collectOperation = (args, collected, value) => {
|
|
7
|
+
args.collect(value);
|
|
8
|
+
if (collected.length) {
|
|
9
|
+
const collect = args.root._opts.collect;
|
|
10
|
+
for (const args of collected) {
|
|
11
|
+
collect(args);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const parseArray = async (args, value, idx = 0) => {
|
|
6
16
|
const fromValue = Array.isArray(value) ? value : [value];
|
|
7
17
|
const q = [];
|
|
8
18
|
const arr = new Array(fromValue.length);
|
|
19
|
+
const collectNested = ['object', 'record', 'text'].includes(args.fieldSchema.values.type);
|
|
20
|
+
const collected = [];
|
|
21
|
+
// optmize this code path
|
|
9
22
|
for (let i = 0; i < fromValue.length; i++) {
|
|
10
23
|
q.push(args.parse({
|
|
11
|
-
key: i,
|
|
24
|
+
key: i + idx,
|
|
12
25
|
value: fromValue[i],
|
|
13
26
|
fieldSchema: args.fieldSchema.values,
|
|
14
|
-
collect: (
|
|
15
|
-
|
|
27
|
+
collect: (nArgs) => {
|
|
28
|
+
const p = nArgs.path.slice(args.path.length);
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
p[0] = p[0] - idx;
|
|
31
|
+
(0, utils_1.setByPath)(arr, p, nArgs.value);
|
|
32
|
+
if (collectNested) {
|
|
33
|
+
collected.push(nArgs);
|
|
34
|
+
}
|
|
16
35
|
},
|
|
17
36
|
}));
|
|
18
37
|
}
|
|
19
38
|
await Promise.all(q);
|
|
20
|
-
return arr;
|
|
39
|
+
return { arr, collected };
|
|
21
40
|
};
|
|
22
41
|
const operations = {
|
|
23
42
|
$insert: async (args, value) => {
|
|
@@ -25,16 +44,19 @@ const operations = {
|
|
|
25
44
|
args.error(error_1.ParseError.incorrectFormat);
|
|
26
45
|
return;
|
|
27
46
|
}
|
|
28
|
-
|
|
29
|
-
|
|
47
|
+
const { collected, arr } = await parseArray(args, value.$insert.$value, value.$insert.$idx);
|
|
48
|
+
value.$insert.$value = arr;
|
|
49
|
+
collectOperation(args, collected, value);
|
|
30
50
|
},
|
|
31
51
|
$push: async (args, value) => {
|
|
32
|
-
|
|
33
|
-
|
|
52
|
+
const { collected, arr } = await parseArray(args, value.$push);
|
|
53
|
+
value.$push = arr;
|
|
54
|
+
collectOperation(args, collected, value);
|
|
34
55
|
},
|
|
35
56
|
$unshift: async (args, value) => {
|
|
36
|
-
|
|
37
|
-
|
|
57
|
+
const { collected, arr } = await parseArray(args, value.$unshift);
|
|
58
|
+
value.$unshift = arr;
|
|
59
|
+
collectOperation(args, collected, value);
|
|
38
60
|
},
|
|
39
61
|
$remove: async (args, value) => {
|
|
40
62
|
if (typeof value.$remove.$idx !== 'number') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../../src/set/fields/array.ts"],"names":[],"mappings":";;;AAAA,uCAAwC;AAIxC,MAAM,UAAU,GAAG,KAAK,EACtB,IAAwC,EACxC,KAAU,
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../../src/set/fields/array.ts"],"names":[],"mappings":";;;AAAA,wCAAwC;AACxC,uCAAwC;AAIxC,MAAM,gBAAgB,GAAG,CACvB,IAAwC,EACxC,SAAgB,EAChB,KAAU,EACV,EAAE;IACF,IAAI,CAAC,OAAO,CAAC,KAAK,CAA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AI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAA;IACnE,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACxB,MAAM,CAAC,GAAmB,EAAE,CAAA;QAC5B,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;QAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACrC,CAAC,CAAC,IAAI,CACJ,IAAI,CAAC,KAAK,CAAC;gBACT,GAAG,EAAE,CAAC;gBACN,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;gBACpB,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM;aACrC,CAAC,CACH,CAAA;SACF;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QACpB,OAAM;KACP;IACD,IAAI,YAAY,GAAG,KAAK,CAAA;IACxB,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;QACvB,IAAI,UAAU,CAAC,GAAG,CAAC,EAAE;YACnB,IAAI,YAAY,EAAE;gBAChB,IAAI,CAAC,KAAK,CAAC,kBAAU,CAAC,4BAA4B,CAAC,CAAA;gBACnD,OAAM;aACP;YACD,MAAM,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YAClC,YAAY,GAAG,IAAI,CAAA;SACpB;aAAM;YACL,IAAI,CAAC,KAAK,CAAC,kBAAU,CAAC,iBAAiB,CAAC,CAAA;SACzC;KACF;AACH,CAAC,CAAA;AAnCY,QAAA,KAAK,SAmCjB"}
|
package/package.json
CHANGED
package/src/set/fields/array.ts
CHANGED
|
@@ -1,28 +1,55 @@
|
|
|
1
|
+
import { setByPath } from '@saulx/utils'
|
|
1
2
|
import { ParseError } from '../../error'
|
|
2
3
|
import { FieldParser, ArgsClass } from '../../walker'
|
|
3
4
|
import { BasedSetTarget } from '../../types'
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
+
const collectOperation = (
|
|
6
7
|
args: ArgsClass<BasedSetTarget, 'array'>,
|
|
8
|
+
collected: any[],
|
|
7
9
|
value: any
|
|
8
|
-
)
|
|
10
|
+
) => {
|
|
11
|
+
args.collect(value)
|
|
12
|
+
if (collected.length) {
|
|
13
|
+
const collect = args.root._opts.collect
|
|
14
|
+
for (const args of collected) {
|
|
15
|
+
collect(args)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const parseArray = async (
|
|
21
|
+
args: ArgsClass<BasedSetTarget, 'array'>,
|
|
22
|
+
value: any,
|
|
23
|
+
idx: number = 0
|
|
24
|
+
): Promise<{ collected: ArgsClass<BasedSetTarget>[]; arr: any[] }> => {
|
|
9
25
|
const fromValue = Array.isArray(value) ? value : [value]
|
|
10
26
|
const q: Promise<any>[] = []
|
|
11
27
|
const arr = new Array(fromValue.length)
|
|
28
|
+
const collectNested = ['object', 'record', 'text'].includes(
|
|
29
|
+
args.fieldSchema.values.type
|
|
30
|
+
)
|
|
31
|
+
const collected: ArgsClass<BasedSetTarget>[] = []
|
|
32
|
+
// optmize this code path
|
|
12
33
|
for (let i = 0; i < fromValue.length; i++) {
|
|
13
34
|
q.push(
|
|
14
35
|
args.parse({
|
|
15
|
-
key: i,
|
|
36
|
+
key: i + idx,
|
|
16
37
|
value: fromValue[i],
|
|
17
38
|
fieldSchema: args.fieldSchema.values,
|
|
18
|
-
collect: (
|
|
19
|
-
|
|
39
|
+
collect: (nArgs) => {
|
|
40
|
+
const p = nArgs.path.slice(args.path.length)
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
p[0] = p[0] - idx
|
|
43
|
+
setByPath(arr, p, nArgs.value)
|
|
44
|
+
if (collectNested) {
|
|
45
|
+
collected.push(nArgs)
|
|
46
|
+
}
|
|
20
47
|
},
|
|
21
48
|
})
|
|
22
49
|
)
|
|
23
50
|
}
|
|
24
51
|
await Promise.all(q)
|
|
25
|
-
return arr
|
|
52
|
+
return { arr, collected }
|
|
26
53
|
}
|
|
27
54
|
|
|
28
55
|
const operations: {
|
|
@@ -36,16 +63,23 @@ const operations: {
|
|
|
36
63
|
args.error(ParseError.incorrectFormat)
|
|
37
64
|
return
|
|
38
65
|
}
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
const { collected, arr } = await parseArray(
|
|
67
|
+
args,
|
|
68
|
+
value.$insert.$value,
|
|
69
|
+
value.$insert.$idx
|
|
70
|
+
)
|
|
71
|
+
value.$insert.$value = arr
|
|
72
|
+
collectOperation(args, collected, value)
|
|
41
73
|
},
|
|
42
74
|
$push: async (args, value) => {
|
|
43
|
-
|
|
44
|
-
|
|
75
|
+
const { collected, arr } = await parseArray(args, value.$push)
|
|
76
|
+
value.$push = arr
|
|
77
|
+
collectOperation(args, collected, value)
|
|
45
78
|
},
|
|
46
79
|
$unshift: async (args, value) => {
|
|
47
|
-
|
|
48
|
-
|
|
80
|
+
const { collected, arr } = await parseArray(args, value.$unshift)
|
|
81
|
+
value.$unshift = arr
|
|
82
|
+
collectOperation(args, collected, value)
|
|
49
83
|
},
|
|
50
84
|
$remove: async (args, value) => {
|
|
51
85
|
if (typeof value.$remove.$idx !== 'number') {
|
package/test/walker.ts
CHANGED
|
@@ -74,11 +74,24 @@ const schema: BasedSchema = {
|
|
|
74
74
|
record: {
|
|
75
75
|
type: 'record',
|
|
76
76
|
values: {
|
|
77
|
-
type: '
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
bla: {
|
|
80
|
+
type: 'array',
|
|
81
|
+
values: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
snux: {
|
|
85
|
+
type: 'object',
|
|
86
|
+
properties: {
|
|
87
|
+
x: {
|
|
88
|
+
type: 'number',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
flap: { type: 'number' },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
82
95
|
},
|
|
83
96
|
},
|
|
84
97
|
},
|
|
@@ -967,9 +980,11 @@ test('string', async (t) => {
|
|
|
967
980
|
$id: 'bl120',
|
|
968
981
|
record: {
|
|
969
982
|
blabla: {
|
|
970
|
-
|
|
971
|
-
$
|
|
972
|
-
|
|
983
|
+
bla: {
|
|
984
|
+
$insert: {
|
|
985
|
+
$value: { flap: 100 },
|
|
986
|
+
$idx: 5,
|
|
987
|
+
},
|
|
973
988
|
},
|
|
974
989
|
},
|
|
975
990
|
},
|