Utilities for Rogain.
Creates a tree node of the frame
type with locals
as scope data and tree
as children. This represents a new scope, no variables fall through by default – all locals should be manually defined from outside scopes.
var frame = createFrame(tree, locals);
// { type: 'frame', children: tree, locals: locals }
Splits an array of trees at matching tree.
treeList
Array of trees.
match
Object.
match
is used to segment the input array between each matching tree.
var branches = splitTree(tree.children, {
type: 'component',
name: 'Else'
});
var passing = branches[0];
var failing = branches[1];
note will return an array of tree branches in the format [ [ trees ], [ trees ] ]
even on a single tree result like, [ [ tree ] ]
Finds the first tree matching the match
object in treeList
.
treeList
Array of trees. Haystack of trees.
match
Object. Needle tree.
var res = find(tree.children, { type: 'component', name: 'Else' });
Finds all trees matching the match
object in treeList
.
treeList
Array of trees. Haystack of trees.
match
Object. Needle tree.
var res = findAll(tree.children, { type: 'tag', tagName: 'a' });
Returns object path for first tree matching match
object. Will treverse deep.
tree
Rogain tree. Haystack.
match
Object. Needle.
initialPath
String. starting path used when building path.
//<Box><div><Block /><Children /></div></Box>
var path = findPath(Box, { name: 'Children' });
// children.0.children.1
Predicate, returns true when tree has children.
tree
Tree.
if (hasChildren(tree)) {
// do children stuff
}
With npm do:
npm install rogain-utils
MIT