Skip to content

Commit 7f64687

Browse files
committed
Added Path class
1 parent 2c2867f commit 7f64687

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

neo4j.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
NODE = 0x4E,
3737
RELATIONSHIP = 0x52,
38+
UNBOUND_RELATIONSHIP = 0x72,
3839
PATH = 0x50,
3940

4041
TINY_TEXT = 0x80,
@@ -114,6 +115,62 @@
114115
return s;
115116
}
116117
}
118+
119+
function UnboundRelationship(identity, type, properties) {
120+
this.identity = identity;
121+
this.type = type;
122+
this.properties = properties;
123+
124+
this.bind = function bind(start, end) {
125+
return new Relationship(identity, start, end, type, properties);
126+
}
127+
128+
this.toString = function toString() {
129+
var s = "-[:" + this.type;
130+
var keys = Object.keys(this.properties);
131+
if (keys.length > 0) {
132+
s += " {";
133+
for(var i = 0; i < keys.length; i++) {
134+
if (i > 0) s += ",";
135+
s += keys[i] + ":" + JSON.stringify(this.properties[keys[i]]);
136+
}
137+
s += "}";
138+
}
139+
s += "]->";
140+
return s;
141+
}
142+
}
143+
144+
function Path(nodes, rels, sequence) {
145+
var last_node = nodes[0],
146+
entities = [last_node];
147+
for (var i = 0; i < sequence.length; i += 2) {
148+
var rel_index = sequence[i],
149+
next_node = nodes[sequence[2 * i + 1]],
150+
rel;
151+
if (rel_index > 0) {
152+
rel = hydrate(rels[rel_index - 1]);
153+
} else {
154+
rel = hydrate(rels[-rel_index - 1]);
155+
}
156+
entities.push(rel.bind(next_node, last_node))
157+
entities.push(next_node)
158+
}
159+
160+
this.nodes = [entities[0]];
161+
this.relationships = [];
162+
for (var i = 1; i < entities.length; i++) {
163+
if (i % 2 == 1) {
164+
this.nodes.push(entities[i]);
165+
} else {
166+
this.relationships.push(entities[i]);
167+
}
168+
}
169+
170+
this.toString = function toString() {
171+
return "<Path>";
172+
}
173+
}
117174

118175
function hydrate(x) {
119176
if (Array.isArray(x)) {
@@ -129,6 +186,12 @@
129186
case RELATIONSHIP:
130187
x = new Relationship(fields[0], fields[1], fields[2], fields[3], fields[4]);
131188
break;
189+
case UNBOUND_RELATIONSHIP:
190+
x = new UnboundRelationship(fields[0], fields[1], fields[2]);
191+
break;
192+
case PATH:
193+
x = new Path(fields[0], fields[1], fields[2]);
194+
break;
132195
}
133196
}
134197
return x;
@@ -344,7 +407,7 @@
344407

345408
function Unpacker(data) {
346409
var p = 0,
347-
view = new DataView(data.buffer);
410+
view = new DataView(data.buffer),
348411
decoder = new TextDecoder();
349412

350413
function read() {
@@ -598,7 +661,7 @@
598661
}
599662

600663
var ws = new WebSocket("ws://localhost:7688/"),
601-
msg = new Message(ws);
664+
msg = new Message(ws),
602665
packer = new Packer(msg);
603666
ws.onmessage = function(event) {
604667
var reader = new FileReader();

0 commit comments

Comments
 (0)