findNode function

RouterNode findNode(
  1. RouterNode node,
  2. String targetRawPath
)

Implementation

RouterNode findNode(RouterNode node, String targetRawPath) {
  final targetRealPath = removeGroup(targetRawPath);

  final currentRealPath = node.path;
  final currentRawPath = node.rawPath;

  if (currentRealPath == targetRealPath && currentRawPath == targetRawPath) {
    return node;
  }

  final nextRelativeRawPath = targetRawPath
      .substring(currentRawPath.length)
      .replaceAll(RegExp(r'^/'), '');

  final nextRawPathStep = nextRelativeRawPath.split('/')[0];

  final nextRawPath =
      '${node.rawPath.replaceAll(RegExp(r'/$'), '')}/$nextRawPathStep';

  final nextRealPath = removeGroup(nextRawPath);

  var match = node.children
      .where((n) => nextRealPath.startsWith(n.path) && n.rawPath == nextRawPath)
      .firstOrNull;

  if (match == null) {
    match = RouterNode(nextRealPath)..rawPath = nextRawPath;
    node.children.add(match);
  }

  return findNode(
    match,
    targetRawPath,
  );
}