topological-sort-group
Version:
Topological sorting and cycle detection. Optional grouping for parallel processing
12 lines (11 loc) • 662 B
JavaScript
// https://gist.github.com/andrewchilds/30a7fb18981d413260c7a36428ed13da
const isArray = Array.isArray || ((x)=>Object.prototype.toString.call(x) === '[object Array]');
export default function deepGet(value, query, defaultVal = undefined) {
const splitQuery = isArray(query) ? query : query.replace(/(\[(\d)\])/g, '.$2').replace(/^\./, '').split('.');
if (!splitQuery.length || splitQuery[0] === undefined) return value;
const key = splitQuery[0];
if (typeof value !== 'object' || value === null || !(key in value) || value[key] === undefined) {
return defaultVal;
}
return deepGet(value[key], splitQuery.slice(1), defaultVal);
}