bbo
Version:
bbo is a utility library of zero dependencies for javascript.
49 lines (35 loc) • 823 B
JavaScript
import './get_tag.js';
import isArray from './is_array.js';
import isString from './is_string.js';
import isSymbol from './is_symbol.js';
function get(obj, propsArg, defaultValue) {
if (!obj) {
return defaultValue;
}
var props;
var prop;
if (Array.isArray(propsArg)) {
props = propsArg.slice(0);
}
if (isString(propsArg)) {
props = propsArg.split('.');
}
if (isSymbol(propsArg)) {
props = [propsArg];
}
if (!isArray(props)) {
throw new Error('props arg must be an array, a string or a symbol');
}
while (props.length) {
prop = props.shift();
if (!obj) {
return defaultValue;
} // eslint-disable-next-line no-param-reassign
obj = obj[prop];
if (obj === undefined) {
return defaultValue;
}
}
return obj;
}
export default get;