Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@antv/graphlib

Package Overview
Dependencies
Maintainers
64
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@antv/graphlib - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

coverage/clover.xml

2

dist/index.umd.min.js

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GraphLib={})}(this,function(e){"use strict";function t(){this._events={}}function d(t,s,e,d){for(;t.length;){var a=t.shift();if(e(a))return!0;s.add(a.id),d(a.id).forEach(e=>{s.has(e.id)||(s.add(e.id),t.push(e))})}return!1}function r(e,t,s,d){if(s(e))return!0;t.add(e.id);for(const a of d(e.id))if(!t.has(a.id)&&r(a,t,s,d))return!0;return!1}t.prototype.on=function(e,t,s){return this._events[e]||(this._events[e]=[]),this._events[e].push({callback:t,once:!!s}),this},t.prototype.once=function(e,t){return this.on(e,t,!0)},t.prototype.emit=function(r){for(var h=this,i=[],e=1;e<arguments.length;e++)i[e-1]=arguments[e];function t(e){for(var t,s,d=e.length,a=0;a<d;a++)e[a]&&(s=(t=e[a]).callback,t.once&&(e.splice(a,1),0===e.length&&delete h._events[r],d--,a--),s.apply(h,i))}var s=this._events[r]||[],d=this._events["*"]||[];t(s),t(d)},t.prototype.off=function(e,t){if(e)if(t){for(var s=this._events[e]||[],d=s.length,a=0;a<d;a++)s[a].callback===t&&(s.splice(a,1),d--,a--);0===s.length&&delete this._events[e]}else delete this._events[e];else this._events={};return this},t.prototype.getEvents=function(){return this._events};const s=()=>!0;class a{graph;nodeFilter;edgeFilter;cacheEnabled;inEdgesMap=new Map;outEdgesMap=new Map;bothEdgesMap=new Map;allNodesMap=new Map;allEdgesMap=new Map;constructor(e){this.graph=e.graph;const d=e.nodeFilter||s,a=e.edgeFilter||s;this.nodeFilter=d,this.edgeFilter=e=>{var{source:t,target:s}=this.graph.getEdgeDetail(e.id);return!(!d(t)||!d(s))&&a(e,t,s)},"auto"===e.cache?(this.cacheEnabled=!0,this.startAutoCache()):"manual"===e.cache?this.cacheEnabled=!0:this.cacheEnabled=!1}clearCache=()=>{this.inEdgesMap.clear(),this.outEdgesMap.clear(),this.bothEdgesMap.clear(),this.allNodesMap.clear(),this.allEdgesMap.clear()};refreshCache=()=>{this.clearCache(),this.updateCache(this.graph.getAllNodes().map(e=>e.id))};updateCache=e=>{const a=new Set;e.forEach(e=>{var t,s,d=this.bothEdgesMap.get(e);d&&d.forEach(e=>a.add(e.id)),this.hasNode(e)?(d=this.graph.getRelatedEdges(e,"in").filter(this.edgeFilter),t=this.graph.getRelatedEdges(e,"out").filter(this.edgeFilter),(s=Array.from(new Set([...d,...t]))).forEach(e=>a.add(e.id)),this.inEdgesMap.set(e,d),this.outEdgesMap.set(e,t),this.bothEdgesMap.set(e,s),this.allNodesMap.set(e,this.graph.getNode(e))):(this.inEdgesMap.delete(e),this.outEdgesMap.delete(e),this.bothEdgesMap.delete(e),this.allNodesMap.delete(e))}),a.forEach(e=>{this.hasEdge(e)?this.allEdgesMap.set(e,this.graph.getEdge(e)):this.allEdgesMap.delete(e)})};startAutoCache(){this.refreshCache(),this.graph.on("changed",this.handleGraphChanged)}stopAutoCache(){this.graph.off("changed",this.handleGraphChanged)}handleGraphChanged=s=>{const d=new Set;s.changes.forEach(e=>{switch(e.type){case"NodeAdded":d.add(e.value.id);break;case"NodeDataUpdated":d.add(e.id);break;case"EdgeAdded":d.add(e.value.source),d.add(e.value.target);break;case"EdgeUpdated":"source"!==e.propertyName&&"target"!==e.propertyName||(d.add(e.oldValue),d.add(e.newValue));break;case"EdgeDataUpdated":var t;s.graph.hasEdge(e.id)&&(t=s.graph.getEdge(e.id),d.add(t.source),d.add(t.target));break;case"EdgeRemoved":d.add(e.value.source),d.add(e.value.target);break;case"NodeRemoved":d.add(e.value.id)}}),this.updateCache(d)};checkNodeExistence(e){this.getNode(e)}hasNode(e){return!!this.graph.hasNode(e)&&(e=this.graph.getNode(e),this.nodeFilter(e))}areNeighbors(t,e){return this.checkNodeExistence(t),this.getNeighbors(e).some(e=>e.id===t)}getNode(e){var t=this.graph.getNode(e);if(this.nodeFilter(t))return t;throw new Error("Node not found for id: "+e)}getRelatedEdges(e,t){return this.checkNodeExistence(e),this.cacheEnabled?("in"===t?this.inEdgesMap:"out"===t?this.outEdgesMap:this.bothEdgesMap).get(e):this.graph.getRelatedEdges(e,t).filter(this.edgeFilter)}getDegree(e,t){return this.getRelatedEdges(e,t).length}getSuccessors(e){e=this.getRelatedEdges(e,"out").map(e=>this.getNode(e.target));return Array.from(new Set(e))}getPredecessors(e){e=this.getRelatedEdges(e,"in").map(e=>this.getNode(e.source));return Array.from(new Set(e))}getNeighbors(e){var t=this.getPredecessors(e),e=this.getSuccessors(e);return Array.from(new Set([...t,...e]))}hasEdge(e){return!!this.graph.hasEdge(e)&&(e=this.graph.getEdge(e),this.edgeFilter(e))}getEdge(e){var t=this.graph.getEdge(e);if(this.edgeFilter(t))return t;throw new Error("Edge not found for id: "+e)}getEdgeDetail(e){e=this.getEdge(e);return{edge:e,source:this.getNode(e.source),target:this.getNode(e.target)}}hasTreeStructure(e){return this.graph.hasTreeStructure(e)}getRoots(e){return this.graph.getRoots(e).filter(this.nodeFilter)}getChildren(e,t){return this.checkNodeExistence(e),this.graph.getChildren(e,t).filter(this.nodeFilter)}getParent(e,t){this.checkNodeExistence(e);e=this.graph.getParent(e,t);return e&&this.nodeFilter(e)?e:null}getAllNodes(){return this.cacheEnabled?Array.from(this.allNodesMap.values()):this.graph.getAllNodes().filter(this.nodeFilter)}getAllEdges(){return this.cacheEnabled?Array.from(this.allEdgesMap.values()):this.graph.getAllEdges().filter(this.edgeFilter)}bfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];d([this.getNode(e)],new Set,t,s)}dfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];r(this.getNode(e),new Set,t,s)}}class h extends t{nodeMap=new Map;edgeMap=new Map;inEdgesMap=new Map;outEdgesMap=new Map;bothEdgesMap=new Map;treeIndices=new Map;changes=[];batchCount=0;onChanged=()=>{};constructor(e){super(),e&&(e.nodes&&this.addNodes(e.nodes),e.edges&&this.addEdges(e.edges),e.tree&&this.addTree(e.tree),e.onChanged)&&(this.onChanged=e.onChanged)}batch=e=>{this.batchCount+=1,e(),--this.batchCount,this.batchCount||this.commit()};commit(){var e=this.changes,e=(this.changes=[],{graph:this,changes:e});this.emit("changed",e),this.onChanged(e)}reduceChanges(e){let a=[];return e.forEach(d=>{switch(d.type){case"NodeRemoved":{let s=!1;a=a.filter(e=>{var t;return"NodeAdded"===e.type?((t=e.value.id===d.value.id)&&(s=!0),!t):"NodeDataUpdated"===e.type?e.id!==d.value.id:"TreeStructureChanged"!==e.type||e.nodeId!==d.value.id}),s||a.push(d);break}case"EdgeRemoved":{let s=!1;a=a.filter(e=>{var t;return"EdgeAdded"===e.type?((t=e.value.id===d.value.id)&&(s=!0),!t):"EdgeDataUpdated"!==e.type&&"EdgeUpdated"!==e.type||e.id!==d.value.id}),s||a.push(d);break}case"NodeDataUpdated":case"EdgeDataUpdated":case"EdgeUpdated":var e=a.findIndex(e=>e.type===d.type&&e.id===d.id&&(void 0===d.propertyName||e.propertyName===d.propertyName)),t=a[e];t?void 0!==d.propertyName?t.newValue=d.newValue:(a.splice(e,1),a.push(d)):a.push(d);break;case"TreeStructureDetached":(a=a.filter(e=>"TreeStructureAttached"!==e.type&&"TreeStructureChanged"!==e.type||e.treeKey!==d.treeKey)).push(d);break;case"TreeStructureChanged":t=a.find(e=>"TreeStructureChanged"===e.type&&e.treeKey===d.treeKey&&e.nodeId===d.nodeId);t?t.newParentId=d.newParentId:a.push(d);break;default:a.push(d)}}),a}checkNodeExistence(e){this.getNode(e)}hasNode(e){return this.nodeMap.has(e)}areNeighbors(t,e){return this.getNeighbors(e).some(e=>e.id===t)}getNode(e){var t=this.nodeMap.get(e);if(t)return t;throw new Error("Node not found for id: "+e)}getRelatedEdges(e,t){var s;return this.checkNodeExistence(e),"in"===t?(s=this.inEdgesMap.get(e),Array.from(s)):"out"===t?(s=this.outEdgesMap.get(e),Array.from(s)):(t=this.bothEdgesMap.get(e),Array.from(t))}getDegree(e,t){return this.getRelatedEdges(e,t).length}getSuccessors(e){e=this.getRelatedEdges(e,"out").map(e=>this.getNode(e.target));return Array.from(new Set(e))}getPredecessors(e){e=this.getRelatedEdges(e,"in").map(e=>this.getNode(e.source));return Array.from(new Set(e))}getNeighbors(e){var t=this.getPredecessors(e),e=this.getSuccessors(e);return Array.from(new Set([...t,...e]))}doAddNode(t){if(this.hasNode(t.id))throw new Error("Node already exists: "+t.id);this.nodeMap.set(t.id,t),this.inEdgesMap.set(t.id,new Set),this.outEdgesMap.set(t.id,new Set),this.bothEdgesMap.set(t.id,new Set),this.treeIndices.forEach(e=>{e.childrenMap.set(t.id,new Set)}),this.changes.push({type:"NodeAdded",value:t})}addNodes(t){this.batch(()=>{for(const e of t)this.doAddNode(e)})}addNode(e){this.addNodes([e])}doRemoveNode(e){var t=this.getNode(e);this.bothEdgesMap.get(e)?.forEach(e=>this.doRemoveEdge(e.id)),this.nodeMap.delete(e),this.treeIndices.forEach(t=>{t.childrenMap.get(e)?.forEach(e=>{t.parentMap.delete(e.id)}),t.parentMap.delete(e),t.childrenMap.delete(e)}),this.changes.push({type:"NodeRemoved",value:t})}removeNodes(e){this.batch(()=>{e.forEach(e=>this.doRemoveNode(e))})}removeNode(e){this.removeNodes([e])}updateNodeDataProperty(s,d,a){const r=this.getNode(s);this.batch(()=>{var e=r.data[d],t=a;r.data[d]=t,this.changes.push({type:"NodeDataUpdated",id:s,propertyName:d,oldValue:e,newValue:t})})}mergeNodeData(s,e){this.batch(()=>{Object.entries(e).forEach(([e,t])=>{this.updateNodeDataProperty(s,e,t)})})}updateNodeData(...e){const d=e[0],a=this.getNode(d);if("string"==typeof e[1])this.updateNodeDataProperty(d,e[1],e[2]);else{let s;var t;"function"==typeof e[1]?(t=e[1],s=t(a.data)):"object"==typeof e[1]&&(s=e[1]),this.batch(()=>{var e=a.data,t=s;a.data=s,this.changes.push({type:"NodeDataUpdated",id:d,oldValue:e,newValue:t})})}}checkEdgeExistence(e){if(!this.hasEdge(e))throw new Error("Edge not found for id: "+e)}hasEdge(e){return this.edgeMap.has(e)}getEdge(e){return this.checkEdgeExistence(e),this.edgeMap.get(e)}getEdgeDetail(e){e=this.getEdge(e);return{edge:e,source:this.getNode(e.source),target:this.getNode(e.target)}}doAddEdge(e){if(this.hasEdge(e.id))throw new Error("Edge already exists: "+e.id);this.checkNodeExistence(e.source),this.checkNodeExistence(e.target),this.edgeMap.set(e.id,e);var t=this.inEdgesMap.get(e.target),s=this.outEdgesMap.get(e.source),d=this.bothEdgesMap.get(e.source),a=this.bothEdgesMap.get(e.target);t.add(e),s.add(e),d.add(e),a.add(e),this.changes.push({type:"EdgeAdded",value:e})}addEdges(t){this.batch(()=>{for(const e of t)this.doAddEdge(e)})}addEdge(e){this.addEdges([e])}doRemoveEdge(e){var t=this.getEdge(e),s=this.outEdgesMap.get(t.source),d=this.inEdgesMap.get(t.target),a=this.bothEdgesMap.get(t.source),r=this.bothEdgesMap.get(t.target);s.delete(t),d.delete(t),a.delete(t),r.delete(t),this.edgeMap.delete(e),this.changes.push({type:"EdgeRemoved",value:t})}removeEdges(e){this.batch(()=>{e.forEach(e=>this.doRemoveEdge(e))})}removeEdge(e){this.removeEdges([e])}updateEdgeSource(e,t){var s=this.getEdge(e);this.checkNodeExistence(t);const d=s.source,a=t;this.outEdgesMap.get(d).delete(s),this.bothEdgesMap.get(d).delete(s),this.outEdgesMap.get(a).add(s),this.bothEdgesMap.get(a).add(s),s.source=t,this.batch(()=>{this.changes.push({type:"EdgeUpdated",id:e,propertyName:"source",oldValue:d,newValue:a})})}updateEdgeTarget(e,t){var s=this.getEdge(e);this.checkNodeExistence(t);const d=s.target,a=t;this.inEdgesMap.get(d).delete(s),this.bothEdgesMap.get(d).delete(s),this.inEdgesMap.get(a).add(s),this.bothEdgesMap.get(a).add(s),s.target=t,this.batch(()=>{this.changes.push({type:"EdgeUpdated",id:e,propertyName:"target",oldValue:d,newValue:a})})}updateEdgeDataProperty(s,d,a){const r=this.getEdge(s);this.batch(()=>{var e=r.data[d],t=a;r.data[d]=t,this.changes.push({type:"EdgeDataUpdated",id:s,propertyName:d,oldValue:e,newValue:t})})}updateEdgeData(...e){const d=e[0],a=this.getEdge(d);if("string"==typeof e[1])this.updateEdgeDataProperty(d,e[1],e[2]);else{let s;var t;"function"==typeof e[1]?(t=e[1],s=t(a.data)):"object"==typeof e[1]&&(s=e[1]),this.batch(()=>{var e=a.data,t=s;a.data=s,this.changes.push({type:"EdgeDataUpdated",id:d,oldValue:e,newValue:t})})}}mergeEdgeData(s,e){this.batch(()=>{Object.entries(e).forEach(([e,t])=>{this.updateEdgeDataProperty(s,e,t)})})}checkTreeExistence(e){if(!this.hasTreeStructure(e))throw new Error("Tree structure not found for treeKey: "+e)}hasTreeStructure(e){return this.treeIndices.has(e)}attachTreeStructure(e){this.treeIndices.has(e)||(this.treeIndices.set(e,{parentMap:new Map,childrenMap:new Map}),this.batch(()=>{this.changes.push({type:"TreeStructureAttached",treeKey:e})}))}detachTreeStructure(e){this.checkTreeExistence(e),this.treeIndices.delete(e),this.batch(()=>{this.changes.push({type:"TreeStructureDetached",treeKey:e})})}addTree(d,a){this.batch(()=>{this.attachTreeStructure(a);for(var e=[],t=Array.isArray(d)?d:[d];t.length;){var s=t.shift();e.push(s),s.children&&t.push(...s.children)}this.addNodes(e),e.forEach(t=>{t.children?.forEach(e=>{this.setParent(e.id,t.id,a)})})})}getRoots(t){return this.checkTreeExistence(t),this.getAllNodes().filter(e=>!this.getParent(e.id,t))}getChildren(e,t){this.checkNodeExistence(e),this.checkTreeExistence(t);t=this.treeIndices.get(t).childrenMap.get(e);return Array.from(t||[])}getParent(e,t){return this.checkNodeExistence(e),this.checkTreeExistence(t),this.treeIndices.get(t).parentMap.get(e)||null}getAncestors(e,t){var s,d=[];let a=this.getNode(e);for(;s=this.getParent(a.id,t);)d.push(s),a=s;return d}setParent(t,s,d){this.checkTreeExistence(d);var a=this.treeIndices.get(d),r=this.getNode(t);const h=a.parentMap.get(t);if(h?.id!==s)if(s){const i=this.getNode(s);a.parentMap.set(t,i),h&&a.childrenMap.get(h.id)?.delete(r);let e=a.childrenMap.get(i.id);e||(e=new Set,a.childrenMap.set(i.id,e)),e.add(r),this.batch(()=>{this.changes.push({type:"TreeStructureChanged",treeKey:d,nodeId:t,oldParentId:h?.id,newParentId:i.id})})}else h&&a.childrenMap.get(h.id)?.delete(r),a.parentMap.delete(t)}dfsTree(e,t,s){return r(this.getNode(e),new Set,t,e=>this.getChildren(e,s))}bfsTree(e,t,s){return d([this.getNode(e)],new Set,t,e=>this.getChildren(e,s))}getAllNodes(){return Array.from(this.nodeMap.values())}getAllEdges(){return Array.from(this.edgeMap.values())}bfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];return d([this.getNode(e)],new Set,t,s)}dfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];return r(this.getNode(e),new Set,t,s)}clone(){var e=this.getAllNodes().map(e=>({...e,data:{...e.data}})),t=this.getAllEdges().map(e=>({...e,data:{...e.data}}));const r=new h({nodes:e,edges:t});return this.treeIndices.forEach(({parentMap:e,childrenMap:t},s)=>{const d=new Map,a=(e.forEach((e,t)=>{d.set(t,r.getNode(e.id))}),new Map);t.forEach((e,t)=>{a.set(t,new Set(Array.from(e).map(e=>r.getNode(e.id))))}),r.treeIndices.set(s,{parentMap:d,childrenMap:a})}),r}toJSON(){return JSON.stringify({nodes:this.getAllNodes(),edges:this.getAllEdges()})}createView(e){return new a({graph:this,...e})}}e.Graph=h,e.GraphView=a,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GraphLib={})}(this,function(e){"use strict";function t(){this._events={}}function d(t,s,e,d){for(;t.length;){var a=t.shift();if(e(a))return!0;s.add(a.id),d(a.id).forEach(e=>{s.has(e.id)||(s.add(e.id),t.push(e))})}return!1}function r(e,t,s,d){if(s(e))return!0;t.add(e.id);for(const a of d(e.id))if(!t.has(a.id)&&r(a,t,s,d))return!0;return!1}t.prototype.on=function(e,t,s){return this._events[e]||(this._events[e]=[]),this._events[e].push({callback:t,once:!!s}),this},t.prototype.once=function(e,t){return this.on(e,t,!0)},t.prototype.emit=function(r){for(var h=this,i=[],e=1;e<arguments.length;e++)i[e-1]=arguments[e];function t(e){for(var t,s,d=e.length,a=0;a<d;a++)e[a]&&(s=(t=e[a]).callback,t.once&&(e.splice(a,1),0===e.length&&delete h._events[r],d--,a--),s.apply(h,i))}var s=this._events[r]||[],d=this._events["*"]||[];t(s),t(d)},t.prototype.off=function(e,t){if(e)if(t){for(var s=this._events[e]||[],d=s.length,a=0;a<d;a++)s[a].callback===t&&(s.splice(a,1),d--,a--);0===s.length&&delete this._events[e]}else delete this._events[e];else this._events={};return this},t.prototype.getEvents=function(){return this._events};const s=()=>!0;class a{graph;nodeFilter;edgeFilter;cacheEnabled;inEdgesMap=new Map;outEdgesMap=new Map;bothEdgesMap=new Map;allNodesMap=new Map;allEdgesMap=new Map;constructor(e){this.graph=e.graph;const d=e.nodeFilter||s,a=e.edgeFilter||s;this.nodeFilter=d,this.edgeFilter=e=>{var{source:t,target:s}=this.graph.getEdgeDetail(e.id);return!(!d(t)||!d(s))&&a(e,t,s)},"auto"===e.cache?(this.cacheEnabled=!0,this.startAutoCache()):"manual"===e.cache?this.cacheEnabled=!0:this.cacheEnabled=!1}clearCache=()=>{this.inEdgesMap.clear(),this.outEdgesMap.clear(),this.bothEdgesMap.clear(),this.allNodesMap.clear(),this.allEdgesMap.clear()};refreshCache=()=>{this.clearCache(),this.updateCache(this.graph.getAllNodes().map(e=>e.id))};updateCache=e=>{const a=new Set;e.forEach(e=>{var t,s,d=this.bothEdgesMap.get(e);d&&d.forEach(e=>a.add(e.id)),this.hasNode(e)?(d=this.graph.getRelatedEdges(e,"in").filter(this.edgeFilter),t=this.graph.getRelatedEdges(e,"out").filter(this.edgeFilter),(s=Array.from(new Set([...d,...t]))).forEach(e=>a.add(e.id)),this.inEdgesMap.set(e,d),this.outEdgesMap.set(e,t),this.bothEdgesMap.set(e,s),this.allNodesMap.set(e,this.graph.getNode(e))):(this.inEdgesMap.delete(e),this.outEdgesMap.delete(e),this.bothEdgesMap.delete(e),this.allNodesMap.delete(e))}),a.forEach(e=>{this.hasEdge(e)?this.allEdgesMap.set(e,this.graph.getEdge(e)):this.allEdgesMap.delete(e)})};startAutoCache(){this.refreshCache(),this.graph.on("changed",this.handleGraphChanged)}stopAutoCache(){this.graph.off("changed",this.handleGraphChanged)}handleGraphChanged=s=>{const d=new Set;s.changes.forEach(e=>{switch(e.type){case"NodeAdded":d.add(e.value.id);break;case"NodeDataUpdated":d.add(e.id);break;case"EdgeAdded":d.add(e.value.source),d.add(e.value.target);break;case"EdgeUpdated":"source"!==e.propertyName&&"target"!==e.propertyName||(d.add(e.oldValue),d.add(e.newValue));break;case"EdgeDataUpdated":var t;s.graph.hasEdge(e.id)&&(t=s.graph.getEdge(e.id),d.add(t.source),d.add(t.target));break;case"EdgeRemoved":d.add(e.value.source),d.add(e.value.target);break;case"NodeRemoved":d.add(e.value.id)}}),this.updateCache(d)};checkNodeExistence(e){this.getNode(e)}hasNode(e){return!!this.graph.hasNode(e)&&(e=this.graph.getNode(e),this.nodeFilter(e))}areNeighbors(t,e){return this.checkNodeExistence(t),this.getNeighbors(e).some(e=>e.id===t)}getNode(e){var t=this.graph.getNode(e);if(this.nodeFilter(t))return t;throw new Error("Node not found for id: "+e)}getRelatedEdges(e,t){return this.checkNodeExistence(e),this.cacheEnabled?("in"===t?this.inEdgesMap:"out"===t?this.outEdgesMap:this.bothEdgesMap).get(e):this.graph.getRelatedEdges(e,t).filter(this.edgeFilter)}getDegree(e,t){return this.getRelatedEdges(e,t).length}getSuccessors(e){e=this.getRelatedEdges(e,"out").map(e=>this.getNode(e.target));return Array.from(new Set(e))}getPredecessors(e){e=this.getRelatedEdges(e,"in").map(e=>this.getNode(e.source));return Array.from(new Set(e))}getNeighbors(e){var t=this.getPredecessors(e),e=this.getSuccessors(e);return Array.from(new Set([...t,...e]))}hasEdge(e){return!!this.graph.hasEdge(e)&&(e=this.graph.getEdge(e),this.edgeFilter(e))}getEdge(e){var t=this.graph.getEdge(e);if(this.edgeFilter(t))return t;throw new Error("Edge not found for id: "+e)}getEdgeDetail(e){e=this.getEdge(e);return{edge:e,source:this.getNode(e.source),target:this.getNode(e.target)}}hasTreeStructure(e){return this.graph.hasTreeStructure(e)}getRoots(e){return this.graph.getRoots(e).filter(this.nodeFilter)}getChildren(e,t){return this.checkNodeExistence(e),this.graph.getChildren(e,t).filter(this.nodeFilter)}getParent(e,t){this.checkNodeExistence(e);e=this.graph.getParent(e,t);return e&&this.nodeFilter(e)?e:null}getAllNodes(){return this.cacheEnabled?Array.from(this.allNodesMap.values()):this.graph.getAllNodes().filter(this.nodeFilter)}getAllEdges(){return this.cacheEnabled?Array.from(this.allEdgesMap.values()):this.graph.getAllEdges().filter(this.edgeFilter)}bfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];d([this.getNode(e)],new Set,t,s)}dfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];r(this.getNode(e),new Set,t,s)}}class h extends t{nodeMap=new Map;edgeMap=new Map;inEdgesMap=new Map;outEdgesMap=new Map;bothEdgesMap=new Map;treeIndices=new Map;changes=[];batchCount=0;onChanged=()=>{};constructor(e){super(),e&&(e.nodes&&this.addNodes(e.nodes),e.edges&&this.addEdges(e.edges),e.tree&&this.addTree(e.tree),e.onChanged)&&(this.onChanged=e.onChanged)}batch=e=>{this.batchCount+=1,e(),--this.batchCount,this.batchCount||this.commit()};commit(){var e=this.changes,e=(this.changes=[],{graph:this,changes:e});this.emit("changed",e),this.onChanged(e)}reduceChanges(e){let a=[];return e.forEach(d=>{switch(d.type){case"NodeRemoved":{let s=!1;a=a.filter(e=>{var t;return"NodeAdded"===e.type?((t=e.value.id===d.value.id)&&(s=!0),!t):"NodeDataUpdated"===e.type?e.id!==d.value.id:"TreeStructureChanged"!==e.type||e.nodeId!==d.value.id}),s||a.push(d);break}case"EdgeRemoved":{let s=!1;a=a.filter(e=>{var t;return"EdgeAdded"===e.type?((t=e.value.id===d.value.id)&&(s=!0),!t):"EdgeDataUpdated"!==e.type&&"EdgeUpdated"!==e.type||e.id!==d.value.id}),s||a.push(d);break}case"NodeDataUpdated":case"EdgeDataUpdated":case"EdgeUpdated":var e=a.findIndex(e=>e.type===d.type&&e.id===d.id&&(void 0===d.propertyName||e.propertyName===d.propertyName)),t=a[e];t?void 0!==d.propertyName?t.newValue=d.newValue:(a.splice(e,1),a.push(d)):a.push(d);break;case"TreeStructureDetached":(a=a.filter(e=>"TreeStructureAttached"!==e.type&&"TreeStructureChanged"!==e.type||e.treeKey!==d.treeKey)).push(d);break;case"TreeStructureChanged":t=a.find(e=>"TreeStructureChanged"===e.type&&e.treeKey===d.treeKey&&e.nodeId===d.nodeId);t?t.newParentId=d.newParentId:a.push(d);break;default:a.push(d)}}),a}checkNodeExistence(e){this.getNode(e)}hasNode(e){return this.nodeMap.has(e)}areNeighbors(t,e){return this.getNeighbors(e).some(e=>e.id===t)}getNode(e){var t=this.nodeMap.get(e);if(t)return t;throw new Error("Node not found for id: "+e)}getRelatedEdges(e,t){var s;return this.checkNodeExistence(e),"in"===t?(s=this.inEdgesMap.get(e),Array.from(s)):"out"===t?(s=this.outEdgesMap.get(e),Array.from(s)):(t=this.bothEdgesMap.get(e),Array.from(t))}getDegree(e,t){return this.getRelatedEdges(e,t).length}getSuccessors(e){e=this.getRelatedEdges(e,"out").map(e=>this.getNode(e.target));return Array.from(new Set(e))}getPredecessors(e){e=this.getRelatedEdges(e,"in").map(e=>this.getNode(e.source));return Array.from(new Set(e))}getNeighbors(e){var t=this.getPredecessors(e),e=this.getSuccessors(e);return Array.from(new Set([...t,...e]))}doAddNode(t){if(this.hasNode(t.id))throw new Error("Node already exists: "+t.id);this.nodeMap.set(t.id,t),this.inEdgesMap.set(t.id,new Set),this.outEdgesMap.set(t.id,new Set),this.bothEdgesMap.set(t.id,new Set),this.treeIndices.forEach(e=>{e.childrenMap.set(t.id,new Set)}),this.changes.push({type:"NodeAdded",value:t})}addNodes(t){this.batch(()=>{for(const e of t)this.doAddNode(e)})}addNode(e){this.addNodes([e])}doRemoveNode(e){var t=this.getNode(e);this.bothEdgesMap.get(e)?.forEach(e=>this.doRemoveEdge(e.id)),this.nodeMap.delete(e),this.treeIndices.forEach(t=>{t.childrenMap.get(e)?.forEach(e=>{t.parentMap.delete(e.id)}),t.parentMap.delete(e),t.childrenMap.delete(e)}),this.changes.push({type:"NodeRemoved",value:t})}removeNodes(e){this.batch(()=>{e.forEach(e=>this.doRemoveNode(e))})}removeNode(e){this.removeNodes([e])}updateNodeDataProperty(s,d,a){const r=this.getNode(s);this.batch(()=>{var e=r.data[d],t=a;r.data[d]=t,this.changes.push({type:"NodeDataUpdated",id:s,propertyName:d,oldValue:e,newValue:t})})}mergeNodeData(s,e){this.batch(()=>{Object.entries(e).forEach(([e,t])=>{this.updateNodeDataProperty(s,e,t)})})}updateNodeData(...e){const d=e[0],a=this.getNode(d);if("string"==typeof e[1])this.updateNodeDataProperty(d,e[1],e[2]);else{let s;var t;"function"==typeof e[1]?(t=e[1],s=t(a.data)):"object"==typeof e[1]&&(s=e[1]),this.batch(()=>{var e=a.data,t=s;a.data=s,this.changes.push({type:"NodeDataUpdated",id:d,oldValue:e,newValue:t})})}}checkEdgeExistence(e){if(!this.hasEdge(e))throw new Error("Edge not found for id: "+e)}hasEdge(e){return this.edgeMap.has(e)}getEdge(e){return this.checkEdgeExistence(e),this.edgeMap.get(e)}getEdgeDetail(e){e=this.getEdge(e);return{edge:e,source:this.getNode(e.source),target:this.getNode(e.target)}}doAddEdge(e){if(this.hasEdge(e.id))throw new Error("Edge already exists: "+e.id);this.checkNodeExistence(e.source),this.checkNodeExistence(e.target),this.edgeMap.set(e.id,e);var t=this.inEdgesMap.get(e.target),s=this.outEdgesMap.get(e.source),d=this.bothEdgesMap.get(e.source),a=this.bothEdgesMap.get(e.target);t.add(e),s.add(e),d.add(e),a.add(e),this.changes.push({type:"EdgeAdded",value:e})}addEdges(t){this.batch(()=>{for(const e of t)this.doAddEdge(e)})}addEdge(e){this.addEdges([e])}doRemoveEdge(e){var t=this.getEdge(e),s=this.outEdgesMap.get(t.source),d=this.inEdgesMap.get(t.target),a=this.bothEdgesMap.get(t.source),r=this.bothEdgesMap.get(t.target);s.delete(t),d.delete(t),a.delete(t),r.delete(t),this.edgeMap.delete(e),this.changes.push({type:"EdgeRemoved",value:t})}removeEdges(e){this.batch(()=>{e.forEach(e=>this.doRemoveEdge(e))})}removeEdge(e){this.removeEdges([e])}updateEdgeSource(e,t){var s=this.getEdge(e);this.checkNodeExistence(t);const d=s.source,a=t;this.outEdgesMap.get(d).delete(s),this.bothEdgesMap.get(d).delete(s),this.outEdgesMap.get(a).add(s),this.bothEdgesMap.get(a).add(s),s.source=t,this.batch(()=>{this.changes.push({type:"EdgeUpdated",id:e,propertyName:"source",oldValue:d,newValue:a})})}updateEdgeTarget(e,t){var s=this.getEdge(e);this.checkNodeExistence(t);const d=s.target,a=t;this.inEdgesMap.get(d).delete(s),this.bothEdgesMap.get(d).delete(s),this.inEdgesMap.get(a).add(s),this.bothEdgesMap.get(a).add(s),s.target=t,this.batch(()=>{this.changes.push({type:"EdgeUpdated",id:e,propertyName:"target",oldValue:d,newValue:a})})}updateEdgeDataProperty(s,d,a){const r=this.getEdge(s);this.batch(()=>{var e=r.data[d],t=a;r.data[d]=t,this.changes.push({type:"EdgeDataUpdated",id:s,propertyName:d,oldValue:e,newValue:t})})}updateEdgeData(...e){const d=e[0],a=this.getEdge(d);if("string"==typeof e[1])this.updateEdgeDataProperty(d,e[1],e[2]);else{let s;var t;"function"==typeof e[1]?(t=e[1],s=t(a.data)):"object"==typeof e[1]&&(s=e[1]),this.batch(()=>{var e=a.data,t=s;a.data=s,this.changes.push({type:"EdgeDataUpdated",id:d,oldValue:e,newValue:t})})}}mergeEdgeData(s,e){this.batch(()=>{Object.entries(e).forEach(([e,t])=>{this.updateEdgeDataProperty(s,e,t)})})}checkTreeExistence(e){if(!this.hasTreeStructure(e))throw new Error("Tree structure not found for treeKey: "+e)}hasTreeStructure(e){return this.treeIndices.has(e)}attachTreeStructure(e){this.treeIndices.has(e)||(this.treeIndices.set(e,{parentMap:new Map,childrenMap:new Map}),this.batch(()=>{this.changes.push({type:"TreeStructureAttached",treeKey:e})}))}detachTreeStructure(e){this.checkTreeExistence(e),this.treeIndices.delete(e),this.batch(()=>{this.changes.push({type:"TreeStructureDetached",treeKey:e})})}addTree(d,a){this.batch(()=>{this.attachTreeStructure(a);for(var e=[],t=Array.isArray(d)?d:[d];t.length;){var s=t.shift();e.push(s),s.children&&t.push(...s.children)}this.addNodes(e),e.forEach(t=>{t.children?.forEach(e=>{this.setParent(e.id,t.id,a)})})})}getRoots(t){return this.checkTreeExistence(t),this.getAllNodes().filter(e=>!this.getParent(e.id,t))}getChildren(e,t){this.checkNodeExistence(e),this.checkTreeExistence(t);t=this.treeIndices.get(t).childrenMap.get(e);return Array.from(t||[])}getParent(e,t){return this.checkNodeExistence(e),this.checkTreeExistence(t),this.treeIndices.get(t).parentMap.get(e)||null}getAncestors(e,t){var s,d=[];let a=this.getNode(e);for(;s=this.getParent(a.id,t);)d.push(s),a=s;return d}setParent(t,s,d){this.checkTreeExistence(d);var a=this.treeIndices.get(d),r=this.getNode(t);const h=a.parentMap.get(t);if(h?.id!==s)if(void 0===s)h&&a.childrenMap.get(h.id)?.delete(r),a.parentMap.delete(t);else{const i=this.getNode(s);a.parentMap.set(t,i),h&&a.childrenMap.get(h.id)?.delete(r);let e=a.childrenMap.get(i.id);e||(e=new Set,a.childrenMap.set(i.id,e)),e.add(r),this.batch(()=>{this.changes.push({type:"TreeStructureChanged",treeKey:d,nodeId:t,oldParentId:h?.id,newParentId:i.id})})}}dfsTree(e,t,s){return r(this.getNode(e),new Set,t,e=>this.getChildren(e,s))}bfsTree(e,t,s){return d([this.getNode(e)],new Set,t,e=>this.getChildren(e,s))}getAllNodes(){return Array.from(this.nodeMap.values())}getAllEdges(){return Array.from(this.edgeMap.values())}bfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];return d([this.getNode(e)],new Set,t,s)}dfs(e,t,s="out"){s={in:this.getPredecessors.bind(this),out:this.getSuccessors.bind(this),both:this.getNeighbors.bind(this)}[s];return r(this.getNode(e),new Set,t,s)}clone(){var e=this.getAllNodes().map(e=>({...e,data:{...e.data}})),t=this.getAllEdges().map(e=>({...e,data:{...e.data}}));const r=new h({nodes:e,edges:t});return this.treeIndices.forEach(({parentMap:e,childrenMap:t},s)=>{const d=new Map,a=(e.forEach((e,t)=>{d.set(t,r.getNode(e.id))}),new Map);t.forEach((e,t)=>{a.set(t,new Set(Array.from(e).map(e=>r.getNode(e.id))))}),r.treeIndices.set(s,{parentMap:d,childrenMap:a})}),r}toJSON(){return JSON.stringify({nodes:this.getAllNodes(),edges:this.getAllEdges()})}createView(e){return new a({graph:this,...e})}}e.Graph=h,e.GraphView=a,Object.defineProperty(e,"__esModule",{value:!0})});

