-
Notifications
You must be signed in to change notification settings - Fork 3
/
SelectionAny.ts
369 lines (340 loc) · 13.6 KB
/
SelectionAny.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { AnyConstructor, enumerable } from '../meta';
import { TreeIndex, TreeNode, TreeNodeRoot, TreeIndexRoot, nodeToString } from '../tree';
import { SelectionGedcom } from './internal';
/**
* A selection of Gedcom nodes, represented in an array-like datastructure.
*/
export class SelectionAny implements ArrayLike<TreeNode> {
/**
* The number of nodes in the selection.
* @category Base
*/
length: number;
/**
* The nodes in the selection.
* @category Base
*/
[n: number]: TreeNode; // eslint-disable-line no-undef
/**
* The common root node of the elements in this selection.
* @category Base
*/
@enumerable(false)
rootNode: TreeNodeRoot;
/**
* @param rootNode
* @param nodes
* @category Base
*/
constructor(rootNode: TreeNodeRoot, nodes: TreeNode[]) {
this.rootNode = rootNode;
this.length = nodes.length;
for (let i = 0; i < nodes.length; i++) {
this[i] = nodes[i];
}
}
/**
* Returns a constructor for this selection.
* @private
* @category Base
*/
#selfConstructor(): AnyConstructor<this> {
if (Object.getPrototypeOf !== undefined) {
return Object.getPrototypeOf(this).constructor as AnyConstructor<this>;
} else {
return (this as any).__proto__.constructor; // eslint-disable-line no-proto
}
}
/**
* Returns an array of {@link TreeNode.tag}.
* @category Base
*/
tag(): (string | null)[] {
const array = [];
for (let i = 0; i < this.length; i++) {
array.push(this[i].tag);
}
return array;
}
/**
* Returns an array of {@link TreeNode.pointer}.
* @category Base
*/
pointer(): (string | null)[] {
const array = [];
for (let i = 0; i < this.length; i++) {
array.push(this[i].pointer);
}
return array;
}
/**
* Returns an array of {@link TreeNode.value}.
* @category Base
*/
value(): (string | null)[] {
const array = [];
for (let i = 0; i < this.length; i++) {
array.push(this[i].value);
}
return array;
}
/**
* Calls {@link value} and filters out <code>null</code> values.
* @category Base
*/
valueNonNull(): string[] {
return this.value().filter((v): v is string => v !== null);
}
/**
* Wraps the value of {@link rootNode} in {@link SelectionGedcom}.
* The selection will contain exactly one node.
* @category Base
*/
root(): SelectionGedcom {
return new SelectionGedcom(this.rootNode, [this.rootNode]);
}
/**
* Query the direct children of this node.
* It is possible to efficiently filter the results by tag and pointer.
* Leaving either or both of these parameter empty will result in a wildcard behavior (not filtering).
* In most cases this method is not useful as the functionality is already implemented in the subclasses through various more precise methods.
* Returns an array of children.
* @param tag Optionally filter the results by their Gedcom tag
* @param pointer Optionally filter the result by their pointer value
* @category Base
*/
get(tag?: string | string[] | null, pointer?: string | string[] | null): SelectionAny;
/**
* Query the direct children of this node.
* It is possible to efficiently filter the results by tag and pointer.
* Leaving either or both of these parameter empty will result in a wildcard behavior (not filtering).
* In most cases this method is not useful as the functionality is already implemented in the subclasses through various more precise methods.
* Returns an array of children.
* Additionally, allows the specification of an adapter class.
* @param tag Optionally filter the results by their Gedcom tag
* @param pointer Optionally filter the result by their pointer value
* @param adapter The adapter class, see {@link as}
* @category Base
*/
get<N extends SelectionAny>(tag: string | string[] | null, pointer: string | string[] | null, adapter: AnyConstructor<N>): N;
// Implementation
get<N extends SelectionAny>(tag?: string | string[] | null, pointer?: string | string[] | null, adapter?: AnyConstructor<N>): N {
const Adapter = adapter != null ? adapter : SelectionAny as unknown as AnyConstructor<N>; // Type safety of this cast is enforced by the signature of the visible methods
const tags = tag != null ? (Array.isArray(tag) ? tag : [tag]) : null;
const pointers = pointer != null ? (Array.isArray(pointer) ? pointer : [pointer]) : null;
const selection: TreeNode[] = [];
for (let i = 0; i < this.length; i++) {
const node = this[i];
if (tags === null && pointers === null) { // No need of index
node.children.forEach(child => selection.push(child));
} else if (node._index !== undefined) { // Use index
const index = node._index as TreeIndex;
const intermediary: TreeNode[] = [];
const requiresSorting = (tags !== null && tags.length > 0) || (pointers !== null && pointers.length > 0);
if (pointers !== null) {
const rootIndex = index as TreeIndexRoot;
if (rootIndex.byTagPointer !== undefined) { // If the cast is unsafe then the selection should be empty
if (tags !== null) {
tags.forEach(t => pointers.forEach(p => {
const childIdx = rootIndex.byTagPointer[t][p];
if (childIdx !== undefined) {
intermediary.push(node.children[childIdx]);
}
}));
} else {
Object.values(rootIndex.byTagPointer).forEach(nodes => pointers.forEach(p => {
const childIdx = nodes[p];
if (childIdx !== undefined) {
intermediary.push(node.children[childIdx]);
}
}));
}
}
} else {
const tagsNonNull = tags as string[]; // Safe cast
tagsNonNull.forEach(t => {
const nodesIdx = index.byTag[t];
if (nodesIdx !== undefined) {
nodesIdx.forEach(childIdx => intermediary.push(node.children[childIdx]));
}
});
}
if (requiresSorting) {
intermediary.sort((a, b) => a.indexSource - b.indexSource);
}
intermediary.forEach(child => selection.push(child));
} else { // No index fallback
node.children.filter(child =>
(tags === null || (child.tag !== null && tags.includes(child.tag))) &&
(pointers === null || (child.pointer !== null && pointers.includes(child.pointer))),
).forEach(child => selection.push(child));
}
}
return new Adapter(this.rootNode, selection);
}
/**
* Filter nodes from the selection based on a predicate.
* @param f The filtering predicate
* @category Base
*/
filter(f: (node: TreeNode) => boolean): this {
const nodes: TreeNode[] = [];
for (let i = 0; i < this.length; i++) {
const node = this[i];
if (f(node)) {
nodes.push(node);
}
}
const Constructor = this.#selfConstructor();
return new Constructor(this.rootNode, nodes);
}
/**
* Filter lifted nodes from the selection based on a predicate.
* The argument is a selection of one node.
* @param f The filtering predicate
* @category Base
*/
filterSelect(f: (node: this) => boolean): this {
const Constructor = this.#selfConstructor();
const nodes: TreeNode[] = [];
for (let i = 0; i < this.length; i++) {
const node = this[i];
if (f(new Constructor(this.rootNode, [node]))) {
nodes.push(node);
}
}
return new Constructor(this.rootNode, nodes);
}
/**
* View this selection as a different type. This method can be used to extend functionality for non-standard Gedcom files.
* @param Adapter The class adapter
* @category Base
*/
as<N extends SelectionAny>(Adapter: AnyConstructor<N>): N {
return new Adapter(this.rootNode, this);
}
/**
* Export the selection as an array of nodes.
* The inverse operation is {@link of}.
* @category Base
*/
array(): TreeNode[] {
const array = [];
for (let i = 0; i < this.length; i++) {
array.push(this[i]);
}
return array;
}
/**
* Exports the selection as an array of selections of one element.
* @category Base
*/
arraySelect(): this[] {
const Constructor = this.#selfConstructor();
const array = [];
for (let i = 0; i < this.length; i++) {
array.push(new Constructor(this.rootNode, [this[i]]));
}
return array;
}
/**
* Returns a concatenation of two selections.
* The right hand side selection should be a subtype of the left hand side's.
* The resulting selection will be the same type as the left hand side's.
* @param other The right hand side selection
* @category Base
*/
concatenate<N extends this>(other: N): this {
const Constructor = this.#selfConstructor();
const nodes: TreeNode[] = [];
for (let i = 0; i < this.length; i++) {
nodes.push(this[i]);
}
for (let i = 0; i < other.length; i++) {
nodes.push(other[i]);
}
return new Constructor(this.rootNode, nodes);
}
/**
* Returns a concatenation of two selections.
* The right hand side selection should be a subtype of the left hand side's.
* The resulting selection will be the same type as the left hand side's, with the elements of the right hand side's first.
* @param other The right hand side selection
* @category Base
*/
concatenateLeft<N extends this>(other: N): this {
const Constructor = this.#selfConstructor();
const nodes: TreeNode[] = [];
for (let i = 0; i < other.length; i++) {
nodes.push(other[i]);
}
for (let i = 0; i < this.length; i++) {
nodes.push(this[i]);
}
return new Constructor(this.rootNode, nodes);
}
/**
* Returns a sorted selection, with respect to the comparator.
* The default comparator relies on the {@link TreeNode.indexSource} attribute.
* @param comparator The comparator
* @category Base
*/
sorted(comparator: (a: TreeNode, b: TreeNode) => number = (a, b) => a.indexSource - b.indexSource): this {
const Constructor = this.#selfConstructor();
const nodes: TreeNode[] = [];
for (let i = 0; i < this.length; i++) {
nodes.push(this[i]);
}
nodes.sort(comparator);
return new Constructor(this.rootNode, nodes);
}
/**
* Checks whether two selections are equal.
* Note that the strategy used here is reference equality, hence for this method to return <code>true</code>, the nodes must be the same references (and in the same order).
* @param other The selection to compare it against
* @category Base
*/
equals(other: SelectionAny): boolean {
if (this.length !== other.length) {
return false;
}
for (let i = 0; i < this.length; i++) {
if (this[i] !== other[i]) {
return false;
}
}
return true;
}
/**
* Returns a string representation for this selection.
* @category Base
*/
toString(): string {
return this.length > 0 ? this.array().map(nodeToString).join('\n\n') : '(empty selection)';
}
/**
* Create a selection from an array of nodes.
* It is highly recommended (but not required) for the nodes to be at the same logical level in the hierarchy.
* The inverse operation is {@link array}.
* @param previous The previous selection, required to inherit the reference to the root
* @param nodes The nodes to be included in the selection
* @category Base
*/
static of(previous: SelectionAny, nodes: TreeNode[] | TreeNode): SelectionAny;
/**
* Create a selection from an array of nodes.
* It is highly recommended (but not required) for the nodes to be at the same logical level in the hierarchy.
* The inverse operation is {@link array}.
* @param previous The previous selection, required to inherit the reference to the root
* @param nodes The nodes to be included in the selection
* @param Adapter The adapter class, see {@link as}
* @category Base
*/
static of<N extends SelectionAny>(previous: SelectionAny, nodes: TreeNode[] | TreeNode, Adapter: AnyConstructor<N>): N;
// Implementation
static of<N extends SelectionAny>(previous: SelectionAny, nodes: TreeNode[] | TreeNode, Adapter?: AnyConstructor<N>): N {
const AdapterClass = Adapter != null ? Adapter : SelectionAny as unknown as AnyConstructor<N>;
const nodesArray = Array.isArray(nodes) ? nodes : [nodes];
return new AdapterClass(previous.rootNode, nodesArray);
}
}