@lionweb/json-utils 0.9.0-beta.2 → 0.9.0-beta.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.
|
@@ -11,6 +11,11 @@ export declare class LionWebJsonChunkWrapper {
|
|
|
11
11
|
* @protected
|
|
12
12
|
*/
|
|
13
13
|
protected nodesIdMap: Map<LionWebId, LionWebJsonNode>;
|
|
14
|
+
/**
|
|
15
|
+
* Create from a collection of nodes
|
|
16
|
+
* @param nodes
|
|
17
|
+
*/
|
|
18
|
+
static fromNodesArray(nodes: LionWebJsonNode[]): LionWebJsonChunkWrapper;
|
|
14
19
|
/**
|
|
15
20
|
* Create a wrapper with `chunk` as its chunk
|
|
16
21
|
* @param chunk
|
|
@@ -27,11 +32,25 @@ export declare class LionWebJsonChunkWrapper {
|
|
|
27
32
|
* @param reference
|
|
28
33
|
*/
|
|
29
34
|
getReferredNodes(reference: LionWebJsonReference | undefined): LionWebJsonNode[];
|
|
35
|
+
setProperty(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: string): void;
|
|
36
|
+
addChild(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: LionWebJsonNode): void;
|
|
37
|
+
removeChild(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: LionWebJsonNode): void;
|
|
30
38
|
/**
|
|
31
|
-
* Return
|
|
39
|
+
* Return an array with _all_ child nodes of `node`
|
|
40
|
+
* @param nodeId
|
|
41
|
+
*/
|
|
42
|
+
getAllChildNodes(node: LionWebJsonNode): LionWebJsonNode[];
|
|
43
|
+
/**
|
|
44
|
+
* Return an array with all child nodes of `node` for the containment `containment`.
|
|
45
|
+
* @param nodeId
|
|
46
|
+
*/
|
|
47
|
+
getContainmentNodes(node: LionWebJsonNode, containment: LionWebJsonMetaPointer): LionWebJsonNode[];
|
|
48
|
+
/**
|
|
49
|
+
* Return the nodes inside `containment` as a list of actual nodes (LionWebJsonNode[])
|
|
32
50
|
* @param reference
|
|
33
51
|
*/
|
|
34
52
|
getChildrenAsNodes(containment: LionWebJsonContainment | undefined): LionWebJsonNode[];
|
|
53
|
+
getSubtreeWithDepth(nodeId: LionWebId, depthLimit: number): LionWebJsonNode[];
|
|
35
54
|
asString(): string;
|
|
36
55
|
private recursiveToString;
|
|
37
56
|
private indent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebJsonChunkWrapper.d.ts","sourceRoot":"","sources":["../src/LionWebJsonChunkWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,SAAS,EACT,gBAAgB,EAChB,sBAAsB,EACtB,sBAAsB,EACtB,eAAe,EACf,oBAAoB,EACvB,MAAM,eAAe,CAAA;AAKtB;;;;GAIG;AACH,qBAAa,uBAAuB;IAChC,SAAS,EAAE,gBAAgB,CAAA;IAC3B;;;OAGG;IACH,SAAS,CAAC,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,eAAe,CAAC,CAAwC;IAE7F;;;OAGG;gBACS,KAAK,EAAE,gBAAgB;IAKnC;;OAEG;IACH,cAAc,CAAC,KAAK,EAAE,gBAAgB;IAMtC,OAAO,CAAC,EAAE,EAAE,SAAS,GAAG,eAAe,GAAG,SAAS;IAInD,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,eAAe,EAAE;IAIzE;;;OAGG;IACH,gBAAgB,CAAC,SAAS,EAAE,oBAAoB,GAAG,SAAS;IAe5D;;;OAGG;IACH,kBAAkB,CAAC,WAAW,EAAE,sBAAsB,GAAG,SAAS;IAclE,QAAQ,IAAI,MAAM;IAUlB,OAAO,CAAC,iBAAiB;IAkCzB,OAAO,CAAC,MAAM;CAGjB"}
|
|
1
|
+
{"version":3,"file":"LionWebJsonChunkWrapper.d.ts","sourceRoot":"","sources":["../src/LionWebJsonChunkWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,SAAS,EACT,gBAAgB,EAChB,sBAAsB,EACtB,sBAAsB,EACtB,eAAe,EACf,oBAAoB,EACvB,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
|
|
@@ -8,6 +8,17 @@ import { NodeUtils } from "./NodeUtils.js";
|
|
|
8
8
|
* NB BE aware that with `pushChunk(...)` the original chunk will be changed.
|
|
9
9
|
*/
|
|
10
10
|
export class LionWebJsonChunkWrapper {
|
|
11
|
+
/**
|
|
12
|
+
* Create from a collection of nodes
|
|
13
|
+
* @param nodes
|
|
14
|
+
*/
|
|
15
|
+
static fromNodesArray(nodes) {
|
|
16
|
+
return new LionWebJsonChunkWrapper({
|
|
17
|
+
languages: [],
|
|
18
|
+
serializationFormatVersion: "2023.1",
|
|
19
|
+
nodes: nodes
|
|
20
|
+
});
|
|
21
|
+
}
|
|
11
22
|
/**
|
|
12
23
|
* Create a wrapper with `chunk` as its chunk
|
|
13
24
|
* @param chunk
|
|
@@ -54,8 +65,65 @@ export class LionWebJsonChunkWrapper {
|
|
|
54
65
|
});
|
|
55
66
|
return result;
|
|
56
67
|
}
|
|
68
|
+
setProperty(node, mp, value) {
|
|
69
|
+
let property = node.properties.find(p => isEqualMetaPointer(p.property, mp));
|
|
70
|
+
if (property === undefined) {
|
|
71
|
+
property = {
|
|
72
|
+
property: mp,
|
|
73
|
+
value: value
|
|
74
|
+
};
|
|
75
|
+
node.properties.push(property);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
property.value = value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
addChild(node, mp, value) {
|
|
82
|
+
let containment = node.containments.find(c => isEqualMetaPointer(c.containment, mp));
|
|
83
|
+
if (containment === undefined) {
|
|
84
|
+
containment = {
|
|
85
|
+
containment: mp,
|
|
86
|
+
children: []
|
|
87
|
+
};
|
|
88
|
+
node.containments.push(containment);
|
|
89
|
+
}
|
|
90
|
+
containment.children.push(value.id);
|
|
91
|
+
}
|
|
92
|
+
removeChild(node, mp, value) {
|
|
93
|
+
const containment = node.containments.find(c => isEqualMetaPointer(c.containment, mp));
|
|
94
|
+
if (containment === undefined) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const index = containment.children.findIndex(c => c === value.id);
|
|
98
|
+
if (index !== -1) {
|
|
99
|
+
containment.children.splice(index, 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
57
102
|
/**
|
|
58
|
-
* Return
|
|
103
|
+
* Return an array with _all_ child nodes of `node`
|
|
104
|
+
* @param nodeId
|
|
105
|
+
*/
|
|
106
|
+
getAllChildNodes(node) {
|
|
107
|
+
return node.containments.flatMap(cont => this.getContainmentNodes(node, cont.containment));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Return an array with all child nodes of `node` for the containment `containment`.
|
|
111
|
+
* @param nodeId
|
|
112
|
+
*/
|
|
113
|
+
getContainmentNodes(node, containment) {
|
|
114
|
+
const cnt = node.containments.find(c => isEqualMetaPointer(containment, c.containment));
|
|
115
|
+
if (cnt === undefined) {
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
return cnt.children.flatMap(child => {
|
|
120
|
+
const childNode = this.getNode(child);
|
|
121
|
+
return childNode === undefined ? [] : [childNode];
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Return the nodes inside `containment` as a list of actual nodes (LionWebJsonNode[])
|
|
59
127
|
* @param reference
|
|
60
128
|
*/
|
|
61
129
|
getChildrenAsNodes(containment) {
|
|
@@ -71,6 +139,23 @@ export class LionWebJsonChunkWrapper {
|
|
|
71
139
|
});
|
|
72
140
|
return result;
|
|
73
141
|
}
|
|
142
|
+
getSubtreeWithDepth(nodeId, depthLimit) {
|
|
143
|
+
const node = this.getNode(nodeId);
|
|
144
|
+
if (node === undefined) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
const result = [node];
|
|
148
|
+
let baseNodes = [node];
|
|
149
|
+
for (let depth = 0; depth < depthLimit; depth++) {
|
|
150
|
+
const children = baseNodes.flatMap(node => this.getAllChildNodes(node));
|
|
151
|
+
result.push(...children);
|
|
152
|
+
baseNodes = children;
|
|
153
|
+
if (baseNodes.length === 0) {
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
74
159
|
asString() {
|
|
75
160
|
let result = "";
|
|
76
161
|
const partitions = this.jsonChunk.nodes.filter(n => n.parent === null);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LionWebJsonChunkWrapper.js","sourceRoot":"","sources":["../src/LionWebJsonChunkWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,kBAAkB,EAOrB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C;;;;GAIG;AACH,MAAM,OAAO,uBAAuB;IAQhC;;;OAGG;IACH,YAAY,KAAuB;
|
|
1
|
+
{"version":3,"file":"LionWebJsonChunkWrapper.js","sourceRoot":"","sources":["../src/LionWebJsonChunkWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,kBAAkB,EAOrB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C;;;;GAIG;AACH,MAAM,OAAO,uBAAuB;IAQhC;;;OAGG;IACH,MAAM,CAAC,cAAc,CAAC,KAAwB;QAC1C,OAAO,IAAI,uBAAuB,CAAC;YAC/B,SAAS,EAAE,EAAE;YACb,0BAA0B,EAAE,QAAQ;YACpC,KAAK,EAAE,KAAK;SACf,CAAC,CAAA;IACN,CAAC;IACD;;;OAGG;IACH,YAAY,KAAuB;QArBnC;;;WAGG;QACO,eAAU,GAAoC,IAAI,GAAG,EAA8B,CAAA;Q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,CAAC,KAAK,GAAG,IAAI,CAAA;QAC3G,CAAC,CAAC,CAAA;QACN,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YAC1B,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,SAAS,CAAC,GAAG,GAAG,KAAK,GAAG,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;QAChH,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC7B,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,GAAG,GAAG,GAAG,IAAI,CAAA;gBAC1E,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;oBACvB,MAAM,IAAI,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAA;gBACjE,CAAC,CAAC,CAAA;YACN,CAAC;QACL,CAAC,CAAC,CAAA;QACF,OAAO,MAAM,CAAA;IACjB,CAAC;IAEO,MAAM,CAAC,KAAa;QACxB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACpC,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lionweb/json-utils",
|
|
3
|
-
"version": "0.9.0-beta.
|
|
3
|
+
"version": "0.9.0-beta.3",
|
|
4
4
|
"description": "Utilities for LionWeb JSON",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"republish-local": "npm unpublish-local && npm publish-local"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@lionweb/json": "0.9.0-beta.
|
|
34
|
-
"@lionweb/ts-utils": "0.9.0-beta.
|
|
33
|
+
"@lionweb/json": "0.9.0-beta.3",
|
|
34
|
+
"@lionweb/ts-utils": "0.9.0-beta.3"
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -24,6 +24,17 @@ export class LionWebJsonChunkWrapper {
|
|
|
24
24
|
*/
|
|
25
25
|
protected nodesIdMap: Map<LionWebId, LionWebJsonNode> = new Map<LionWebId, LionWebJsonNode>()
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Create from a collection of nodes
|
|
29
|
+
* @param nodes
|
|
30
|
+
*/
|
|
31
|
+
static fromNodesArray(nodes: LionWebJsonNode[]) {
|
|
32
|
+
return new LionWebJsonChunkWrapper({
|
|
33
|
+
languages: [],
|
|
34
|
+
serializationFormatVersion: "2023.1",
|
|
35
|
+
nodes: nodes
|
|
36
|
+
})
|
|
37
|
+
}
|
|
27
38
|
/**
|
|
28
39
|
* Create a wrapper with `chunk` as its chunk
|
|
29
40
|
* @param chunk
|
|
@@ -69,8 +80,68 @@ export class LionWebJsonChunkWrapper {
|
|
|
69
80
|
return result
|
|
70
81
|
}
|
|
71
82
|
|
|
83
|
+
setProperty(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: string): void {
|
|
84
|
+
let property = node.properties.find(p => isEqualMetaPointer(p.property, mp))
|
|
85
|
+
if (property === undefined) {
|
|
86
|
+
property = {
|
|
87
|
+
property: mp,
|
|
88
|
+
value: value
|
|
89
|
+
}
|
|
90
|
+
node.properties.push(property)
|
|
91
|
+
} else {
|
|
92
|
+
property.value = value
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
addChild(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: LionWebJsonNode): void {
|
|
97
|
+
let containment = node.containments.find(c => isEqualMetaPointer(c.containment, mp))
|
|
98
|
+
if (containment === undefined) {
|
|
99
|
+
containment = {
|
|
100
|
+
containment: mp,
|
|
101
|
+
children: []
|
|
102
|
+
}
|
|
103
|
+
node.containments.push(containment)
|
|
104
|
+
}
|
|
105
|
+
containment.children.push(value.id)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
removeChild(node: LionWebJsonNode, mp: LionWebJsonMetaPointer, value: LionWebJsonNode): void {
|
|
109
|
+
const containment = node.containments.find(c => isEqualMetaPointer(c.containment, mp))
|
|
110
|
+
if (containment === undefined) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const index = containment.children.findIndex(c => c === value.id)
|
|
114
|
+
if (index !== -1) {
|
|
115
|
+
containment.children.splice(index, 1)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
72
119
|
/**
|
|
73
|
-
* Return
|
|
120
|
+
* Return an array with _all_ child nodes of `node`
|
|
121
|
+
* @param nodeId
|
|
122
|
+
*/
|
|
123
|
+
getAllChildNodes(node: LionWebJsonNode): LionWebJsonNode[] {
|
|
124
|
+
return node.containments.flatMap(cont => this.getContainmentNodes(node, cont.containment))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Return an array with all child nodes of `node` for the containment `containment`.
|
|
129
|
+
* @param nodeId
|
|
130
|
+
*/
|
|
131
|
+
getContainmentNodes(node: LionWebJsonNode, containment: LionWebJsonMetaPointer): LionWebJsonNode[] {
|
|
132
|
+
const cnt = node.containments.find(c => isEqualMetaPointer(containment, c.containment))
|
|
133
|
+
if (cnt === undefined) {
|
|
134
|
+
return []
|
|
135
|
+
} else {
|
|
136
|
+
return cnt.children.flatMap(child => {
|
|
137
|
+
const childNode = this.getNode(child)
|
|
138
|
+
return childNode === undefined ? [] : [childNode]
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Return the nodes inside `containment` as a list of actual nodes (LionWebJsonNode[])
|
|
74
145
|
* @param reference
|
|
75
146
|
*/
|
|
76
147
|
getChildrenAsNodes(containment: LionWebJsonContainment | undefined) {
|
|
@@ -87,6 +158,24 @@ export class LionWebJsonChunkWrapper {
|
|
|
87
158
|
return result
|
|
88
159
|
}
|
|
89
160
|
|
|
161
|
+
getSubtreeWithDepth(nodeId: LionWebId, depthLimit: number): LionWebJsonNode[] {
|
|
162
|
+
const node = this.getNode(nodeId)
|
|
163
|
+
if (node === undefined) {
|
|
164
|
+
return []
|
|
165
|
+
}
|
|
166
|
+
const result: LionWebJsonNode[] = [node]
|
|
167
|
+
let baseNodes: LionWebJsonNode[] = [node]
|
|
168
|
+
for (let depth = 0; depth < depthLimit; depth++) {
|
|
169
|
+
const children = baseNodes.flatMap(node => this.getAllChildNodes(node))
|
|
170
|
+
result.push(...children)
|
|
171
|
+
baseNodes = children
|
|
172
|
+
if (baseNodes.length === 0) {
|
|
173
|
+
break
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return result
|
|
177
|
+
}
|
|
178
|
+
|
|
90
179
|
asString(): string {
|
|
91
180
|
let result = ""
|
|
92
181
|
const partitions = this.jsonChunk.nodes.filter(n => n.parent === null)
|