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); 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" 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); }