Skip to content

Commit e520bfb

Browse files
committed
The result of getPath method should include up to this
1 parent c2f2e5a commit e520bfb

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ console.log(num3Node.getParentNode());
141141

142142
console.log(num3Node.getPath("nodeKey"));
143143
/* output
144-
root/node1
144+
root/node1/node3
145145
*/
146146

147147
console.log(
@@ -250,7 +250,7 @@ root: {
250250
}
251251

252252
// current node is level2
253-
node.getPath('key') // -> level0Node/level1Node
253+
level2Node.getPath('key') // -> level0Node/level1Node/level2Node
254254
```
255255

256256
#### `Node#getTree`

src/node.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ export class Node<T extends Data> {
137137

138138
getPath(key: keyof T): string {
139139
const ancestors = this.getAncestorNodes();
140-
return ancestors.map((ancestor) => ancestor.data[key]).join("/");
140+
return `${ancestors.map((ancestor) => ancestor.data[key]).join("/")}/${
141+
this.data[key]
142+
}`;
141143
}
142144

143145
getTree(): TreeType<T> {

test/tree.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, beforeEach, afterEach, expect, test } from "bun:test";
2-
import { TreeFactory, TreeType, treeMap } from "../src";
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+
import { TreeFactory, type TreeType, treeMap } from "../src";
33

44
type Data = {
55
nodeKey: string;
@@ -229,7 +229,7 @@ describe("Treejs", () => {
229229
const path = node?.getPath("nodeKey");
230230

231231
expect(path).not.toBeUndefined();
232-
expect(path).toBe("root/child1/child2");
232+
expect(path).toBe("root/child1/child2/child3");
233233
});
234234

235235
test("Two trees can be generated at the same time", () => {

0 commit comments

Comments
 (0)