UNPKG

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