UNPKG

9.84 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.isNode = isNode;
9exports.isRelationship = isRelationship;
10exports.isUnboundRelationship = isUnboundRelationship;
11exports.isPath = isPath;
12exports.isPathSegment = isPathSegment;
13exports.PathSegment = exports.Path = exports.UnboundRelationship = exports.Relationship = exports.Node = void 0;
14
15var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
16
17var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
18
19/**
20 * Copyright (c) 2002-2019 "Neo4j,"
21 * Neo4j Sweden AB [http://neo4j.com]
22 *
23 * This file is part of Neo4j.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 */
37var IDENTIFIER_PROPERTY_ATTRIBUTES = {
38 value: true,
39 enumerable: false,
40 configurable: false,
41 writable: false
42};
43var NODE_IDENTIFIER_PROPERTY = '__isNode__';
44var RELATIONSHIP_IDENTIFIER_PROPERTY = '__isRelationship__';
45var UNBOUND_RELATIONSHIP_IDENTIFIER_PROPERTY = '__isUnboundRelationship__';
46var PATH_IDENTIFIER_PROPERTY = '__isPath__';
47var PATH_SEGMENT_IDENTIFIER_PROPERTY = '__isPathSegment__';
48
49function hasIdentifierProperty(obj, property) {
50 return (obj && obj[property]) === true;
51}
52/**
53 * Class for Node Type.
54 */
55
56
57var Node =
58/*#__PURE__*/
59function () {
60 /**
61 * @constructor
62 * @protected
63 * @param {Integer} identity - Unique identity
64 * @param {Array<string>} labels - Array for all labels
65 * @param {Object} properties - Map with node properties
66 */
67 function Node(identity, labels, properties) {
68 (0, _classCallCheck2["default"])(this, Node);
69
70 /**
71 * Identity of the node.
72 * @type {Integer}
73 */
74 this.identity = identity;
75 /**
76 * Labels of the node.
77 * @type {string[]}
78 */
79
80 this.labels = labels;
81 /**
82 * Properties of the node.
83 * @type {Object}
84 */
85
86 this.properties = properties;
87 }
88 /**
89 * @ignore
90 */
91
92
93 (0, _createClass2["default"])(Node, [{
94 key: "toString",
95 value: function toString() {
96 var s = '(' + this.identity;
97
98 for (var i = 0; i < this.labels.length; i++) {
99 s += ':' + this.labels[i];
100 }
101
102 var keys = Object.keys(this.properties);
103
104 if (keys.length > 0) {
105 s += ' {';
106
107 for (var _i = 0; _i < keys.length; _i++) {
108 if (_i > 0) s += ',';
109 s += keys[_i] + ':' + JSON.stringify(this.properties[keys[_i]]);
110 }
111
112 s += '}';
113 }
114
115 s += ')';
116 return s;
117 }
118 }]);
119 return Node;
120}();
121
122exports.Node = Node;
123Object.defineProperty(Node.prototype, NODE_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
124/**
125 * Test if given object is an instance of {@link Node} class.
126 * @param {Object} obj the object to test.
127 * @return {boolean} `true` if given object is a {@link Node}, `false` otherwise.
128 */
129
130function isNode(obj) {
131 return hasIdentifierProperty(obj, NODE_IDENTIFIER_PROPERTY);
132}
133/**
134 * Class for Relationship Type.
135 */
136
137
138var Relationship =
139/*#__PURE__*/
140function () {
141 /**
142 * @constructor
143 * @protected
144 * @param {Integer} identity - Unique identity
145 * @param {Integer} start - Identity of start Node
146 * @param {Integer} end - Identity of end Node
147 * @param {string} type - Relationship type
148 * @param {Object} properties - Map with relationship properties
149 */
150 function Relationship(identity, start, end, type, properties) {
151 (0, _classCallCheck2["default"])(this, Relationship);
152
153 /**
154 * Identity of the relationship.
155 * @type {Integer}
156 */
157 this.identity = identity;
158 /**
159 * Identity of the start node.
160 * @type {Integer}
161 */
162
163 this.start = start;
164 /**
165 * Identity of the end node.
166 * @type {Integer}
167 */
168
169 this.end = end;
170 /**
171 * Type of the relationship.
172 * @type {string}
173 */
174
175 this.type = type;
176 /**
177 * Properties of the relationship.
178 * @type {Object}
179 */
180
181 this.properties = properties;
182 }
183 /**
184 * @ignore
185 */
186
187
188 (0, _createClass2["default"])(Relationship, [{
189 key: "toString",
190 value: function toString() {
191 var s = '(' + this.start + ')-[:' + this.type;
192 var keys = Object.keys(this.properties);
193
194 if (keys.length > 0) {
195 s += ' {';
196
197 for (var i = 0; i < keys.length; i++) {
198 if (i > 0) s += ',';
199 s += keys[i] + ':' + JSON.stringify(this.properties[keys[i]]);
200 }
201
202 s += '}';
203 }
204
205 s += ']->(' + this.end + ')';
206 return s;
207 }
208 }]);
209 return Relationship;
210}();
211
212exports.Relationship = Relationship;
213Object.defineProperty(Relationship.prototype, RELATIONSHIP_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
214/**
215 * Test if given object is an instance of {@link Relationship} class.
216 * @param {Object} obj the object to test.
217 * @return {boolean} `true` if given object is a {@link Relationship}, `false` otherwise.
218 */
219
220function isRelationship(obj) {
221 return hasIdentifierProperty(obj, RELATIONSHIP_IDENTIFIER_PROPERTY);
222}
223/**
224 * Class for UnboundRelationship Type.
225 * @access private
226 */
227
228
229var UnboundRelationship =
230/*#__PURE__*/
231function () {
232 /**
233 * @constructor
234 * @protected
235 * @param {Integer} identity - Unique identity
236 * @param {string} type - Relationship type
237 * @param {Object} properties - Map with relationship properties
238 */
239 function UnboundRelationship(identity, type, properties) {
240 (0, _classCallCheck2["default"])(this, UnboundRelationship);
241
242 /**
243 * Identity of the relationship.
244 * @type {Integer}
245 */
246 this.identity = identity;
247 /**
248 * Type of the relationship.
249 * @type {string}
250 */
251
252 this.type = type;
253 /**
254 * Properties of the relationship.
255 * @type {Object}
256 */
257
258 this.properties = properties;
259 }
260 /**
261 * Bind relationship
262 *
263 * @protected
264 * @param {Integer} start - Identity of start node
265 * @param {Integer} end - Identity of end node
266 * @return {Relationship} - Created relationship
267 */
268
269
270 (0, _createClass2["default"])(UnboundRelationship, [{
271 key: "bind",
272 value: function bind(start, end) {
273 return new Relationship(this.identity, start, end, this.type, this.properties);
274 }
275 /**
276 * @ignore
277 */
278
279 }, {
280 key: "toString",
281 value: function toString() {
282 var s = '-[:' + this.type;
283 var keys = Object.keys(this.properties);
284
285 if (keys.length > 0) {
286 s += ' {';
287
288 for (var i = 0; i < keys.length; i++) {
289 if (i > 0) s += ',';
290 s += keys[i] + ':' + JSON.stringify(this.properties[keys[i]]);
291 }
292
293 s += '}';
294 }
295
296 s += ']->';
297 return s;
298 }
299 }]);
300 return UnboundRelationship;
301}();
302
303exports.UnboundRelationship = UnboundRelationship;
304Object.defineProperty(UnboundRelationship.prototype, UNBOUND_RELATIONSHIP_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
305/**
306 * Test if given object is an instance of {@link UnboundRelationship} class.
307 * @param {Object} obj the object to test.
308 * @return {boolean} `true` if given object is a {@link UnboundRelationship}, `false` otherwise.
309 */
310
311function isUnboundRelationship(obj) {
312 return hasIdentifierProperty(obj, UNBOUNT_RELATIONSHIP_IDENTIFIER_PROPERTY);
313}
314/**
315 * Class for PathSegment Type.
316 */
317
318
319var PathSegment =
320/**
321 * @constructor
322 * @protected
323 * @param {Node} start - start node
324 * @param {Relationship} rel - relationship that connects start and end node
325 * @param {Node} end - end node
326 */
327function PathSegment(start, rel, end) {
328 (0, _classCallCheck2["default"])(this, PathSegment);
329
330 /**
331 * Start node.
332 * @type {Node}
333 */
334 this.start = start;
335 /**
336 * Relationship.
337 * @type {Relationship}
338 */
339
340 this.relationship = rel;
341 /**
342 * End node.
343 * @type {Node}
344 */
345
346 this.end = end;
347};
348
349exports.PathSegment = PathSegment;
350Object.defineProperty(PathSegment.prototype, PATH_SEGMENT_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
351/**
352 * Test if given object is an instance of {@link PathSegment} class.
353 * @param {Object} obj the object to test.
354 * @return {boolean} `true` if given object is a {@link PathSegment}, `false` otherwise.
355 */
356
357function isPathSegment(obj) {
358 return hasIdentifierProperty(obj, PATH_SEGMENT_IDENTIFIER_PROPERTY);
359}
360/**
361 * Class for Path Type.
362 */
363
364
365var Path =
366/**
367 * @constructor
368 * @protected
369 * @param {Node} start - start node
370 * @param {Node} end - end node
371 * @param {Array<PathSegment>} segments - Array of Segments
372 */
373function Path(start, end, segments) {
374 (0, _classCallCheck2["default"])(this, Path);
375
376 /**
377 * Start node.
378 * @type {Node}
379 */
380 this.start = start;
381 /**
382 * End node.
383 * @type {Node}
384 */
385
386 this.end = end;
387 /**
388 * Segments.
389 * @type {Array<PathSegment>}
390 */
391
392 this.segments = segments;
393 /**
394 * Length of the segments.
395 * @type {Number}
396 */
397
398 this.length = segments.length;
399};
400
401exports.Path = Path;
402Object.defineProperty(Path.prototype, PATH_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
403/**
404 * Test if given object is an instance of {@link Path} class.
405 * @param {Object} obj the object to test.
406 * @return {boolean} `true` if given object is a {@link Path}, `false` otherwise.
407 */
408
409function isPath(obj) {
410 return hasIdentifierProperty(obj, PATH_IDENTIFIER_PROPERTY);
411}
\No newline at end of file