From 013f9a2bbc8c07f0bf576700409f56d2a92833c9 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 24 May 2016 13:46:50 -0700 Subject: [PATCH] feat: add tree.siblings --- modules/@angular/router/src/tree.ts | 8 ++++++++ modules/@angular/router/test/tree.spec.ts | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/modules/@angular/router/src/tree.ts b/modules/@angular/router/src/tree.ts index f70040115f..54aaa16c3d 100644 --- a/modules/@angular/router/src/tree.ts +++ b/modules/@angular/router/src/tree.ts @@ -21,6 +21,14 @@ export class Tree { return n && n.children.length > 0 ? n.children[0].value : null; } + siblings(t: T): 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); + } + pathFromRoot(t: T): T[] { return findPath(t, this._root, []).map(s => s.value); } contains(tree: Tree): boolean { return contains(this._root, tree._root); } diff --git a/modules/@angular/router/test/tree.spec.ts b/modules/@angular/router/test/tree.spec.ts index 048a117db8..5d832558df 100644 --- a/modules/@angular/router/test/tree.spec.ts +++ b/modules/@angular/router/test/tree.spec.ts @@ -24,6 +24,12 @@ describe('tree', () => { expect(t.firstChild(2)).toEqual(null); }); + it("should return the siblings of a node", () => { + const t = new Tree(new TreeNode(1, [new TreeNode(2, []), new TreeNode(3, [])])); + expect(t.siblings(2)).toEqual([3]); + expect(t.siblings(1)).toEqual([]); + }); + it("should return the path to the root", () => { const t = new Tree(new TreeNode(1, [new TreeNode(2, [])])); expect(t.pathFromRoot(2)).toEqual([1, 2]);