Skip to content

Commit

Permalink
refactor: setParent support pass null to cancel relationship (#16)
Browse files Browse the repository at this point in the history
* refactor: setParent support pass null to cancel relationship

* chore: update version and deps

* test: update test case
  • Loading branch information
Aarebecca authored Nov 27, 2024
1 parent 1ac494f commit d2a790c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
12 changes: 12 additions & 0 deletions __tests__/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ describe('Tree related methods', () => {
expect(graph.getChildren(1).map((n) => n.id)).toEqual([3]);
});

test('Remove parent', () => {
const graph = initTreeGraph();
graph.setParent(2, 4);
expect(graph.getParent(2)?.id).toEqual(4);
graph.setParent(2, null);
expect(graph.getParent(2)).toEqual(null);

graph.setParent(2, 1);
expect(graph.getParent(2)?.id).toEqual(1);
graph.setParent(2); // use undefined to remove parent
});

test('Removing nodes', () => {
const graph = initTreeGraph();
graph.removeNode(1);
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/graphlib",
"version": "2.0.3",
"version": "2.0.4",
"main": "lib/index.js",
"module": "esm/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -35,27 +35,27 @@
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@rollup/plugin-commonjs": "^21.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4",
"@types/jest": "^29.5.12",
"@types/jest": "^29.5.14",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"eslint": "^8.57.1",
"eslint-plugin-import": "^2.31.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"limit-size": "^0.1.4",
"lint-staged": "^13.3.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rimraf": "^3.0.2",
"rollup": "^2.79.1",
"rollup": "^2.79.2",
"rollup-plugin-polyfill-node": "^0.8.0",
"rollup-plugin-typescript2": "^0.35.0",
"rollup-plugin-visualizer": "^5.12.0",
"ts-jest": "^29.1.5",
"tslib": "^2.6.3",
"ts-jest": "^29.2.5",
"tslib": "^2.8.1",
"typedoc": "^0.25.13",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^4.9.5"
Expand Down
13 changes: 8 additions & 5 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,22 +1008,25 @@ 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. If it is undefined, means unset parent for node with id.
* @param parent - ID of the parent node. If it is undefined or null, 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 | null, treeKey?: string) {
this.checkTreeExistence(treeKey);

const tree = this.treeIndices.get(treeKey)!;
const tree = this.treeIndices.get(treeKey);

if (!tree) return;

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) {
// New parent is undefined or null, unset parent for the node
if (parent === undefined || parent === null) {
if (oldParent) {
tree.childrenMap.get(oldParent.id)?.delete(node);
}
Expand Down

0 comments on commit d2a790c

Please sign in to comment.