UNPKG

necto

Version:

Necto compliments Redux by providing a composable, declarative api to create flows through redux (Action -> Reducer or Action -> Saga). The intent of Necto is to reduce boilerplate, simplify and standardize action creators, and group action logic so that

50 lines (39 loc) 1.3 kB
// Implementation thanks to Chris Ferdinandi. // https://gomakethings.com/how-to-get-the-value-of-an-object-from-a-specific-path-with-vanilla-js/ const get = function(obj, path, def) { /** * If the path is a string, convert it to an array * @param {String|Array} path The path * @return {Array} The path array */ const stringToPath = function(path) { // If the path isn't a string, return it if (typeof path !== 'string') return path; // Create new array var output = []; // Split to an array with dot notation path.split('.').forEach(function(item, index) { // Split to an array with bracket notation item.split(/\[([^}]+)\]/g).forEach(function(key) { // Push to the new array if (key.length > 0) { output.push(key); } }); }); return output; }; // Get the path as an array path = stringToPath(path); // Cache the current object var current = obj; // For each item in the path, dig into the object for (var i = 0; i < path.length; i++) { // If the item isn't found, return the default (or null) if (!current[path[i]]) return def; // Otherwise, update the current value current = current[path[i]]; } return current; }; export default get;