/
NodePathElement.ts
172 lines (160 loc) · 5.37 KB
/
NodePathElement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Hierarchies
*/
import { Node, NodeJSON } from "./Node";
/**
* Serialized [[NodePathElement]] JSON representation.
* @public
* @deprecated in 3.x. Use [[NodePathElement]].
*/
export interface NodePathElementJSON {
// eslint-disable-next-line deprecation/deprecation
node: NodeJSON;
index: number;
isMarked?: boolean;
// eslint-disable-next-line deprecation/deprecation
children: NodePathElementJSON[];
// eslint-disable-next-line deprecation/deprecation
filteringData?: NodePathFilteringDataJSON;
}
/**
* Describes a single step in the nodes path.
* @public
*/
export interface NodePathElement {
/** Node instance */
node: Node;
/** Node index */
index: number;
/** Is this element part of the marked path */
isMarked?: boolean;
/** Child path elements */
children: NodePathElement[];
/** Additional filtering-related information */
filteringData?: NodePathFilteringData;
}
/** @public */
export namespace NodePathElement {
/**
* Serialize given [[NodePathElement]] to JSON
* @deprecated in 3.x. Use [[NodePathElement]].
*/
// eslint-disable-next-line deprecation/deprecation
export function toJSON(npe: NodePathElement): NodePathElementJSON {
// eslint-disable-next-line deprecation/deprecation
const result: NodePathElementJSON = {
// eslint-disable-next-line deprecation/deprecation
node: Node.toJSON(npe.node),
index: npe.index,
// eslint-disable-next-line deprecation/deprecation
children: npe.children.map(NodePathElement.toJSON),
};
if (undefined !== npe.isMarked)
result.isMarked = npe.isMarked;
if (undefined !== npe.filteringData) {
// eslint-disable-next-line deprecation/deprecation
result.filteringData = NodePathFilteringData.toJSON(npe.filteringData);
}
return result;
}
/**
* Deserialize [[NodePathElement]] from JSON
* @deprecated in 3.x. Use [[NodePathElement]].
*/
// eslint-disable-next-line deprecation/deprecation
export function fromJSON(json: NodePathElementJSON | string): NodePathElement {
if (typeof json === "string")
return JSON.parse(json, reviver);
const result: NodePathElement = {
index: json.index,
// eslint-disable-next-line deprecation/deprecation
node: Node.fromJSON(json.node),
children: listFromJSON(json.children),
};
if (undefined !== json.isMarked)
result.isMarked = json.isMarked;
if (undefined !== json.filteringData) {
// eslint-disable-next-line deprecation/deprecation
result.filteringData = NodePathFilteringData.fromJSON(json.filteringData);
}
return result;
}
/**
* Reviver function that can be used as a second argument for
* `JSON.parse` method when parsing [[NodePathElement]] objects.
* @internal
*/
export function reviver(key: string, value: any): any {
// eslint-disable-next-line deprecation/deprecation
return key === "" ? fromJSON(value) : value;
}
/**
* Deserialize [[NodePathElement]] list from JSON
* @param json JSON or JSON serialized to string to deserialize from
* @returns Deserialized [[NodePathElement]] list
* @internal
*/
// eslint-disable-next-line deprecation/deprecation
export function listFromJSON(json: NodePathElementJSON[] | string): NodePathElement[] {
if (typeof json === "string")
return JSON.parse(json, listReviver);
// eslint-disable-next-line deprecation/deprecation
return json.map((m) => fromJSON(m));
}
/**
* Reviver function that can be used as a second argument for
* `JSON.parse` method when parsing [[NodePathElement]][] objects.
* @internal
*/
export function listReviver(key: string, value: any): any {
return key === "" ? listFromJSON(value) : value;
}
}
/**
* Serialized [[NodePathFilteringData]] JSON representation.
* @public
* @deprecated in 3.x. Use [[NodePathFilteringData]].
*/
export interface NodePathFilteringDataJSON {
occurances: number;
childrenOccurances: number;
}
/**
* Data related to node hierarchy filtering
* @public
*/
export interface NodePathFilteringData {
/** Number of filter matches in the current element */
matchesCount: number;
/** Number of filter matches in the current element's children (recursively) */
childMatchesCount: number;
}
/** @public */
export namespace NodePathFilteringData {
/**
* Serialize given [[NodePathFilteringData]] to JSON
* @deprecated in 3.x. Use [[NodePathFilteringData]].
*/
// eslint-disable-next-line deprecation/deprecation
export function toJSON(npfd: NodePathFilteringData): NodePathFilteringDataJSON {
return {
occurances: npfd.matchesCount,
childrenOccurances: npfd.childMatchesCount,
};
}
/**
* Deserialize [[NodePathFilteringData]] from JSON
* @deprecated in 3.x. Use [[NodePathFilteringData]].
*/
// eslint-disable-next-line deprecation/deprecation
export function fromJSON(json: NodePathFilteringDataJSON): NodePathFilteringData {
return {
matchesCount: json.occurances,
childMatchesCount: json.childrenOccurances,
};
}
}