Comparing version 1.5.0 to 2.0.0
Changelog | ||
---- | ||
### Tue Jan 10 10:33:59 2017 -0600 | ||
- Commit: `6fdf1043def44abc2aeb900ccd39fc564b3b7d1d` | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version 2.0.0 | ||
#### Changes | ||
- Update `Node` & `Edge` id creation. | ||
- Splice `Node`s & `Edge`s from their space on the DB to keep up with memory usage. | ||
- Introduce a GUUID generator. | ||
- Create `findById` and `findIndexById` functions. | ||
- Testing for all of the above | ||
- Update READNE | ||
- Update `devDependencies` | ||
### Thu Jan 5 14:52:24 2017 -0600 | ||
- Commit: `b365d55c68fb83f6f42a676ba753b7aec83c3dcb` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.4.2 | ||
@@ -16,3 +30,3 @@ | ||
- Commit: `1bb3ec4808bea50b01a8ecfb8db2ed2c98d8cbcb` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.4.1 | ||
@@ -25,3 +39,3 @@ | ||
- Commit: `b8ef6a2a58931916df75951c1d2fcc69b0e95d89` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.4.0 | ||
@@ -41,3 +55,3 @@ | ||
- Commit: `93748aa95fe81ef6435b3ab839a5202ee28f25c5` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.3.0 | ||
@@ -50,3 +64,3 @@ | ||
- Commit: `1e5ef48025c32588fd183ab7274b231d245bb460` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.2.0 | ||
@@ -62,3 +76,3 @@ | ||
- Commit: `b0d761352551c95738f0690052283c7de1051276` | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Author: Tom Bremer ([tom@tbremer.com](tom@tbremer.com)) | ||
- Version: 1.1.0 | ||
@@ -65,0 +79,0 @@ |
/** | ||
* Module: giraffe | ||
* Version: 1.5.0 | ||
* Version: 2.0.0 | ||
* By: Tom Bremer <tom@tbremer.com> | ||
@@ -77,2 +77,11 @@ * URL: https://github.com/tbremer/Giraffe#readme | ||
function guid() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
function checkProperties(node, properties) { | ||
@@ -135,4 +144,4 @@ var props = node.properties; | ||
var edge = edges[idx]; | ||
var from = _extends({}, this.nodes[edge.from]); // eslint-disable-line | ||
var through = _extends({}, this.nodes[edge.through]); // eslint-disable-line | ||
var from = _extends({}, findById(edge.from, this.nodes)); // eslint-disable-line | ||
var through = _extends({}, findById(edge.through, this.nodes)); // eslint-disable-line | ||
@@ -145,2 +154,29 @@ built.push(_extends({}, edge, { from: from, through: through })); | ||
/** | ||
* Search a group for an object containing the proper identity | ||
*/ | ||
function findById(id, group) { | ||
for (var idx in group) { | ||
var node = group[idx]; | ||
if ('identity' in node && node.identity === id) return node; | ||
} | ||
} | ||
/** | ||
* Given an ID and a group | ||
* return it's index | ||
* or -1 | ||
* based on IDs | ||
*/ | ||
function findIndexById(id, group) { | ||
for (var idx in group) { | ||
var node = group[idx]; | ||
if ('identity' in node && node.identity === id) return idx; | ||
} | ||
return -1; | ||
} | ||
function Giraffe(dataset, callback) { | ||
@@ -152,2 +188,15 @@ if (dataset && dataset.constructor === Function && !callback) { | ||
Object.defineProperty(this, '_ids', { value: [] }); | ||
Object.defineProperty(this, '_generateId', { | ||
value: function generateId() { | ||
var id = guid(); | ||
if (this._ids.indexOf(id) !== -1) return this._generateId(); | ||
this._ids.push(id); | ||
return id; | ||
} | ||
}); | ||
this.nodes = []; | ||
@@ -170,2 +219,3 @@ this.edges = []; | ||
/** | ||
* Add id's to db._ids array | ||
* Build up this.labels object for Nodes | ||
@@ -175,2 +225,3 @@ */ | ||
var node = this.nodes[idx]; | ||
this._ids.push(node.identity); | ||
if (!node.labels.length) continue; | ||
@@ -187,2 +238,3 @@ | ||
/** | ||
* Add id's to db._ids array | ||
* Build up this.labels object for Edges | ||
@@ -192,2 +244,3 @@ */ | ||
var edge = this.edges[_idx2]; | ||
this._ids.push(edge.identity); | ||
if (!edge.label) throw new Error('All Edges need a label'); | ||
@@ -207,3 +260,3 @@ var _label = edge.label; | ||
var id = this.nodes.length; | ||
var id = this._generateId(); | ||
var node = new Node({ id: id, label: label, data: data }); | ||
@@ -263,3 +316,3 @@ | ||
var edgeId = node.edges[e]; | ||
var edge = this.edges[edgeId]; | ||
var edge = findById(edgeId, this.edges); | ||
var label = edge.label, | ||
@@ -276,3 +329,3 @@ _identity = edge.identity; | ||
*/ | ||
this.edges[edgeId] = undefined; | ||
this.edges.splice(findIndexById(edgeId, this.edges), 1); | ||
} | ||
@@ -321,3 +374,3 @@ | ||
*/ | ||
this.nodes[identity] = undefined; | ||
this.nodes.splice(findIndexById(identity, this.nodes), 1); | ||
} | ||
@@ -339,3 +392,3 @@ | ||
var _through = through[t]; | ||
var id = this.edges.length; | ||
var id = this._generateId(); | ||
var edg = new Edge({ id: id, label: label, data: data, from: _from, through: _through }); | ||
@@ -395,3 +448,3 @@ var labelObj = this.labels.edges; | ||
var id = this.labels.edges[edgeName][idFromLabel]; | ||
var edge = this.edges[id]; | ||
var edge = findById(id, this.edges); | ||
@@ -421,4 +474,4 @@ if (!edge) continue; //edges can be undefined from the `remove` method. | ||
var edgeId = returnObj.edges[edgeIdx]; | ||
var _edge2 = _extends({}, this.edges[edgeId]); | ||
var throughNode = this.nodes[_edge2.through]; | ||
var _edge2 = _extends({}, findById(edgeId, this.edges)); | ||
var throughNode = findById(_edge2.through, this.nodes); | ||
@@ -425,0 +478,0 @@ _edge2.through = _extends({}, throughNode); |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.Giraffe=r()}(this,function(){"use strict";function e(e){var r=e.id,t=e.label,s=e.data;if(!r&&0!==r)throw new Error("All Node's require an id");this.identity=r,this.properties=l({},s),this.labels=t?t.constructor===Array?t:[t]:[],this.edges=[]}function r(e){return h.indexOf(e.constructor)>-1}function t(e){var t=e.from,s=e.through,i=e.id,n=e.data,o=e.label;if(!o||!r(o))throw new Error("All Edges need a single Label");this.identity=i,this.from=t.identity,this.through=s.identity,this.label=o,this.properties=l({},n)}function s(e,r){var t=e.properties;for(var s in r)if("edges"!==s){if(!(s in t))return!1;if(t[s]!==r[s])return!1}return!0}function i(e,r){return Boolean(r&&e in r)}function n(e,r){if(!e||e.constructor!==Array)throw new Error("Objects needs to be an array");for(var t in e){var s=e[t];for(var i in r)if(!(i in s)||s[i].constructor!==r[i])throw new Error("Incorrect shape for "+JSON.stringify(s))}return!0}function o(e){var r=[];for(var t in e){var s=e[t],i=l({},this.nodes[s.from]),n=l({},this.nodes[s.through]);r.push(l({},s,{from:i,through:n}))}return r}function a(e,r){e&&e.constructor===Function&&!r&&(r=e,e={}),this.nodes=[],this.edges=[],this.labels={edges:{},nodes:{}},this.callback=r,i("nodes",e)&&n(e.nodes,d)&&(this.nodes=e.nodes),i("edges",e)&&n(e.edges,c)&&(this.edges=e.edges);for(var t in this.nodes){var s=this.nodes[t];if(s.labels.length)for(var o in s.labels){var a=s.labels[o];a in this.labels.nodes||(this.labels.nodes[a]=[]),this.labels.nodes[a].push(s.identity)}}for(var l in this.edges){var h=this.edges[l];if(!h.label)throw new Error("All Edges need a label");var u=h.label;u in this.labels.edges||(this.labels.edges[u]=[]),this.labels.edges[u].push(h.identity)}}var l=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},d={identity:Number,properties:Object,labels:Array,edges:Array},h=[String,Number],c={identity:Number,properties:Object,label:String,from:Number,through:Number};return a.prototype.create=function(r,t){r.constructor===Object&&(t=r,r=null);var s=this.nodes.length,i=new e({id:s,label:r,data:t});if(this.nodes.push(i),r){var n=this.labels.nodes;r in n||(n[r]=[]),n[r].push(i.identity)}return this.callback&&this.callback("create",i),i},a.prototype.update=function e(e,r,t){e.constructor!==Array&&(e=[e]),r.constructor===Object&&(t=r,r=null),null!==r&&r.constructor!==Array&&(r=[r]),t||(t={});for(var s in e){var i=e[s],n="label"in i;if(n&&r)throw new TypeError("Edge Labels cannot be changed.");r&&(i.labels=i.labels.concat(r)),i.properties=l({},i.properties,t)}return this.callback&&this.callback("update",e),e},a.prototype.remove=function(e){e.constructor!==Array&&(e=[e]);for(var r in e){var t=e[r],s=t.identity;for(var i in t.edges){var n=t.edges[i],o=this.edges[n],a=o.label,l=o.identity;if(a in this.labels.edges){var d=this.labels.edges[a].indexOf(l);d!==-1&&this.labels.edges[a].splice(d,1)}this.edges[n]=void 0}for(var h in t.labels){var a=t.labels[h];if(a in this.labels.nodes){var c=this.labels.nodes[a].indexOf(s);c!==-1&&this.labels.nodes[a].splice(c,1)}}for(var u in this.edges){var f=this.edges[u];if(f&&f.through===s){var b=this.nodes[f.from];if(b){var g=b.edges.indexOf(f.identity);g!==-1&&(b.edges.splice(g,1),this.edges[u]=void 0)}}}this.nodes[s]=void 0}this.callback&&this.callback("remove")},a.prototype.edge=function(e,r,s,i){e.constructor!==Array&&(e=[e]),r.constructor!==Array&&(r=[r]);var n=[];for(var a in e){var l=e[a];for(var d in r){var h=r[d],c=this.edges.length,u=new t({id:c,label:s,data:i,from:l,through:h}),f=this.labels.edges;s in f||(f[s]=[]),f[s].push(u.identity),l.edges.push(c),this.edges.push(u),n.push(u)}}return this.callback&&this.callback("edge",o.call(this,n)),n},a.prototype.query=function(e,r){e||r||(e=r=null),e&&e.constructor===Object&&(r=e,e=null);var t=[];for(var i in this.nodes){var n=this.nodes[i];if(n&&(!e||n.labels.indexOf(e)!==-1)){var o=s(n,r),a=r&&"edges"in r;if(a){var d=r.edges.length,h=0;for(var c in r.edges){var u=r.edges[c],f=[];if(u in this.labels.edges){for(var b in this.labels.edges[u]){var g=this.labels.edges[u][b],v=this.edges[g];v&&f.push(v.from)}f.indexOf(n.identity)>-1&&h++}}if(h<d)continue}if(!r||o){var p=l({},n);for(var y in p.edges){var m=p.edges[y],O=l({},this.edges[m]),w=this.nodes[O.through];O.through=l({},w),O.from=p,p.edges[y]=O}t.push(p)}}}return this.callback&&this.callback("query",t),t},a}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.Giraffe=r()}(this,function(){"use strict";function e(e){var r=e.id,t=e.label,i=e.data;if(!r&&0!==r)throw new Error("All Node's require an id");this.identity=r,this.properties=c({},i),this.labels=t?t.constructor===Array?t:[t]:[],this.edges=[]}function r(e){return f.indexOf(e.constructor)>-1}function t(e){var t=e.from,i=e.through,s=e.id,n=e.data,a=e.label;if(!a||!r(a))throw new Error("All Edges need a single Label");this.identity=s,this.from=t.identity,this.through=i.identity,this.label=a,this.properties=c({},n)}function i(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var r=16*Math.random()|0,t="x"===e?r:3&r|8;return t.toString(16)})}function s(e,r){var t=e.properties;for(var i in r)if("edges"!==i){if(!(i in t))return!1;if(t[i]!==r[i])return!1}return!0}function n(e,r){return Boolean(r&&e in r)}function a(e,r){if(!e||e.constructor!==Array)throw new Error("Objects needs to be an array");for(var t in e){var i=e[t];for(var s in r)if(!(s in i)||i[s].constructor!==r[s])throw new Error("Incorrect shape for "+JSON.stringify(i))}return!0}function o(e){var r=[];for(var t in e){var i=e[t],s=c({},l(i.from,this.nodes)),n=c({},l(i.through,this.nodes));r.push(c({},i,{from:s,through:n}))}return r}function l(e,r){for(var t in r){var i=r[t];if("identity"in i&&i.identity===e)return i}}function d(e,r){for(var t in r){var i=r[t];if("identity"in i&&i.identity===e)return t}return-1}function h(e,r){e&&e.constructor===Function&&!r&&(r=e,e={}),Object.defineProperty(this,"_ids",{value:[]}),Object.defineProperty(this,"_generateId",{value:function(){var e=i();return this._ids.indexOf(e)!==-1?this._generateId():(this._ids.push(e),e)}}),this.nodes=[],this.edges=[],this.labels={edges:{},nodes:{}},this.callback=r,n("nodes",e)&&a(e.nodes,u)&&(this.nodes=e.nodes),n("edges",e)&&a(e.edges,g)&&(this.edges=e.edges);for(var t in this.nodes){var s=this.nodes[t];if(this._ids.push(s.identity),s.labels.length)for(var o in s.labels){var l=s.labels[o];l in this.labels.nodes||(this.labels.nodes[l]=[]),this.labels.nodes[l].push(s.identity)}}for(var d in this.edges){var h=this.edges[d];if(this._ids.push(h.identity),!h.label)throw new Error("All Edges need a label");var c=h.label;c in this.labels.edges||(this.labels.edges[c]=[]),this.labels.edges[c].push(h.identity)}}var c=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e},u={identity:Number,properties:Object,labels:Array,edges:Array},f=[String,Number],g={identity:Number,properties:Object,label:String,from:Number,through:Number};return h.prototype.create=function(r,t){r.constructor===Object&&(t=r,r=null);var i=this._generateId(),s=new e({id:i,label:r,data:t});if(this.nodes.push(s),r){var n=this.labels.nodes;r in n||(n[r]=[]),n[r].push(s.identity)}return this.callback&&this.callback("create",s),s},h.prototype.update=function e(e,r,t){e.constructor!==Array&&(e=[e]),r.constructor===Object&&(t=r,r=null),null!==r&&r.constructor!==Array&&(r=[r]),t||(t={});for(var i in e){var s=e[i],n="label"in s;if(n&&r)throw new TypeError("Edge Labels cannot be changed.");r&&(s.labels=s.labels.concat(r)),s.properties=c({},s.properties,t)}return this.callback&&this.callback("update",e),e},h.prototype.remove=function(e){e.constructor!==Array&&(e=[e]);for(var r in e){var t=e[r],i=t.identity;for(var s in t.edges){var n=t.edges[s],a=l(n,this.edges),o=a.label,h=a.identity;if(o in this.labels.edges){var c=this.labels.edges[o].indexOf(h);c!==-1&&this.labels.edges[o].splice(c,1)}this.edges.splice(d(n,this.edges),1)}for(var u in t.labels){var o=t.labels[u];if(o in this.labels.nodes){var f=this.labels.nodes[o].indexOf(i);f!==-1&&this.labels.nodes[o].splice(f,1)}}for(var g in this.edges){var b=this.edges[g];if(b&&b.through===i){var v=this.nodes[b.from];if(v){var p=v.edges.indexOf(b.identity);p!==-1&&(v.edges.splice(p,1),this.edges[g]=void 0)}}}this.nodes.splice(d(i,this.nodes),1)}this.callback&&this.callback("remove")},h.prototype.edge=function(e,r,i,s){e.constructor!==Array&&(e=[e]),r.constructor!==Array&&(r=[r]);var n=[];for(var a in e){var l=e[a];for(var d in r){var h=r[d],c=this._generateId(),u=new t({id:c,label:i,data:s,from:l,through:h}),f=this.labels.edges;i in f||(f[i]=[]),f[i].push(u.identity),l.edges.push(c),this.edges.push(u),n.push(u)}}return this.callback&&this.callback("edge",o.call(this,n)),n},h.prototype.query=function(e,r){e||r||(e=r=null),e&&e.constructor===Object&&(r=e,e=null);var t=[];for(var i in this.nodes){var n=this.nodes[i];if(n&&(!e||n.labels.indexOf(e)!==-1)){var a=s(n,r),o=r&&"edges"in r;if(o){var d=r.edges.length,h=0;for(var u in r.edges){var f=r.edges[u],g=[];if(f in this.labels.edges){for(var b in this.labels.edges[f]){var v=this.labels.edges[f][b],p=l(v,this.edges);p&&g.push(p.from)}g.indexOf(n.identity)>-1&&h++}}if(h<d)continue}if(!r||a){var y=c({},n);for(var x in y.edges){var m=y.edges[x],O=c({},l(m,this.edges)),w=l(O.through,this.nodes);O.through=c({},w),O.from=y,y.edges[x]=O}t.push(y)}}}return this.callback&&this.callback("query",t),t},h}); |
{ | ||
"name": "giraffe", | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"description": "Lightweight Graph DB implementation", | ||
@@ -36,3 +36,3 @@ "main": "dist/bundle.js", | ||
"babel-register": "^6.18.0", | ||
"eslint": "^3.12.2", | ||
"eslint": "^3.13.1", | ||
"eslint-config-tbremer": "^1.2.1", | ||
@@ -42,8 +42,8 @@ "expect": "^1.20.2", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^0.38.0", | ||
"rollup": "^0.41.1", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-node-resolve": "^2.0.0", | ||
"rollup-plugin-uglify": "^1.0.1", | ||
"rollup-watch": "^2.5.0" | ||
"rollup-watch": "^3.2.2" | ||
} | ||
} |
@@ -101,3 +101,3 @@ Giraffe | ||
{ | ||
identity: Number, | ||
identity: <UUID />, | ||
properties: Object, | ||
@@ -116,3 +116,3 @@ labels: Array, | ||
{ | ||
identity: Number, | ||
identity: <UUID />, | ||
from: <Node Identity /> || <Node />, | ||
@@ -119,0 +119,0 @@ through: <Node Identity /> || <Node />, |
import Node, { shape as nodeShape } from './Node'; | ||
import Edge, { shape as edgeShape } from './Edge'; | ||
import { checkProperties, lookForKey, ensureObjectsShape, buildEdges } from './lib'; | ||
import guid from './ID'; | ||
import { checkProperties, lookForKey, ensureObjectsShape, buildEdges, findById, findIndexById } from './lib'; | ||
@@ -11,2 +12,18 @@ export default function Giraffe(dataset, callback) { | ||
Object.defineProperty(this, '_ids', { value: [] }); | ||
Object.defineProperty( | ||
this, | ||
'_generateId', | ||
{ | ||
value: function generateId() { | ||
const id = guid(); | ||
if (this._ids.indexOf(id) !== -1) return this._generateId(); | ||
this._ids.push(id); | ||
return id; | ||
} | ||
}); | ||
this.nodes = []; | ||
@@ -29,2 +46,3 @@ this.edges = []; | ||
/** | ||
* Add id's to db._ids array | ||
* Build up this.labels object for Nodes | ||
@@ -34,2 +52,3 @@ */ | ||
const node = this.nodes[idx]; | ||
this._ids.push(node.identity); | ||
if (!node.labels.length) continue; | ||
@@ -46,2 +65,3 @@ | ||
/** | ||
* Add id's to db._ids array | ||
* Build up this.labels object for Edges | ||
@@ -51,2 +71,3 @@ */ | ||
const edge = this.edges[idx]; | ||
this._ids.push(edge.identity); | ||
if (!edge.label) throw new Error('All Edges need a label'); | ||
@@ -66,3 +87,3 @@ const label = edge.label; | ||
const id = this.nodes.length; | ||
const id = this._generateId(); | ||
const node = new Node({ id, label, data }); | ||
@@ -121,3 +142,3 @@ | ||
const edgeId = node.edges[e]; | ||
const edge = this.edges[edgeId]; | ||
const edge = findById(edgeId, this.edges); | ||
const { label, identity } = edge; | ||
@@ -133,3 +154,3 @@ | ||
*/ | ||
this.edges[edgeId] = undefined; | ||
this.edges.splice(findIndexById(edgeId, this.edges), 1); | ||
} | ||
@@ -178,3 +199,3 @@ | ||
*/ | ||
this.nodes[identity] = undefined; | ||
this.nodes.splice(findIndexById(identity, this.nodes), 1); | ||
} | ||
@@ -196,3 +217,3 @@ | ||
const _through = through[t]; | ||
const id = this.edges.length; | ||
const id = this._generateId(); | ||
const edg = new Edge({ id, label, data, from: _from, through: _through }); | ||
@@ -252,3 +273,3 @@ const labelObj = this.labels.edges; | ||
const id = this.labels.edges[edgeName][idFromLabel]; | ||
const edge = this.edges[id]; | ||
const edge = findById(id, this.edges); | ||
@@ -276,6 +297,7 @@ if (!edge) continue; //edges can be undefined from the `remove` method. | ||
for (const edgeIdx in returnObj.edges) { | ||
const edgeId = returnObj.edges[edgeIdx]; | ||
const edge = Object.assign({}, this.edges[edgeId]); | ||
const throughNode = this.nodes[edge.through]; | ||
const edge = Object.assign({}, findById(edgeId, this.edges)); | ||
const throughNode = findById(edge.through, this.nodes); | ||
@@ -282,0 +304,0 @@ edge.through = Object.assign({}, throughNode); |
@@ -57,4 +57,4 @@ export function checkProperties(node, properties) { | ||
const edge = edges[idx]; | ||
const from = Object.assign({}, this.nodes[edge.from]); // eslint-disable-line | ||
const through = Object.assign({}, this.nodes[edge.through]); // eslint-disable-line | ||
const from = Object.assign({}, findById(edge.from, this.nodes)); // eslint-disable-line | ||
const through = Object.assign({}, findById(edge.through, this.nodes)); // eslint-disable-line | ||
@@ -66,1 +66,28 @@ built.push(Object.assign({}, edge, { from, through })); | ||
} | ||
/** | ||
* Search a group for an object containing the proper identity | ||
*/ | ||
export function findById(id, group) { | ||
for (const idx in group) { | ||
const node = group[idx]; | ||
if ('identity' in node && node.identity === id) return node; | ||
} | ||
} | ||
/** | ||
* Given an ID and a group | ||
* return it's index | ||
* or -1 | ||
* based on IDs | ||
*/ | ||
export function findIndexById(id, group) { | ||
for (const idx in group) { | ||
const node = group[idx]; | ||
if ('identity' in node && node.identity === id) return idx; | ||
} | ||
return (-1); | ||
} |
import expect from 'expect'; | ||
import { | ||
buildEdges, | ||
checkProperties, | ||
lookForKey, | ||
ensureObjectsShape, | ||
buildEdges | ||
findById, | ||
findIndexById, | ||
lookForKey | ||
} from './'; | ||
@@ -112,2 +114,49 @@ | ||
}); | ||
describe('findById', () => { | ||
const foo = { | ||
identity: 123, | ||
foo: 'bar' | ||
}; | ||
const nodes = [ | ||
{ identity: 0, a: 'b' }, | ||
{ identity: 1, c: 'd' }, | ||
{ identity: 2, e: 'f' }, | ||
foo, | ||
{ identity: 3, g: 'h' }, | ||
]; | ||
expect(findById(123, nodes)).toEqual(foo); | ||
}); | ||
describe('findIndexById', () => { | ||
it('returns correct id', () => { | ||
const foo = { | ||
identity: 123, | ||
foo: 'bar' | ||
}; | ||
const nodes = [ | ||
{ identity: 0, a: 'b' }, | ||
{ identity: 1, c: 'd' }, | ||
{ identity: 2, e: 'f' }, | ||
foo, | ||
{ identity: 3, g: 'h' }, | ||
]; | ||
expect(findIndexById(123, nodes)).toEqual(3); | ||
}); | ||
it('returns -1', () => { | ||
const nodes = [ | ||
{ identity: 0, a: 'b' }, | ||
{ identity: 1, c: 'd' }, | ||
{ identity: 2, e: 'f' }, | ||
{ identity: 3, g: 'h' }, | ||
]; | ||
expect(findIndexById(123, nodes)).toEqual(-1); | ||
}); | ||
}); | ||
}); |
@@ -191,2 +191,29 @@ import expect, { createSpy } from 'expect'; | ||
}); | ||
describe('tracks IDs', () => { | ||
it('has an IDs key', () => { | ||
expect(db).toIncludeKey('_ids'); | ||
expect(db._ids).toBeAn('array'); | ||
}); | ||
it('has a generateId function', () => { | ||
expect(db).toIncludeKey('_generateId'); | ||
expect(db._generateId).toBeAn('function'); | ||
}); | ||
it('hidden props do not show up on enumerations', () => { | ||
expect(Object.keys(db)).toExclude('_ids'); | ||
expect(Object.keys(db)).toExclude('_generateId'); | ||
}); | ||
it('increments with each creation', () => { | ||
expect(db._ids.length).toBe(0); | ||
const cat = db.create({ name: 'Cat' }); | ||
expect(db._ids.length).toBe(1); | ||
const dog = db.create({ name: 'Dog' }); | ||
expect(db._ids.length).toBe(2); | ||
db.edge(cat, dog, relationship); | ||
expect(db._ids.length).toBe(3); | ||
}); | ||
}); | ||
}); | ||
@@ -207,3 +234,2 @@ | ||
expect(node).toInclude({ | ||
identity: 0, | ||
labels: [ 'Animal' ], | ||
@@ -216,11 +242,2 @@ properties: { | ||
}); | ||
it('nodes increment properly', () => { | ||
const nodeA = db.create(label, { name: 'Fido', type: 'dog' }); | ||
const nodeB = db.create(label, { name: 'Fido', type: 'dog' }); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(nodeA.identity).toEqual(0); | ||
expect(nodeB.identity).toEqual(1); | ||
}); | ||
}); | ||
@@ -244,5 +261,4 @@ | ||
expect(edge).toInclude({ | ||
identity: 0, | ||
from: 0, | ||
through: 1, | ||
from: nodeA.identity, | ||
through: nodeB.identity, | ||
label: 'CHASES', | ||
@@ -288,3 +304,3 @@ properties: {} | ||
it('nodes returned in query', () => { | ||
it('nodes returned', () => { | ||
db.create(label, { name: 'Cat' }); | ||
@@ -295,5 +311,5 @@ db.create(label, { name: 'Dog' }); | ||
const results = db.query(label, { name: 'CatDog' }); | ||
const [ res ] = results; | ||
expect(results.length).toEqual(1); | ||
expect(results[0]).toInclude({ | ||
identity: 2, | ||
expect(res).toInclude({ | ||
labels: [ label ], | ||
@@ -304,3 +320,3 @@ properties: { name: 'CatDog' } | ||
it('edges returned in search', () => { | ||
it('edges returned', () => { | ||
const cat = db.create(label, { name: 'Cat' }); | ||
@@ -326,3 +342,2 @@ const dog = db.create(label, { name: 'Dog' }); | ||
label: 'CHASES', | ||
identity: 0, | ||
from: cat, | ||
@@ -392,3 +407,3 @@ through: dog | ||
const nodeA = db.create(label, { name: 'Dog' }); | ||
db.create(label, { name: 'Cat' }); | ||
const nodeB = db.create(label, { name: 'Cat' }); | ||
@@ -399,4 +414,4 @@ expect(db.nodes.length).toEqual(2); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.nodes[0]).toEqual(undefined); | ||
expect(db.nodes.length).toEqual(1); | ||
expect(db.nodes[0]).toEqual(nodeB); | ||
}); | ||
@@ -414,18 +429,17 @@ | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.edges.length).toEqual(1); | ||
expect(db.nodes[0]).toEqual(undefined); | ||
expect(db.edges[0]).toEqual(undefined); | ||
expect(db.nodes.length).toEqual(1); | ||
expect(db.edges.length).toEqual(0); | ||
expect(db.nodes[0]).toEqual(nodeB); | ||
}); | ||
it('removes reference to nodes in labels.nodes', () => { | ||
db.create(label, { name: 'Cat' }); | ||
const node = db.create(label, { name: 'Dog' }); | ||
const nodeA = db.create(label, { name: 'Cat' }); | ||
const nodeB = db.create(label, { name: 'Dog' }); | ||
db.remove(node); | ||
db.remove(nodeB); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.nodes.length).toEqual(1); | ||
expect(db.nodes[1]).toEqual(undefined); | ||
expect(db.labels.nodes[label].length).toEqual(1); | ||
expect(db.labels.nodes[label][0]).toEqual(0); | ||
expect(db.labels.nodes[label][0]).toEqual(nodeA.identity); | ||
}); | ||
@@ -440,4 +454,4 @@ | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.nodes[0]).toEqual(undefined); | ||
expect(db.nodes.length).toEqual(1); | ||
expect(db.nodes[0]).toEqual(dog); | ||
expect(db.labels.edges[relationship].length).toEqual(0); | ||
@@ -444,0 +458,0 @@ expect(db.labels.edges[relationship][0]).toEqual(undefined); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60041
17
1388