Skip to content

Commit c0792c3

Browse files
fix: return TreeNode for first and last child (#894)
closes #893
1 parent 2c8120b commit c0792c3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

projects/angular-tree-component/src/lib/models/tree-node.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ export class TreeNode implements ITreeNode {
120120
getFirstChild(skipHidden = false) {
121121
let children = skipHidden ? this.visibleChildren : this.children;
122122

123-
return [].concat(children || []).shift();
123+
return children != null && children.length ? children[0] : null;
124124
}
125125

126126
getLastChild(skipHidden = false) {
127127
let children = skipHidden ? this.visibleChildren : this.children;
128128

129-
return [].concat(children || []).pop();
129+
return children != null && children.length ? children[children.length - 1] : null;
130130
}
131131

132132
findNextNode(goInside = true, skipHidden = false) {

projects/angular-tree-component/src/lib/models/tree.model.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ export class TreeModel implements ITreeModel, OnDestroy {
6060
}
6161

6262
getFirstRoot(skipHidden = false) {
63-
return [].concat(skipHidden ? this.getVisibleRoots() : this.roots).shift();
63+
const root = skipHidden ? this.getVisibleRoots() : this.roots;
64+
return root != null && root.length ? root[0] : null;
6465
}
6566

6667
getLastRoot(skipHidden = false) {
67-
return [].concat(skipHidden ? this.getVisibleRoots() : this.roots).pop();
68+
const root = skipHidden ? this.getVisibleRoots() : this.roots;
69+
return root != null && root.length ? root[root.length - 1] : null;
6870
}
6971

7072
get isFocused() {

0 commit comments

Comments
 (0)