get.js

import {
	nodePathToKeys,
	nodePathToKeysChildren
} from './utils';

/**
 * @module get
 */

/**
 * Once fully applied, this returns a specific node from the tree.
 * When no `childPath` is provided then this is functionally equivalent to Immutable's `getIn()` function.
 *
 * @param {NodePath} nodePath A `NodePath` used to identify the node to return.
 * @param {ChildPath} [childPath=null] An `Array` or `List` of keys indicating where to find each node's children from within each node.
 * @param {*} [notSetValue=null] A value to return when there is no node corresponding to `nodePath`.
 * @return {InputFunction} A partially applied function which accepts a single tree `Iterable`. `InputFunction` will return the node at the specified `nodePath`, or if no node exists then `notSetValue` will be returned.
 */

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

/**
 * Once fully applied, this returns the children of a specific node from the tree.
 *
 * This is intended to be used with a `childPath`, but if no `childPath` is provided
 * then this is functionally equivalent to Immutable's `getIn()` function or `deepMap()`.
 *
 * @param {NodePath} nodePath A `NodePath` used to identify the node to return.
 * @param {ChildPath} [childPath=null] An `Array` or `List` of keys indicating where to find each node's children from within each node.
 * @param {*} [notSetValue=null] A value to return when there is no node corresponding to `nodePath`, or if the node doesn't have any children.
 * @return {InputFunction} A partially applied function which accepts a single tree `Iterable`. `InputFunction` will return the children of the node at the specified `nodePath`, or if no node or children exist then `notSetValue` will be returned
 */

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

export {
  deepGet,
  deepGetChildren
}