findByPath method

TreeResult? findByPath(
  1. List<String> path
)

Implementation

TreeResult? findByPath(List<String> path) {
  TreeNode? currentNode = _root;
  var params = <String, String>{};
  TreeNode? finalNode;

  var count = path.length;

  for (var i = 0; i < count; i++) {
    if (currentNode == null) break;

    var value = path[i];

    var existsNode = currentNode._children[value];
    if (existsNode != null) {
      currentNode = existsNode;
    } else if (currentNode._children.length > 0 && currentNode.childIsPlaceholder) {
      currentNode = currentNode._children.values.first;
      params[currentNode.realKey] = value;
    } else {
      currentNode = null;
      break;
    }
    if (i == count - 1) {
      finalNode = currentNode.value == null ? null : currentNode;
    }
  }
  return finalNode == null ? null : TreeResult(params, finalNode);
}