UNPKG

1.7 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @emails oncall+relay
8 *
9 * @format
10 */
11'use strict';
12
13var invariant = require("fbjs/lib/invariant");
14
15function getValueAtPath(data, path) {
16 var result = data;
17 var _iteratorNormalCompletion = true;
18 var _didIteratorError = false;
19 var _iteratorError = undefined;
20
21 try {
22 for (var _iterator = path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
23 var key = _step.value;
24
25 if (result == null) {
26 return null;
27 }
28
29 if (typeof key === 'number') {
30 !Array.isArray(result) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected an array when extracting value at path. ' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
31 result = result[key];
32 } else {
33 !(typeof result === 'object' && !Array.isArray(result)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected an object when extracting value at path. ' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
34 result = result[key];
35 }
36 }
37 } catch (err) {
38 _didIteratorError = true;
39 _iteratorError = err;
40 } finally {
41 try {
42 if (!_iteratorNormalCompletion && _iterator["return"] != null) {
43 _iterator["return"]();
44 }
45 } finally {
46 if (_didIteratorError) {
47 throw _iteratorError;
48 }
49 }
50 }
51
52 return result;
53}
54
55module.exports = getValueAtPath;
\No newline at end of file