deepGet.js

/**
 * @module deepGet
 */

/**
 * Gets a specific node from the tree. When no childPath is provided then this is functionally equivalent to Immutable's getIn function.
 *
 * @param {Array|List} keys An {Array} or {List} of keys used to identify a node.
 * @param {Array|List} [childPath=null] An {Array} or {List} of the key path to each node's children.
 * @param [notSetValue=null] A value to return when there is no node corresponding to keys
 * @return {inputFunction} The node at the specified path of keys, or if no node exists then notSetValue will be returned
 */

function deepGet(keys, childPath, notSetValue = null) {
  return (tree) => tree.getIn(keysToPath(keys, childPath), notSetValue);
}

/**
 * Gets the children of a specific node from the tree.
 *
 * @param {Array|List} keys An {Array} or {List} of keys used to identify nested nodes.
 * @param {Array|List} [childPath=null] An {Array} or {List} of the key path to each node's children.
 * @param [notSetValue=null] A value to return when there is no node corresponding to keys, or if the node doesn't have any children
 * @return {inputFunction} The children of the node at the specified path of keys, or if no node or children exist then notSetValue will be returned
 */

function deepGetChildren(keys, childPath, notSetValue = null) {
  return (tree) => tree.getIn(keysToPathChildren(keys, childPath), notSetValue);
}

export {
  deepGet,
  deepGetChildren
}