MCPcopy Create free account
hub / github.com/angular/angular / mergeEmptyPathMatches

Function mergeEmptyPathMatches

packages/router/src/recognize.ts:578–612  ·  view source on GitHub ↗

* Finds `TreeNode`s with matching empty path route configs and merges them into `TreeNode` with * the children from each duplicate. This is necessary because different outlets can match a * single empty path route config and the results need to then be merged.

(
  nodes: Array<TreeNode<ActivatedRouteSnapshot>>,
)

Source from the content-addressed store, hash-verified

576 * single empty path route config and the results need to then be merged.
577 */
578function mergeEmptyPathMatches(
579 nodes: Array<TreeNode<ActivatedRouteSnapshot>>,
580): Array<TreeNode<ActivatedRouteSnapshot>> {
581 const result: Array<TreeNode<ActivatedRouteSnapshot>> = [];
582 // The set of nodes which contain children that were merged from two duplicate empty path nodes.
583 const mergedNodes: Set<TreeNode<ActivatedRouteSnapshot>> = new Set();
584
585 for (const node of nodes) {
586 if (!hasEmptyPathConfig(node)) {
587 result.push(node);
588 continue;
589 }
590
591 const duplicateEmptyPathNode = result.find(
592 (resultNode) => node.value.routeConfig === resultNode.value.routeConfig,
593 );
594 if (duplicateEmptyPathNode !== undefined) {
595 duplicateEmptyPathNode.children.push(...node.children);
596 mergedNodes.add(duplicateEmptyPathNode);
597 } else {
598 result.push(node);
599 }
600 }
601 // For each node which has children from multiple sources, we need to recompute a new `TreeNode`
602 // by also merging those children. This is necessary when there are multiple empty path configs
603 // in a row. Put another way: whenever we combine children of two nodes, we need to also check
604 // if any of those children can be combined into a single node as well.
605 for (const mergedNode of mergedNodes) {
606 const mergedChildren = mergeEmptyPathMatches(mergedNode.children);
607 result.push(new TreeNode(mergedNode.value, mergedChildren));
608 }
609 const merged = result.filter((n) => !mergedNodes.has(n));
610 checkOutletNameUniqueness(merged);
611 return merged;
612}
613
614function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
615 const names: {[k: string]: ActivatedRouteSnapshot} = Object.create(null);

Callers 1

processChildrenMethod · 0.85

Calls 7

hasEmptyPathConfigFunction · 0.85
addMethod · 0.65
hasMethod · 0.65
pushMethod · 0.45
findMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected