package.esm2022.src.utils.tree.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of router Show documentation
Show all versions of router Show documentation
Angular - the routing library
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
export class Tree {
constructor(root) {
this._root = root;
}
get root() {
return this._root.value;
}
/**
* @internal
*/
parent(t) {
const p = this.pathFromRoot(t);
return p.length > 1 ? p[p.length - 2] : null;
}
/**
* @internal
*/
children(t) {
const n = findNode(t, this._root);
return n ? n.children.map((t) => t.value) : [];
}
/**
* @internal
*/
firstChild(t) {
const n = findNode(t, this._root);
return n && n.children.length > 0 ? n.children[0].value : null;
}
/**
* @internal
*/
siblings(t) {
const p = findPath(t, this._root);
if (p.length < 2)
return [];
const c = p[p.length - 2].children.map((c) => c.value);
return c.filter((cc) => cc !== t);
}
/**
* @internal
*/
pathFromRoot(t) {
return findPath(t, this._root).map((s) => s.value);
}
}
// DFS for the node matching the value
function findNode(value, node) {
if (value === node.value)
return node;
for (const child of node.children) {
const node = findNode(value, child);
if (node)
return node;
}
return null;
}
// Return the path to the node with the given value using DFS
function findPath(value, node) {
if (value === node.value)
return [node];
for (const child of node.children) {
const path = findPath(value, child);
if (path.length) {
path.unshift(node);
return path;
}
}
return [];
}
export class TreeNode {
constructor(value, children) {
this.value = value;
this.children = children;
}
toString() {
return `TreeNode(${this.value})`;
}
}
// Return the list of T indexed by outlet name
export function nodeChildrenAsMap(node) {
const map = {};
if (node) {
node.children.forEach((child) => (map[child.value.outlet] = child));
}
return map;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJlZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL3JvdXRlci9zcmMvdXRpbHMvdHJlZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzI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