UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
8
9var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
10
11var _createClass2 = require("babel-runtime/helpers/createClass");
12
13var _createClass3 = _interopRequireDefault(_createClass2);
14
15var _error = require("./error");
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function generateFieldLookup(keys) {
20 var lookup = {};
21 keys.forEach(function (name, idx) {
22 lookup[name] = idx;
23 });
24 return lookup;
25}
26
27/**
28 * Records make up the contents of the {@link Result}, and is how you access
29 * the output of a statement. A simple statement might yield a result stream
30 * with a single record, for instance:
31 *
32 * MATCH (u:User) RETURN u.name, u.age
33 *
34 * This returns a stream of records with two fields, named `u.name` and `u.age`,
35 * each record represents one user found by the statement above. You can access
36 * the values of each field either by name:
37 *
38 * record.get("u.name")
39 *
40 * Or by it's position:
41 *
42 * record.get(0)
43 *
44 * @access public
45 */
46/**
47 * Copyright (c) 2002-2018 Neo4j Sweden AB [http://neo4j.com]
48 *
49 * This file is part of Neo4j.
50 *
51 * Licensed under the Apache License, Version 2.0 (the "License");
52 * you may not use this file except in compliance with the License.
53 * You may obtain a copy of the License at
54 *
55 * http://www.apache.org/licenses/LICENSE-2.0
56 *
57 * Unless required by applicable law or agreed to in writing, software
58 * distributed under the License is distributed on an "AS IS" BASIS,
59 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
60 * See the License for the specific language governing permissions and
61 * limitations under the License.
62 */
63
64var Record = function () {
65 /**
66 * Create a new record object.
67 * @constructor
68 * @access private
69 * @param {string[]} keys An array of field keys, in the order the fields appear in the record
70 * @param {Array} fields An array of field values
71 * @param {Object} fieldLookup An object of fieldName -> value index, used to map
72 * field names to values. If this is null, one will be
73 * generated.
74 */
75 function Record(keys, fields) {
76 var fieldLookup = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
77 (0, _classCallCheck3.default)(this, Record);
78
79 this.keys = keys;
80 this.length = keys.length;
81 this._fields = fields;
82 this._fieldLookup = fieldLookup || generateFieldLookup(keys);
83 }
84
85 /**
86 * Run the given function for each field in this record. The function
87 * will get three arguments - the value, the key and this record, in that
88 * order.
89 *
90 * @param {function(value: Object, key: string, record: Record)} visitor the function to apply to each field.
91 */
92
93
94 (0, _createClass3.default)(Record, [{
95 key: "forEach",
96 value: function forEach(visitor) {
97 for (var i = 0; i < this.keys.length; i++) {
98 visitor(this._fields[i], this.keys[i], this);
99 }
100 }
101
102 /**
103 * Generates an object out of the current Record
104 *
105 * @returns {Object}
106 */
107
108 }, {
109 key: "toObject",
110 value: function toObject() {
111 var object = {};
112 this.forEach(function (value, key) {
113 object[key] = value;
114 });
115
116 return object;
117 }
118
119 /**
120 * Get a value from this record, either by index or by field key.
121 *
122 * @param {string|Number} key Field key, or the index of the field.
123 * @returns {*}
124 */
125
126 }, {
127 key: "get",
128 value: function get(key) {
129 var index = void 0;
130 if (!(typeof key === "number")) {
131 index = this._fieldLookup[key];
132 if (index === undefined) {
133 throw (0, _error.newError)("This record has no field with key '" + key + "', available key are: [" + this.keys + "].");
134 }
135 } else {
136 index = key;
137 }
138
139 if (index > this._fields.length - 1 || index < 0) {
140 throw (0, _error.newError)("This record has no field with index '" + index + "'. Remember that indexes start at `0`, " + "and make sure your statement returns records in the shape you meant it to.");
141 }
142
143 return this._fields[index];
144 }
145
146 /**
147 * Check if a value from this record, either by index or by field key, exists.
148 *
149 * @param {string|Number} key Field key, or the index of the field.
150 * @returns {boolean}
151 */
152
153 }, {
154 key: "has",
155 value: function has(key) {
156 // if key is a number, we check if it is in the _fields array
157 if (typeof key === "number") {
158 return key >= 0 && key < this._fields.length;
159 }
160
161 // if it's not a number, we check _fieldLookup dictionary directly
162 return this._fieldLookup[key] !== undefined;
163 }
164 }]);
165 return Record;
166}();
167
168exports.default = Record;
\No newline at end of file