graphts
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("uuid/v4"),require("object-resolve-path")):"function"==typeof define&&define.amd?define("graphts",["exports","uuid/v4","object-resolve-path"],t):t(e.graphts={},e.uuid,e.objectResolvePath)}(this,function(e,r,s){"use strict";r=r&&r.hasOwnProperty("default")?r["default"]:r,s=s&&s.hasOwnProperty("default")?s["default"]:s;var i=function(){function e(e,t){this._id=e,this.data=t}return Object.defineProperty(e.prototype,"id",{get:function(){return this._id},enumerable:!0,configurable:!0}),e}(),n=function(){function e(e,t,r){this._from=e,this._to=t,this.data=r}return Object.defineProperty(e.prototype,"from",{get:function(){return this._from},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"to",{get:function(){return this._to},enumerable:!0,configurable:!0}),e.prototype.match=function(e,t){if(e&&t)return this._from===e&&this._to===t;if(e&&!t)return this._from===e;if(!e&&t)return this._to===t;if(!e&&!t)throw Error("ErrorNoVertice: match() needs at least one valid vertice.");return!1},e}(),t=function(){function d(e){this._settings={directed:!0,acyclic:!1,allowSelfLoops:!0,maxVertices:-1,maxEdges:-1,maxEdgesFromVertice:-1,maxEdgesToVertice:-1,maxEdgesPerVertice:-1},this._vertices=new Map,this._edges=new Map,Object.assign(this._settings,e)}return Object.defineProperty(d.prototype,"settings",{get:function(){return this._settings},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"vertices",{get:function(){return this._vertices},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"edges",{get:function(){return this._edges},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"count",{get:function(){return this._vertices.size},enumerable:!0,configurable:!0}),d.prototype.canAddVertice=function(){this._settings.maxVertices===this._vertices.size&&this["throw"](d.ERROR_TOO_MANY_VERTICES,"Graph allows a maximum of "+this._settings.maxVertices+" vertices.")},d.prototype.addVertice=function(e){this.canAddVertice();var t=new i(r(),e);return this._vertices.set(t.id,t),t},d.prototype.addVerticeWithId=function(e,t){this.canAddVertice();var r=new i(e,t);return this._vertices.set(r.id,r),r},d.prototype.deleteVertice=function(e){return this._vertices.has(e)||this["throw"](d.ERROR_NOT_FOUND,"Attempted deletion of non-existing vertice, identified by: "+e+".\n"),this.deleteEdgesBidirectionally(this._vertices.get(e)),this._vertices["delete"](e)},d.prototype.getVertice=function(e){return this._vertices.get(e)||null},d.prototype.getVerticeIds=function(){return Array.from(this._vertices.keys())},d.prototype.find=function(t,r){var i=[];return this._vertices.forEach(function(e){("object"==typeof e.data&&r?s(e.data,r):e.data)===t&&i.push(e)}),i},d.prototype.countEdges=function(){var t=0;return this._edges.forEach(function(e){t+=e.length}),t},d.prototype.canAddEdge=function(e,t){if(this._settings.acyclic&&this.cycleTest(e,t)&&this["throw"](d.ERROR_CYCLE,"Trying to create an edge that would cause a cyle, but graph does not allow cycles.",[e,t]),this._settings.allowSelfLoops||e!==t||this["throw"](d.ERROR_SELF_LOOP,"Trying to create self-loop edge to vertice, but graph does not allow self-loops.",[e]),0<=this._settings.maxEdges){var r=this.countEdges();this._settings.maxEdges===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdges+" edges.",[e,t])}if(0<=this._settings.maxEdgesPerVertice){r=this.findEdgesBidirectionally(e).length;this._settings.maxEdgesPerVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesPerVertice+" edges per vertice.",[e]),r=this.findEdgesBidirectionally(t).length,this._settings.maxEdgesPerVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesPerVertice+" edges per vertice.",[t])}if(0<=this._settings.maxEdgesFromVertice){r=this.findEdges(e).length;this._settings.maxEdgesFromVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesFromVertice+" edges from vertice.",[e])}if(0<=this._settings.maxEdgesToVertice){r=this.findEdges(null,t).length;this._settings.maxEdgesFromVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesFromVertice+" edges from vertice.",[t])}var i=this.findEdges(e,t);i.length?this["throw"](d.ERROR_DUPLICATE_EDGE,"An edge between these vertices already exists.",[e,t]):this._settings.directed||(i=this.findEdges(t,e)).length&&this["throw"](d.ERROR_DUPLICATE_EDGE,"An edge between these vertices already exists, in the opposite direction. This undirected graph only allows one edge between two vertices, regardless of direction.",[e,t])},d.prototype.addEdge=function(e,t,r){this.canAddEdge(e,t);var i=new n(e,t,r||null),s=this._edges.get(e.id)||[];return s.push(i),this._edges.set(e.id,s),i},d.prototype.findEdges=function(e,r){var i=[];if(e||r)if(e&&!r)i=this._edges.get(e.id)||[];else if(e&&r)for(var t=this._edges.get(e.id)||[],s=0;s<t.length;s++)t[s].to===r&&i.push(t[s]);else!e&&r&&this._edges.forEach(function(e){for(var t=0;t<e.length;t++)e[t].to===r&&i.push(e[t])});else this["throw"](d.ERROR_NO_VERTICE,"Cannot findEdges() for a null vertice.",[e]);return i},d.prototype.findFrom=function(e,t){void 0===t&&(t=Infinity);for(var r=this.findEdges(null,e),i=[],s=0;s<r.length&&s<t;s++)i.push(r[s].from);return i},d.prototype.findTo=function(e,t){void 0===t&&(t=Infinity);for(var r=this.findEdges(e),i=[],s=0;s<r.length&&s<t;s++)i.push(r[s].to);return i},d.prototype.findEdgesBidirectionally=function(e,t){return this.findEdges(e,t).concat(this.findEdges(t,e))},d.prototype.deleteEdgesFromArray=function(e,t,r){for(var i=[],s=0;s<e.length;s++)e[s].match(t,r)||i.push(e[s]);return i},d.prototype.deleteEdges=function(r,i){var s=this,n=0,o=[];if(r||i)if(r&&!i){var e=this._edges.get(r.id)||[];n=e.length,this._edges["delete"](r.id)}else if(r&&i){e=this.findEdges(r);(o=this.deleteEdgesFromArray(e,r,i)).length?this._edges.set(r.id,o):this._edges["delete"](r.id),n=e.length-o.length}else!r&&i&&this._edges.forEach(function(e,t){(o=s.deleteEdgesFromArray(e,r,i)).length?s._edges.set(t,o):s._edges["delete"](t),n+=e.length-o.length});else this["throw"](d.ERROR_NO_VERTICE,"Cannot deleteEdges() of null vertice.",[r]);return n},d.prototype.deleteEdgesBidirectionally=function(e,t){return this.deleteEdges(e,t)+this.deleteEdges(t,e)},d.prototype.findNeighbors=function(e){for(var t=this.findEdgesBidirectionally(e),r=[],i=0;i<t.length;i++)r.push(t[i].to);return r},d.prototype.clear=function(){this._vertices.clear(),this.clearEdges()},d.prototype.clearEdges=function(){this._edges.clear()},d.prototype.depthFirstSearch=function(e,t){var r,i=[],s=[],n=0,o=[];for(i.push(e);i.length;)if(e=i.pop(),s.indexOf(e.id)<0){if(s.push(e.id),n++,o.push(e),t&&t(e,n))return o;r=this.findEdges(e);for(var d=0;d<r.length;d++)i.push(r[d].to);if(!this._settings.directed){r=this.findEdges(null,e);for(d=0;d<r.length;d++)i.push(r[d].from)}}return o},d.prototype.breadthFirstSearch=function(e,t){var r,i=[],s=[],n=0,o=[];for(i.push(e);0<=i.length&&e;)if(s.indexOf(e.id)<0){if(s.push(e.id),n++,o.push(e),t&&t(e,n))return o;if((r=this.findEdges(e)).length)for(var d=0;d<r.length;d++)i.push(r[d].to);if(!this._settings.directed&&(r=this.findEdges(null,e)).length)for(d=0;d<r.length;d++)i.push(r[d].from)}else e=i.shift();return o},d.prototype.findByInDegree=function(t){var r=this,i=[];return this._vertices.forEach(function(e){r.findEdges(null,e).length===t&&i.push(e)}),i},d.prototype.getInDegrees=function(){var t=this,r={};return this._vertices.forEach(function(e){r[e.id]={vertice:e,inDegree:t.findEdges(null,e).length}}),r},d.prototype.getOutDegrees=function(){var t=this,r={};return this._vertices.forEach(function(e){r[e.id]={vertice:e,outDegree:t.findEdges(e).length}}),r},d.prototype.findByOutDegree=function(t){var r=this,i=[];return this._vertices.forEach(function(e){r.findEdges(e).length===t&&i.push(e)}),i},d.prototype.topologicalSort=function(e){for(var t,r=this.findByInDegree(0),i=this.getInDegrees(),s=[],n=[],o=0;r.length;){for(var d=0;d<r.length;d++){t=this.findTo(r[d]);for(var h=0;h<t.length;h++)i[t[h].id].inDegree--,0===i[t[h].id].inDegree&&s.push(i[t[h].id].vertice);if(o++,e(r[d],o))return n}n=n.concat(r),r=[].concat(s),s=[]}return this._vertices.size===n.length&&n},d.prototype.connected=function(e,t){var r=[],i=!1;return this.depthFirstSearch(e,function s(e){if(r.push(e),t===e)return i=!0}),i&&1!==r.length||(r=[]),r},d.prototype.expand=function(e){var r=this;return this.breadthFirstSearch(e,function i(e,t){Object.assign(e,{from:r.findFrom(e),to:r.findTo(e)})}),e},d.prototype.cycleTest=function(t,e){var r=!1;return this.depthFirstSearch(e,function i(e){if(e===t)return r=!0}),r},d.prototype.reorderTo=function(i,e){for(var s=e(this.findTo(i).map(function(e){return e.id})),n=this.findEdges(i),o=[],t=function(e){var t=s[e],r=n.find(function(e){return e.to.id===t});if(!r)throw Error("Edge from "+i.id+" to "+t+" was not found.");o.push(r)},r=0;r<s.length;r++)t(r);if(o.length!==n.length)throw Error("Ordered edges length differs from the length of the original edges.");var d=function(t){if(o.findIndex(function(e){return e.to.id===n[t].to.id})<0)throw Error("At least one edge was not found in the ordered edges.")};for(r=0;r<n.length;r++)d(r);this._edges.set(i.id,o)},d.prototype.serialize=function(){return{vertices:this.serializeVertices(),edges:this.serializeEdges()}},d.prototype.serializeEdges=function(){var n={};return this._edges.forEach(function(e,t){for(var r,i=0;i<e.length;i++){var s=e[i];n[t]||(n[t]=[]),n[t].push(((r={})[s.to.id]=s.data,r))}}),n},d.prototype.serializeVertices=function(){return Array.from(this._vertices.values())},d.prototype["import"]=function(e){var i=this;e.vertices&&e.edges||this["throw"](d.ERROR_IMPORT_DATA,'The data to import is not in a proper format. It must have "vertices" and "edges" keys.');for(var t=0;t<e.vertices.length;t++)this.addVerticeWithId(e.vertices[t].id,e.vertices[t].data);Object.keys(e.edges).forEach(function(r){e.edges[r].forEach(function(e){var t=Object.keys(e)[0];i.addEdge(i.vertices.get(r),i.vertices.get(t),e[t])})})},d.prototype["throw"]=function(e,t,r){throw{name:e,message:t,vertices:r}},d.ERROR_NOT_FOUND="ErrorNotFound",d.ERROR_NO_VERTICE="ErrorNoVertice",d.ERROR_TOO_MANY_VERTICES="ErrorTooManyVertices",d.ERROR_TOO_MANY_EDGES="ErrorTooManyEdges",d.ERROR_SELF_LOOP="ErrorSelfLoop",d.ERROR_CYCLE="ErrorCycle",d.ERROR_DUPLICATE_EDGE="ErrorDuplicateEdge",d.ERROR_IMPORT_DATA="ErrorImportData",d}();e.GraphVertice=i,e.GraphEdge=n,e.Graph=t,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("uuid/v4"),require("object-resolve-path")):"function"==typeof define&&define.amd?define("graphts",["exports","uuid/v4","object-resolve-path"],t):t(e.graphts={},e.uuid,e.objectResolvePath)}(this,function(e,r,s){"use strict";r=r&&r.hasOwnProperty("default")?r["default"]:r,s=s&&s.hasOwnProperty("default")?s["default"]:s;var i=function(){function e(e,t){this._id=e,this.data=t}return Object.defineProperty(e.prototype,"id",{get:function(){return this._id},enumerable:!0,configurable:!0}),e}(),n=function(){function e(e,t,r){this._from=e,this._to=t,this.data=r}return Object.defineProperty(e.prototype,"from",{get:function(){return this._from},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"to",{get:function(){return this._to},enumerable:!0,configurable:!0}),e.prototype.match=function(e,t){if(e&&t)return this._from===e&&this._to===t;if(e&&!t)return this._from===e;if(!e&&t)return this._to===t;if(!e&&!t)throw Error("ErrorNoVertice: match() needs at least one valid vertice.");return!1},e}(),t=function(){function d(e){this._settings={directed:!0,acyclic:!1,allowSelfLoops:!0,maxVertices:-1,maxEdges:-1,maxEdgesFromVertice:-1,maxEdgesToVertice:-1,maxEdgesPerVertice:-1},this._vertices=new Map,this._edges=new Map,Object.assign(this._settings,e)}return Object.defineProperty(d.prototype,"settings",{get:function(){return this._settings},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"vertices",{get:function(){return this._vertices},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"edges",{get:function(){return this._edges},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"count",{get:function(){return this._vertices.size},enumerable:!0,configurable:!0}),d.prototype.canAddVertice=function(){this._settings.maxVertices===this._vertices.size&&this["throw"](d.ERROR_TOO_MANY_VERTICES,"Graph allows a maximum of "+this._settings.maxVertices+" vertices.")},d.prototype.addVertice=function(e){this.canAddVertice();var t=new i(r(),e);return this._vertices.set(t.id,t),t},d.prototype.addVerticeWithId=function(e,t){this.canAddVertice();var r=new i(e,t);return this._vertices.set(r.id,r),r},d.prototype.deleteVertice=function(e){return this._vertices.has(e)||this["throw"](d.ERROR_NOT_FOUND,"Attempted deletion of non-existing vertice, identified by: "+e+".\n"),this.deleteEdgesBidirectionally(this._vertices.get(e)),this._vertices["delete"](e)},d.prototype.getVertice=function(e){return this._vertices.get(e)||null},d.prototype.getVerticeIds=function(){return Array.from(this._vertices.keys())},d.prototype.find=function(t,r){var i=[];return this._vertices.forEach(function(e){("object"==typeof e.data&&r?s(e.data,r):e.data)===t&&i.push(e)}),i},d.prototype.countEdges=function(){var t=0;return this._edges.forEach(function(e){t+=e.length}),t},d.prototype.canAddEdge=function(e,t){if(this._settings.acyclic&&this.cycleTest(e,t)&&this["throw"](d.ERROR_CYCLE,"Trying to create an edge that would cause a cyle, but graph does not allow cycles.",[e,t]),this._settings.allowSelfLoops||e!==t||this["throw"](d.ERROR_SELF_LOOP,"Trying to create self-loop edge to vertice, but graph does not allow self-loops.",[e]),0<=this._settings.maxEdges){var r=this.countEdges();this._settings.maxEdges===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdges+" edges.",[e,t])}if(0<=this._settings.maxEdgesPerVertice){r=this.findEdgesBidirectionally(e).length;this._settings.maxEdgesPerVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesPerVertice+" edges per vertice.",[e]),r=this.findEdgesBidirectionally(t).length,this._settings.maxEdgesPerVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesPerVertice+" edges per vertice.",[t])}if(0<=this._settings.maxEdgesFromVertice){r=this.findEdges(e).length;this._settings.maxEdgesFromVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesFromVertice+" edges from vertice.",[e])}if(0<=this._settings.maxEdgesToVertice){r=this.findEdges(null,t).length;this._settings.maxEdgesFromVertice===r&&this["throw"](d.ERROR_TOO_MANY_EDGES,"Graph allows a maximum of "+this._settings.maxEdgesFromVertice+" edges from vertice.",[t])}var i=this.findEdges(e,t);i.length?this["throw"](d.ERROR_DUPLICATE_EDGE,"An edge between these vertices already exists.",[e,t]):this._settings.directed||(i=this.findEdges(t,e)).length&&this["throw"](d.ERROR_DUPLICATE_EDGE,"An edge between these vertices already exists, in the opposite direction. This undirected graph only allows one edge between two vertices, regardless of direction.",[e,t])},d.prototype.addEdge=function(e,t,r){this.canAddEdge(e,t);var i=new n(e,t,r||null),s=this._edges.get(e.id)||[];return s.push(i),this._edges.set(e.id,s),i},d.prototype.findEdges=function(e,r){var i=[];if(e||r)if(e&&!r)i=this._edges.get(e.id)||[];else if(e&&r)for(var t=this._edges.get(e.id)||[],s=0;s<t.length;s++)t[s].to===r&&i.push(t[s]);else!e&&r&&this._edges.forEach(function(e){for(var t=0;t<e.length;t++)e[t].to===r&&i.push(e[t])});else this["throw"](d.ERROR_NO_VERTICE,"Cannot findEdges() for a null vertice.",[e]);return i},d.prototype.findFrom=function(e,t){void 0===t&&(t=Infinity);for(var r=this.findEdges(null,e),i=[],s=0;s<r.length&&s<t;s++)i.push(r[s].from);return i},d.prototype.findTo=function(e,t){void 0===t&&(t=Infinity);for(var r=this.findEdges(e),i=[],s=0;s<r.length&&s<t;s++)i.push(r[s].to);return i},d.prototype.findEdgesBidirectionally=function(e,t){return this.findEdges(e,t).concat(this.findEdges(t,e))},d.prototype.deleteEdgesFromArray=function(e,t,r){for(var i=[],s=0;s<e.length;s++)e[s].match(t,r)||i.push(e[s]);return i},d.prototype.deleteEdges=function(r,i){var s=this,n=0,o=[];if(r||i)if(r&&!i){var e=this._edges.get(r.id)||[];n=e.length,this._edges["delete"](r.id)}else if(r&&i){e=this.findEdges(r);(o=this.deleteEdgesFromArray(e,r,i)).length?this._edges.set(r.id,o):this._edges["delete"](r.id),n=e.length-o.length}else!r&&i&&this._edges.forEach(function(e,t){(o=s.deleteEdgesFromArray(e,r,i)).length?s._edges.set(t,o):s._edges["delete"](t),n+=e.length-o.length});else this["throw"](d.ERROR_NO_VERTICE,"Cannot deleteEdges() of null vertice.",[r]);return n},d.prototype.deleteEdgesBidirectionally=function(e,t){return this.deleteEdges(e,t)+this.deleteEdges(t,e)},d.prototype.findNeighbors=function(e){for(var t=this.findEdgesBidirectionally(e),r=[],i=0;i<t.length;i++)r.push(t[i].to);return r},d.prototype.clear=function(){this._vertices.clear(),this.clearEdges()},d.prototype.clearEdges=function(){this._edges.clear()},d.prototype.depthFirstSearch=function(e,t){var r,i=[],s=[],n=0,o=[];for(i.push(e);i.length;)if(e=i.pop(),s.indexOf(e.id)<0){if(s.push(e.id),n++,o.push(e),t&&t(e,n))return o;r=this.findEdges(e);for(var d=0;d<r.length;d++)i.push(r[d].to);if(!this._settings.directed){r=this.findEdges(null,e);for(d=0;d<r.length;d++)i.push(r[d].from)}}return o},d.prototype.breadthFirstSearch=function(e,t){var r,i=[],s=[],n=0,o=[];for(i.push(e);0<=i.length&&e;)if(s.indexOf(e.id)<0){if(s.push(e.id),n++,o.push(e),t&&t(e,n))return o;if((r=this.findEdges(e)).length)for(var d=0;d<r.length;d++)i.push(r[d].to);if(!this._settings.directed&&(r=this.findEdges(null,e)).length)for(d=0;d<r.length;d++)i.push(r[d].from)}else e=i.shift();return o},d.prototype.findByInDegree=function(t){var r=this,i=[];return this._vertices.forEach(function(e){r.findEdges(null,e).length===t&&i.push(e)}),i},d.prototype.getInDegrees=function(){var t=this,r={};return this._vertices.forEach(function(e){r[e.id]={vertice:e,inDegree:t.findEdges(null,e).length}}),r},d.prototype.getOutDegrees=function(){var t=this,r={};return this._vertices.forEach(function(e){r[e.id]={vertice:e,outDegree:t.findEdges(e).length}}),r},d.prototype.findByOutDegree=function(t){var r=this,i=[];return this._vertices.forEach(function(e){r.findEdges(e).length===t&&i.push(e)}),i},d.prototype.topologicalSort=function(e){for(var t,r=this.findByInDegree(0),i=this.getInDegrees(),s=[],n=[],o=0;r.length;){for(var d=0;d<r.length;d++){t=this.findTo(r[d]);for(var h=0;h<t.length;h++)i[t[h].id].inDegree--,0===i[t[h].id].inDegree&&s.push(i[t[h].id].vertice);if(o++,e(r[d],o))return n}n=n.concat(r),r=[].concat(s),s=[]}return this._vertices.size===n.length&&n},d.prototype.connected=function(e,t){var r=[],i=!1;return this.depthFirstSearch(e,function s(e){if(r.push(e),t===e)return i=!0}),i&&1!==r.length||(r=[]),r},d.prototype.expand=function(e){var r=this;return this.breadthFirstSearch(e,function i(e,t){Object.assign(e,{from:r.findFrom(e),to:r.findTo(e)})}),e},d.prototype.cycleTest=function(t,e){var r=!1;return this.depthFirstSearch(e,function i(e){if(e===t)return r=!0}),r},d.prototype.reorderTo=function(i,e){for(var s=e(this.findTo(i).map(function(e){return e.id})),n=this.findEdges(i),o=[],t=function(e){var t=s[e],r=n.find(function(e){return e.to.id===t});if(!r)throw Error("Edge from "+i.id+" to "+t+" was not found.");o.push(r)},r=0;r<s.length;r++)t(r);if(o.length!==n.length)throw Error("Ordered edges length differs from the length of the original edges.");var d=function(t){if(o.findIndex(function(e){return e.to.id===n[t].to.id})<0)throw Error("At least one edge was not found in the ordered edges.")};for(r=0;r<n.length;r++)d(r);this._edges.set(i.id,o)},d.prototype.serialize=function(){return{vertices:this.serializeVertices(),edges:this.serializeEdges()}},d.prototype.serializeEdges=function(){var n={};return this._edges.forEach(function(e,t){for(var r,i=0;i<e.length;i++){var s=e[i];n[t]||(n[t]=[]),n[t].push(((r={})[s.to.id]=s.data,r))}}),n},d.prototype.serializeVertices=function(){return Array.from(this._vertices.values())},d.prototype["import"]=function(t,s){var n=this;void 0===s&&(s=!1),t.vertices&&t.edges||this["throw"](d.ERROR_IMPORT_DATA,'The data to import is not in a proper format. It must have "vertices" and "edges" keys.');for(var o={},e=0;e<t.vertices.length;e++)if(s){var r=this.addVertice(t.vertices[e].data);o[t.vertices[e].id]=r.id}else this.addVerticeWithId(t.vertices[e].id,t.vertices[e].data);Object.keys(t.edges).forEach(function(e){var i=s?o[e]:e;t.edges[e].forEach(function(e){var t=Object.keys(e)[0],r=s?o[t]:t;n.addEdge(n.vertices.get(i),n.vertices.get(r),e[r])})})},d.prototype["throw"]=function(e,t,r){throw{name:e,message:t,vertices:r}},d.ERROR_NOT_FOUND="ErrorNotFound",d.ERROR_NO_VERTICE="ErrorNoVertice",d.ERROR_TOO_MANY_VERTICES="ErrorTooManyVertices",d.ERROR_TOO_MANY_EDGES="ErrorTooManyEdges",d.ERROR_SELF_LOOP="ErrorSelfLoop",d.ERROR_CYCLE="ErrorCycle",d.ERROR_DUPLICATE_EDGE="ErrorDuplicateEdge",d.ERROR_IMPORT_DATA="ErrorImportData",d}();e.GraphVertice=i,e.GraphEdge=n,e.Graph=t,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=graphts.umd.min.js.map |
@@ -911,13 +911,26 @@ import uuid from 'uuid/v4'; | ||
* @param {?} data A serial representation of the graph. | ||
* @param {?=} reindex If true, the imported graph data will be reindexed with new IDs. | ||
* @return {?} | ||
*/ | ||
import(data) { | ||
import(data, reindex = false) { | ||
if (!data.vertices || !data.edges) { | ||
this.throw(Graph.ERROR_IMPORT_DATA, `The data to import is not in a proper format. It must have "vertices" and "edges" keys.`); | ||
} | ||
// Keys are the imported IDs, values are the new (reindexed) IDs: | ||
/** @type {?} */ | ||
const reindexes = {}; | ||
for (let i = 0; i < data.vertices.length; i++) { | ||
this.addVerticeWithId(data.vertices[i].id, data.vertices[i].data); | ||
if (!reindex) { | ||
this.addVerticeWithId(data.vertices[i].id, data.vertices[i].data); | ||
} | ||
else { | ||
/** @type {?} */ | ||
const vertice = this.addVertice(data.vertices[i].data); | ||
reindexes[data.vertices[i].id] = vertice.id; | ||
} | ||
} | ||
Object.keys(data.edges).forEach(fromId => { | ||
/** @type {?} */ | ||
const actualFromId = reindex ? reindexes[fromId] : fromId; | ||
/** @type {?} */ | ||
const tos = data.edges[fromId]; | ||
@@ -927,3 +940,5 @@ tos.forEach(to => { | ||
const toId = Object.keys(to)[0]; | ||
this.addEdge(this.vertices.get(fromId), this.vertices.get(toId), to[toId]); | ||
/** @type {?} */ | ||
const actualToId = reindex ? reindexes[toId] : toId; | ||
this.addEdge(this.vertices.get(actualFromId), this.vertices.get(actualToId), to[actualToId]); | ||
}); | ||
@@ -930,0 +945,0 @@ }); |
@@ -328,4 +328,5 @@ import { GraphVertice } from './graph-vertice'; | ||
* @param data A serial representation of the graph. | ||
* @param reindex If true, the imported graph data will be reindexed with new IDs. | ||
*/ | ||
import(data: GraphSerialized): void; | ||
import(data: GraphSerialized, reindex?: boolean): void; | ||
/** | ||
@@ -332,0 +333,0 @@ * Throws a formatted exception. |
{ | ||
"name": "graphts", | ||
"version": "1.2.0", | ||
"peerDependencies": { | ||
"object-resolve-path": "^1.1.1", | ||
"uuid": "^3.3.2" | ||
}, | ||
"main": "bundles/graphts.umd.js", | ||
"module": "fesm5/graphts.js", | ||
"es2015": "fesm2015/graphts.js", | ||
"esm5": "esm5/graphts.js", | ||
"esm2015": "esm2015/graphts.js", | ||
"fesm5": "fesm5/graphts.js", | ||
"fesm2015": "fesm2015/graphts.js", | ||
"typings": "graphts.d.ts", | ||
"metadata": "graphts.metadata.json", | ||
"sideEffects": false, | ||
"dependencies": { | ||
"tslib": "^1.9.0" | ||
} | ||
"name": "graphts", | ||
"version": "1.3.0", | ||
"peerDependencies": { | ||
"object-resolve-path": "^1.1.1", | ||
"uuid": "^3.3.2" | ||
}, | ||
"main": "bundles/graphts.umd.js", | ||
"module": "fesm5/graphts.js", | ||
"es2015": "fesm2015/graphts.js", | ||
"esm5": "esm5/graphts.js", | ||
"esm2015": "esm2015/graphts.js", | ||
"fesm5": "fesm5/graphts.js", | ||
"fesm2015": "fesm2015/graphts.js", | ||
"typings": "graphts.d.ts", | ||
"metadata": "graphts.metadata.json", | ||
"sideEffects": false, | ||
"dependencies": { | ||
"tslib": "^1.9.0" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
703262
7211