@@ -869,3 +869,3 @@ import EventEmitter from '@antv/event-emitter';

// New parent is undefined, unset parent for the node
if (!parent) {
if (parent === undefined) {
if (oldParent) {

@@ -872,0 +872,0 @@ tree.childrenMap.get(oldParent.id)?.delete(node);

@@ -875,3 +875,3 @@ "use strict";

// New parent is undefined, unset parent for the node
if (!parent) {
if (parent === undefined) {
if (oldParent) {

@@ -878,0 +878,0 @@ tree.childrenMap.get(oldParent.id)?.delete(node);

{
"name": "@antv/graphlib",
"version": "2.0.1",
"version": "2.0.2",
"sideEffects": false,

@@ -5,0 +5,0 @@ "scripts": {

@@ -24,3 +24,3 @@ # Graphlib

#### 2.0.1
#### 2.0.2

@@ -27,0 +27,0 @@ - feat: supports undefined parent as unsetting parent for setParent API;

@@ -1019,3 +1019,3 @@ import EventEmitter from '@antv/event-emitter';

// New parent is undefined, unset parent for the node
if (!parent) {
if (parent === undefined) {
if (oldParent) {

@@ -1022,0 +1022,0 @@ tree.childrenMap.get(oldParent.id)?.delete(node);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc