This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
reValuate/node_modules/fbjs/lib/getByPath.js.flow

53 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2021-04-02 02:24:13 +03:00
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule getByPath
* @flow
* @typechecks
*/
'use strict';
/**
* Get a value from an object based on the given path
*
* Usage example:
*
* var obj = {
* a : {
* b : 123
* }
* };
*
* var result = getByPath(obj, ['a', 'b']); // 123
*
* You may also specify the path using an object with a path field
*
* var result = getByPath(obj, {path: ['a', 'b']}); // 123
*
* If the path doesn't exist undefined will be returned
*
* var result = getByPath(obj, ['x', 'y', 'z']); // undefined
*/
function getByPath(root
/*?Object | Error*/
: any, path: Array<string>, fallbackValue?: any): any {
let current = root;
for (let i = 0; i < path.length; i++) {
const segment = path[i]; // Use 'in' to check entire prototype chain since immutable js records
// use prototypes
if (current && segment in current) {
current = current[segment];
} else {
return fallbackValue;
}
}
return current;
}
module.exports = getByPath;