From 5e039de6185d32a2bc0fba8a7b10e5c2a320abe0 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 27 Nov 2024 16:27:48 +0800 Subject: [PATCH 1/3] refactor: setParent support pass null to cancel relationship --- src/graph.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/graph.ts b/src/graph.ts index 9aafa61..78a9dfd 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -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); } From b2ddfe01a0def5df85a9b436f300f4a14f7c1ca0 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 27 Nov 2024 16:27:58 +0800 Subject: [PATCH 2/3] chore: update version and deps --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index ceeb668..420fcc7 100644 --- a/package.json +++ b/package.json @@ -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", @@ -35,14 +35,14 @@ "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", @@ -50,12 +50,12 @@ "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" From 503b950117024680bfa25fb503e55bfa8faee824 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 27 Nov 2024 16:53:05 +0800 Subject: [PATCH 3/3] test: update test case --- __tests__/tree.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/__tests__/tree.spec.ts b/__tests__/tree.spec.ts index 6b78c32..021eda1 100644 --- a/__tests__/tree.spec.ts +++ b/__tests__/tree.spec.ts @@ -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);