function Mapper(value: *, nodePath: List, tree: Iterable): *
A function required as an argument for the deepMap
functions.
The value to update.
The current node's NodePath
, a List
of keys used to uniquely identify the current node.
The original tree
passed into the map function.
The replacement value.
An Array
or List
of keys indicating where to find each node's children from within each node.
This works in the same way as the keyPath
parameter in Immutable's getIn()
method.
It also accepts null in place of an empty Array
or List
.
// this tree would have a ChildPath of ['children'], as each node's children can be found in the 'children' iterable const tree = fromJS({ name: "root", children: [ { name: "child 1", children: [ {name: "grandchild 1"}, {name: "grandchild 2"} ] }, { name: "child 2", children: [] }, {name: "child 3"} ] }); // nested maps and lists like these don't require a ChildPath, as each Iterable contains its own children const nestedMaps = fromJS({ a: { b: "B", c: "C", d: { e: "E" } }, f: "F" }); const nestedLists = fromJS([ ["A","B","C"], ["D",["E","F"]], "G" );
function InputFunction(tree: Iterable): Iterable
InputFunction is a partially applied function returned from all 'deep' functions in immutable-recursive. It accepts an iterable, the tree of data you wish to call your deep function on, and returns the result of the deep function you're calling.
For example, one way to use deepGet
is to call it, passing in any parameters required, then call the returned function, passing in your data tree.
const nestedMaps = fromJS({
a: {
b: "B",
c: "C",
d: {
e: "E"
}
},
f: "F"
});
return deepGet(['a','b'])(nestedMaps); // returns "B"
This design of using a partially applied function allows immutable-recursive's functions to be chained easily with Immutable,
by using them inside of an update()
method. This is the preferred usage pattern as it retains chainability.
return fromJS(someData)
.get('tree')
.update(deepGet(['a','b'])) // using deepGet in a chain
.set('done', true)
.toJS();
You can also define a function to perform a specific operation, and use it multiple times by passing in different tree data.
// make every node's name uppercase
const uppercaseNames = deepMap((node) => {
return node.update('name', (name) => name.toUpperCase());
}, ['children']);
const a = fromJS(aDataTree).update(uppercaseNames);
const b = fromJS(aDifferentDataTree).update(uppercaseNames);
The tree Iterable to be processed by one of the deep functions.
The modified iterable.
function Filterer(value: *, nodePath: List, tree: Iterable): *
A function required as an argument for the deepFilter
functions.
The value to filter.
The current node's NodePath
, a List
of keys used to uniquely identify the current node.
The original tree
passed into the map function.
The replacement value.
When passed as a parameter, NodePath
can be a List
or an Array
of keys used to uniquely identify a node.
Almost always used in conjunction with a ChildPath
.
Also accepts null in place of an empty Array
or List
.
Whenever NodePath
is returned from a function in the immutable-recursive library,
it will always be a List
.
const tree = fromJS({ name: "root", children: [ { name: "child 1", children: [ {name: "grandchild 1"}, {name: "grandchild 2"} ] } {name: "child 2"} ] }); // The NodePath for "root" is [] (or null) // The NodePath for "child 1" is [0] // The NodePath for "grandchild 2" is [0,1] const nestedMaps = fromJS({ a: { b: "B", c: "C", d: { e: "E" } }, f: "F" }); // The NodePath for "A" is ['a'] // The NodePath for "B" is ['a','b'] // The NodePath for "E" is ['a','d','e']
function Updater(value: *): *
A function required as an argument for the deepUpdate
functions.
The value to update.
The replacement value.
function Reducer(reduction: *, value: *, nodePath: List, tree: Iterable): *
A function required as an argument for the deepReduce
functions.
The current reduction.
The current value.
The current node's NodePath
, a List
of keys used to uniquely identify the current node.
The original tree
passed into the map function.
The new reduced value.
function asList(input: Array|List|null): List
Accepts an Array
, List
or null
and returns an equivalent List
. Passing in null
will return an empty List
.
asList(null); // returns List() asList([0,1,2]); // returns List().of(0,1,2) asList(List().of(0,1,2)); // returns List().of(0,1,2)
The input to be convert to a List
. List
items will pass through unchanged, all others will be passed into a List
constructor.
The equivalent List
.
function blankNode(node: *, childPath?: ChildPath): *
Accepts a node and returns a copy of it with all children and all non-childPath
keys removed.
Essentially the minimum data structure required for the node.
The node to make blank.
An Array or List of keys indicating where to find the node's children.
The blank node.
function nodePathToKeys(nodePath: NodePath, childPath?: ChildPath): List
Turns a node's nodePath
and its childPath
into a full key path.
nodePathToKeys(null, ['children']); // returns [] nodePathToKeys(['bob'], ['children']); // returns ['children', 'bob'] nodePathToKeys([0,1], ['children']); // returns ['children', 0, 'children', 1]
A NodePath used to uniquely identify a node.
An Array or List of keys indicating where to find each node's children from within each node.
function nodePathToKeysChildren(nodePath: NodePath, childPath?: ChildPath): List
Turns a node's nodePath and its childPath into a full key path to the node's children.
nodePathToKeysChildren(null, ['children']); // returns ['children'] nodePathToKeysChildren(['bob'], ['children']); // returns ['children', 'bob', children'] nodePathToKeysChildren([0,1], ['children']); // returns ['children', 0, 'children', 1, 'children']
A NodePath used to uniquely identify a node.
An Array or List of keys indicating where to find each node's children from within each node.
function isLeaf(node: *, childPath?: ChildPath): boolean
Accepts a node and returns a boolean indicating if the node is a leaf node (i.e. it has no children).
const tree = fromJS({ name: "root", children: [ { name: "child 1", children: [ {name: "grandchild 1"}, {name: "grandchild 2"} ] }, { name: "child 2", children: [] } ] }); isLeaf(tree.getIn(['children', 0]), ['childPath']); // returns false because child 1 has children isLeaf(tree.getIn(['children', 1]), ['childPath']); // returns true because child 2 has no children // note that normally deepGet is recommended instead of getIn for getting nodes from a tree
The node to check.
An Array or List of keys indicating where to find each node's children from within each node.
A boolean indicating if the node is a leaf node.
function pick(iterable: Iterable, keys: Array): Iterable
Filter an Immutable Iterable
so it only retains any keys provided in the keys
param.
Similar to the pick function in Lodash.
const map = Map({ a: "A", b: "B", c: "C", d: "D" }); pick(map, ['a', 'c']); // returns Map({a: "A", c: "C"});
The Iterable
to filter.
The iterable's keys to keep.
function deepFilter(filterer: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through nodes in the provided tree, passing them through a filter
function.
Only nodes which return a truthy value from the filter
function will be present in the modified tree.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
The function to be called for each node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepGet(nodePath: NodePath, childPath?: ChildPath, notSetValue?: *): (tree: Iterable) => Iterable
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.
A NodePath
used to identify the node to return.
An Array
or List
of keys indicating where to find each node's children from within each node.
A value to return when there is no node corresponding to nodePath
.
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 deepGetChildren(nodePath: NodePath, childPath?: ChildPath, notSetValue?: *): (tree: Iterable) => Iterable
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()
.
A NodePath
used to identify the node to return.
An Array
or List
of keys indicating where to find each node's children from within each node.
A value to return when there is no node corresponding to nodePath
, or if the node doesn't have any children.
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 deepMap(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through all nodes in the provided tree, passing them all through a mapper
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, inward from leaves to the root node.
Sibling nodes are processed in the order that Immutable iterates through collections.
TODO: note about care with child collections, note about using as reduce
The function to be called for every node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepMapLeaves(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the leaf nodes in the provided tree, passing them all through a mapper
function. Leaf nodes are nodes that have no child nodes.
Nodes are processed branch by branch, in the order that Immutable maps through the child iterables.
The function to be called for every leaf node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepMapParents(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the parent nodes in the provided tree, passing them all through a mapper
function. Parent nodes are nodes that have child nodes.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, inward from leaves to the root node.
The function to be called for every parent node in the tree. the results of which will be used to create the modified iterable.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepMapOutwards(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through all nodes in the provided tree, passing them all through a mapper
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
Because this moves from the root node outwards this function behaves quite differently to the other deep map functions.
mapper
function.mapper
function.The function to be called for every node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepMapLeavesOutwards(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the leaf nodes in the provided tree, passing them all through a mapper
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
The function to be called for every leaf node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepMapParentsOutwards(mapper: (value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the parent nodes in the provided tree, passing them all through a mapper
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
Because this moves from the root node outwards this function behaves quite differently to the other deep map functions.
mapper
function.mapper
function.The function to be called for every parent node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepPick(nodePath: NodePath, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree containing only nodes lying on the supplied nodePath
, with all nodes and children outside of the nodePath
removed.
A NodePath
whose nodes will be filtered and returned.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduce(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through all nodes in the provided tree, passing them all through a reducer
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, inward from leaves to the root node.
Sibling nodes are processed in the order that Immutable iterates through collections.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduceLeaves(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the leaf nodes in the provided tree, passing them all through a reducer
function. Leaf nodes are nodes that have no child nodes.
Nodes are processed branch by branch, in the order that Immutable maps through the child iterables.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every leaf node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduceParents(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the parent nodes in the provided tree, passing them all through a reducer
function. Parent nodes are nodes that have child nodes.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, inward from leaves to the root node.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every parent node in the tree. the results of which will be used to create the modified iterable.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduceOutwards(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through all nodes in the provided tree, passing them all through a reducer
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduceLeavesOutwards(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the leaf nodes in the provided tree, passing them all through a reducer
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
Because this moves from the root node outwards this function behaves quite differently to the other deep map functions.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every leaf node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepReduceParentsOutwards(reducer: (reduction: *, value: *, nodePath: List, tree: Iterable) => *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this iterates through the parent nodes in the provided tree, passing them all through a reducer
function.
Nodes are processed branch by branch in the order that Immutable maps through the child iterables, outwards from the root node to the leaf nodes.
Sibling nodes are processed in the order that Immutable iterates through collections.
Because this moves from the root node outwards this function behaves quite differently to the other deep map functions.
Unlike Immutable.js, if the initialReduction
is not provided or is null, then the initialReduction will be null
.
The function to be called for every parent node in the tree, the results of which will be used to create the modified tree.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepSet(nodePath: NodePath, value: *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree containing the new node value
at the location determined by nodePath
.
If an equivalent node already exists in this tree it will be replaced.
If any keys on nodePath
do not exist, a new Immutable Map
will be created at the missing key.
Be aware that by setting values on deep nodes that don't yet exist,
this may cause Maps
to be created for child Iterables
where other types of Iterables
may be used for children elsewhere in the tree.
This will be addressed in a future release.
When no childPath
is provided then this is functionally equivalent to Immutable's setIn()
method.
A NodePath used to uniquely identify the node to set.
The value to set the node to.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepSetChildren(nodePath: NodePath, value: *, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree where the children Iterable
of the node at nodePath
are set to value
.
If no node exists at nodePath
then an Immutable Map
will be created at that location.
This is intended to be used with a childPath
, but if no childPath
is provided
then this is functionally equivalent to both Immutable's setIn()
method and deepSet()
.
A NodePath
used to uniquely identify a node whose children will be set.
The value to set the node to.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepUpdate(nodePath: NodePath, updater: (value: *) => *, childPath?: ChildPath, notSetValue?: *): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree with the node at nodePath
set to the return value of updater
.
If any keys on nodePath
do not exist, a new Immutable Map
will be created at the missing key.
If the node at nodePath
doesn't exist, updater
will be called with the value of notSetValue
.
Be aware that by updating values on deep nodes that don't yet exist,
this may cause Maps
to be created for child Iterables
where other types of Iterables
may be used for children elsewhere in the tree.
This will be addressed in a future release.
This is intended to be used with a childPath
, but if no childPath
is provided
then this is functionally equivalent to Immutable's updateIn()
method.
A NodePath
used to uniquely identify the node to update.
The function to update the node, which is passed the existing node and must return the updated value of the node.
An Array
or List
of keys indicating where to find each node's children from within each node.
When there is no node corresponding to nodePath
, this value is passed into updater
.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepUpdateChildren(nodePath: NodePath, updater: (value: *) => *, childPath?: ChildPath, notSetValue?: *): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree with the children of the node at nodePath
set to the return value of updater
.
If no childPath
is used there will be no change if the node at nodePath
is not Iterable
.
If no node exists at nodePath
then an Immutable Map
will be created at that location.
If the node at nodePath
doesn't exist, updater
will be called with the value of notSetValue
.
This is intended to be used with a childPath
, but if no childPath
is provided
then this is functionally equivalent to both Immutable's 'updateIn()method and
deepUpdate()`.
A NodePath
used to uniquely identify a node whose children will be updated.
The function to update the node, which is passed the existing node's children and must return the updated value of the node's children.
An Array
or List
of keys indicating where to find each node's children from within each node.
When there is no node corresponding to nodePath
, this value is passed into updater
.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepDelete(nodePath: NodePath, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree that no longer contains the node at nodePath
, or any of its child nodes.
If there is no node at nodePath
then no change will occur.
When no childPath
is provided then this is functionally equivalent to Immutable's deleteIn()
method.
A NodePath used to uniquely identify the node to delete.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.
function deepDeleteChildren(nodePath: NodePath, childPath?: ChildPath): (tree: Iterable) => Iterable
Once fully applied, this returns a new tree where the children of the node at nodePath
have been deleted. The child container will still remain.
This is the equivalent of calling the following:
deepUpdateChildren(nodePath, kids => kids.clear(), childPath);
No change will occur if any of the following circumstances are true:
nodePath
childPath
is provided and the node has no child Iterable
childPath
is provided and the node itself is not Iterable
If no childPath
is provided and the node is Iterable
, then this has the same effect as calling clear()
on the node.
A NodePath
used to uniquely identify a node whose children will be deleted.
An Array
or List
of keys indicating where to find each node's children from within each node.
A partially applied function which accepts a single tree Iterable
, and returns the modified tree Iterable
.