Skip to content

Commit

Permalink
feat: supports undefined parent as unsetting parent for setParent API (
Browse files Browse the repository at this point in the history
…#8)

* feat: supports undefiend parent as unsetting parent for setParent API

* chore: upgrade version num

* docs: update changelog

* fix: judgement for undefined parent id
  • Loading branch information
Yanyan-Wang authored Jun 27, 2023
1 parent e3d8f13 commit a759533
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

## Change Log

#### 2.0.2

- feat: supports undefined parent as unsetting parent for setParent API;

#### 2.0

- 🎉 Release new version
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/graphlib",
"version": "2.0.0",
"version": "2.0.2",
"sideEffects": false,
"scripts": {
"clean": "rimraf lib esm dist",
Expand Down
17 changes: 15 additions & 2 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,16 +1001,29 @@ export class Graph<
/**
* Set node parent. If this operation causes a circle, it fails with an error.
* @param id - ID of the child node.
* @param parent - ID of the parent node.
* @param parent - ID of the parent node. If it is undefined, means unset parent for node with id.
* @param treeKey - Which tree structure the relation is applied to.
* @group TreeMethods
*/
public setParent(id: ID, parent: ID, treeKey?: string) {
public setParent(id: ID, parent?: ID, treeKey?: string) {
this.checkTreeExistence(treeKey);

const tree = this.treeIndices.get(treeKey)!;
const node = this.getNode(id);
const oldParent = tree.parentMap.get(id);

// Same parent id as old one, skip
if (oldParent?.id === parent) return;

// New parent is undefined, unset parent for the node
if (parent === undefined) {
if (oldParent) {
tree.childrenMap.get(oldParent.id)?.delete(node);
}
tree.parentMap.delete(id);
return;
}

const newParent = this.getNode(parent);

// Set parent
Expand Down

0 comments on commit a759533

Please sign in to comment.