navigateNode function

Node? navigateNode(
  1. Node? astNode,
  2. String path
)

Implementation

Node? navigateNode(Node? astNode, String path) {
  Node? node;
  if (astNode is ObjectNode) {
    final ObjectNode objectNode = astNode;
    PropertyNode? propertyNode;
    for (int i = 0; i < objectNode.children.length; i++) {
      final prop = objectNode.children[i];
      if (prop.key != null && prop.key?.value == path) {
        propertyNode = prop;
        break;
      }
    }
    if (propertyNode != null) {
      node = propertyNode.value;
    }
  }
  if (astNode is ArrayNode) {
    final ArrayNode arrayNode = astNode;
    final index = int.tryParse(path) ?? null;
    if (index != null && arrayNode.children.length > index) {
      node = arrayNode.children[index];
    }
  }
  return node;
}