OMD Documentation

omdStepVisualizerNodeUtils

This module provides a collection of utility functions specifically designed to assist with node operations and analysis within the context of step visualizations, particularly for identifying and extracting information from omdNode instances.

Class: omdStepVisualizerNodeUtils

This class is not meant to be instantiated. All its methods are static.

Static Methods

isLeafNode(node)

Checks if a given omdNode is a leaf node (e.g., omdConstantNode, omdVariableNode, omdOperatorNode).


isBinaryNode(node)

Checks if a given omdNode is an omdBinaryExpressionNode and has both left and right children.


isUnaryNode(node)

Checks if a given omdNode is an omdUnaryExpressionNode and has an argument child.


hasExpression(node)

Checks if a given omdNode has an expression property (common in nodes like omdParenthesisNode).


getNodeValue(node)

Attempts to retrieve a string representation of the node's value. This is particularly useful for leaf nodes (constants, variables) but can also provide a string for more complex nodes.


findLeafNodes(node)

Recursively finds all leaf nodes within the subtree rooted at the given node.


findVariableNodes(node)

Finds all omdVariableNode instances within the subtree rooted at the given node.


findConstantNodes(node)

Finds all omdConstantNode instances within the subtree rooted at the given node.


findLeafNodesWithValue(node, value)

Finds leaf nodes within a subtree that match a specific string value. It performs exact matches first, then numeric matches for constants, and finally partial string matches.


findAllNodes(node)

Recursively finds all omdNode instances (including non-leaf nodes) within the subtree rooted at the given node.


findRightmostNodeWithValue(node, value)

Finds the rightmost leaf node within a subtree that matches a specific value. It has special handling for nodes that are the right operand of a subtraction.

Example

import { omdStepVisualizerNodeUtils } from './omd/utils/omdStepVisualizerNodeUtils.js';
import { omdEquationNode } from './omd/nodes/omdEquationNode.js';

const equation = omdEquationNode.fromString('2x + 3 = 7');

// Find all leaf nodes
const leafNodes = omdStepVisualizerNodeUtils.findLeafNodes(equation);
console.log(leafNodes.map(node => node.toString())); // Output: ['2', 'x', '3', '7']

// Find all constant nodes
const constantNodes = omdStepVisualizerNodeUtils.findConstantNodes(equation);
console.log(constantNodes.map(node => node.getValue())); // Output: [2, 3, 7]

// Get the value of a specific node
const firstLeaf = leafNodes[0];
console.log(omdStepVisualizerNodeUtils.getNodeValue(firstLeaf)); // Output: '2'
↑ Top