@based/schema 1.1.2 → 1.1.4
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 +51 -12
- package/test/walker.ts +43 -37
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.$value ?? 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.$value ?? 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,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
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,29 @@ 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(
|
|
76
|
+
args,
|
|
77
|
+
value.$push.$value ?? value.$push
|
|
78
|
+
)
|
|
79
|
+
value.$push = arr
|
|
80
|
+
collectOperation(args, collected, value)
|
|
46
81
|
},
|
|
47
82
|
$unshift: async (args, value) => {
|
|
48
|
-
|
|
49
|
-
|
|
83
|
+
const { collected, arr } = await parseArray(
|
|
84
|
+
args,
|
|
85
|
+
value.$unshift.$value ?? value.$unshift
|
|
86
|
+
)
|
|
87
|
+
value.$unshift = arr
|
|
88
|
+
collectOperation(args, collected, value)
|
|
50
89
|
},
|
|
51
90
|
$remove: async (args, value) => {
|
|
52
91
|
if (typeof value.$remove.$idx !== 'number') {
|
package/test/walker.ts
CHANGED
|
@@ -946,26 +946,15 @@ test('string', async (t) => {
|
|
|
946
946
|
)
|
|
947
947
|
|
|
948
948
|
console.info('---- doink 40 array ------')
|
|
949
|
-
r = await setWalker(
|
|
950
|
-
|
|
951
|
-
{
|
|
952
|
-
$
|
|
953
|
-
|
|
954
|
-
$
|
|
955
|
-
$idx: 10,
|
|
956
|
-
$value: 1212,
|
|
957
|
-
},
|
|
949
|
+
r = await setWalker(schema, {
|
|
950
|
+
$id: 'bl120',
|
|
951
|
+
intarray: {
|
|
952
|
+
$insert: {
|
|
953
|
+
$idx: 10,
|
|
954
|
+
$value: 1212,
|
|
958
955
|
},
|
|
959
956
|
},
|
|
960
|
-
|
|
961
|
-
console.info('GO ASYNC', args.path, args.value, type)
|
|
962
|
-
if (args.value.type === 'thing') {
|
|
963
|
-
return 'ti' + Math.floor(Math.random() * 10000).toString(16)
|
|
964
|
-
} else {
|
|
965
|
-
return 'bl1221'
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
)
|
|
957
|
+
})
|
|
969
958
|
|
|
970
959
|
console.dir(r.errors)
|
|
971
960
|
console.dir(
|
|
@@ -974,31 +963,48 @@ test('string', async (t) => {
|
|
|
974
963
|
)
|
|
975
964
|
|
|
976
965
|
console.info('---- doink 41 record + array ------')
|
|
977
|
-
r = await setWalker(
|
|
978
|
-
|
|
979
|
-
{
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
$
|
|
985
|
-
$value: { flap: 100 },
|
|
986
|
-
$idx: 0,
|
|
987
|
-
},
|
|
966
|
+
r = await setWalker(schema, {
|
|
967
|
+
$id: 'bl120',
|
|
968
|
+
record: {
|
|
969
|
+
blabla: {
|
|
970
|
+
bla: {
|
|
971
|
+
$insert: {
|
|
972
|
+
$value: { flap: 100 },
|
|
973
|
+
$idx: 5,
|
|
988
974
|
},
|
|
989
975
|
},
|
|
990
976
|
},
|
|
991
977
|
},
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
978
|
+
})
|
|
979
|
+
|
|
980
|
+
console.dir(r.errors)
|
|
981
|
+
console.dir(
|
|
982
|
+
r.collected.map((v) => ({ path: v.path, value: v.value })),
|
|
983
|
+
{ depth: 10 }
|
|
984
|
+
)
|
|
985
|
+
|
|
986
|
+
console.info('---- doink 41 int unshift + array ------')
|
|
987
|
+
r = await setWalker(schema, {
|
|
988
|
+
$id: 'bl120',
|
|
989
|
+
intarray: {
|
|
990
|
+
$unshift: [-10, -20, -30],
|
|
991
|
+
},
|
|
992
|
+
})
|
|
993
|
+
|
|
994
|
+
console.dir(r.errors)
|
|
995
|
+
console.dir(
|
|
996
|
+
r.collected.map((v) => ({ path: v.path, value: v.value })),
|
|
997
|
+
{ depth: 10 }
|
|
1000
998
|
)
|
|
1001
999
|
|
|
1000
|
+
console.info('---- doink 42 int unshift + array ------')
|
|
1001
|
+
r = await setWalker(schema, {
|
|
1002
|
+
$id: 'bl120',
|
|
1003
|
+
intarray: {
|
|
1004
|
+
$unshift: { $value: [-10, -20, -30] },
|
|
1005
|
+
},
|
|
1006
|
+
})
|
|
1007
|
+
|
|
1002
1008
|
console.dir(r.errors)
|
|
1003
1009
|
console.dir(
|
|
1004
1010
|
r.collected.map((v) => ({ path: v.path, value: v.value })),
|