@based/schema 1.1.2 → 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 +32 -11
- package/dist/set/fields/array.js.map +1 -1
- package/package.json +1 -1
- package/src/set/fields/array.ts +45 -12
- package/test/walker.ts +1 -1
package/dist/set/fields/array.js
CHANGED
|
@@ -3,22 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.array = void 0;
|
|
4
4
|
const utils_1 = require("@saulx/utils");
|
|
5
5
|
const error_1 = require("../../error");
|
|
6
|
-
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) => {
|
|
7
16
|
const fromValue = Array.isArray(value) ? value : [value];
|
|
8
17
|
const q = [];
|
|
9
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
|
|
10
22
|
for (let i = 0; i < fromValue.length; i++) {
|
|
11
23
|
q.push(args.parse({
|
|
12
|
-
|
|
24
|
+
key: i + idx,
|
|
13
25
|
value: fromValue[i],
|
|
14
26
|
fieldSchema: args.fieldSchema.values,
|
|
15
|
-
collect: (
|
|
16
|
-
|
|
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
|
+
}
|
|
17
35
|
},
|
|
18
36
|
}));
|
|
19
37
|
}
|
|
20
38
|
await Promise.all(q);
|
|
21
|
-
return arr;
|
|
39
|
+
return { arr, collected };
|
|
22
40
|
};
|
|
23
41
|
const operations = {
|
|
24
42
|
$insert: async (args, value) => {
|
|
@@ -26,16 +44,19 @@ const operations = {
|
|
|
26
44
|
args.error(error_1.ParseError.incorrectFormat);
|
|
27
45
|
return;
|
|
28
46
|
}
|
|
29
|
-
|
|
30
|
-
|
|
47
|
+
const { collected, arr } = await parseArray(args, value.$insert.$value, value.$insert.$idx);
|
|
48
|
+
value.$insert.$value = arr;
|
|
49
|
+
collectOperation(args, collected, value);
|
|
31
50
|
},
|
|
32
51
|
$push: async (args, value) => {
|
|
33
|
-
|
|
34
|
-
|
|
52
|
+
const { collected, arr } = await parseArray(args, value.$push);
|
|
53
|
+
value.$push = arr;
|
|
54
|
+
collectOperation(args, collected, value);
|
|
35
55
|
},
|
|
36
56
|
$unshift: async (args, value) => {
|
|
37
|
-
|
|
38
|
-
|
|
57
|
+
const { collected, arr } = await parseArray(args, value.$unshift);
|
|
58
|
+
value.$unshift = arr;
|
|
59
|
+
collectOperation(args, collected, value);
|
|
39
60
|
},
|
|
40
61
|
$remove: async (args, value) => {
|
|
41
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,wCAAwC;AACxC,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,CAAC,CAAA;IACnB,IAAI,SA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AAM,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
|
@@ -3,27 +3,53 @@ import { ParseError } from '../../error'
|
|
|
3
3
|
import { FieldParser, ArgsClass } from '../../walker'
|
|
4
4
|
import { BasedSetTarget } from '../../types'
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const collectOperation = (
|
|
7
7
|
args: ArgsClass<BasedSetTarget, 'array'>,
|
|
8
|
+
collected: any[],
|
|
8
9
|
value: any
|
|
9
|
-
)
|
|
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[] }> => {
|
|
10
25
|
const fromValue = Array.isArray(value) ? value : [value]
|
|
11
26
|
const q: Promise<any>[] = []
|
|
12
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
|
|
13
33
|
for (let i = 0; i < fromValue.length; i++) {
|
|
14
34
|
q.push(
|
|
15
35
|
args.parse({
|
|
16
|
-
|
|
36
|
+
key: i + idx,
|
|
17
37
|
value: fromValue[i],
|
|
18
38
|
fieldSchema: args.fieldSchema.values,
|
|
19
|
-
collect: (
|
|
20
|
-
|
|
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
|
+
}
|
|
21
47
|
},
|
|
22
48
|
})
|
|
23
49
|
)
|
|
24
50
|
}
|
|
25
51
|
await Promise.all(q)
|
|
26
|
-
return arr
|
|
52
|
+
return { arr, collected }
|
|
27
53
|
}
|
|
28
54
|
|
|
29
55
|
const operations: {
|
|
@@ -37,16 +63,23 @@ const operations: {
|
|
|
37
63
|
args.error(ParseError.incorrectFormat)
|
|
38
64
|
return
|
|
39
65
|
}
|
|
40
|
-
|
|
41
|
-
|
|
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)
|
|
42
73
|
},
|
|
43
74
|
$push: async (args, value) => {
|
|
44
|
-
|
|
45
|
-
|
|
75
|
+
const { collected, arr } = await parseArray(args, value.$push)
|
|
76
|
+
value.$push = arr
|
|
77
|
+
collectOperation(args, collected, value)
|
|
46
78
|
},
|
|
47
79
|
$unshift: async (args, value) => {
|
|
48
|
-
|
|
49
|
-
|
|
80
|
+
const { collected, arr } = await parseArray(args, value.$unshift)
|
|
81
|
+
value.$unshift = arr
|
|
82
|
+
collectOperation(args, collected, value)
|
|
50
83
|
},
|
|
51
84
|
$remove: async (args, value) => {
|
|
52
85
|
if (typeof value.$remove.$idx !== 'number') {
|