Skip to content

Commit

Permalink
fix(router): revert commit that replaced last helper with native `A…
Browse files Browse the repository at this point in the history
…rray.at(-1)` (#54021)

While `Array.at` is technically supported in all browsers we officially
support, the change was needlessly breaking without any real benefit.

PR Close #54021
  • Loading branch information
atscott authored and pkozlowski-opensource committed Jan 24, 2024
1 parent 9a9a00e commit f222bee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Expand Up @@ -1583,6 +1583,9 @@
{
"name": "last"
},
{
"name": "last3"
},
{
"name": "lastNodeWasCreated"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/create_url_tree.ts
Expand Up @@ -12,7 +12,7 @@ import {RuntimeErrorCode} from './errors';
import {ActivatedRouteSnapshot} from './router_state';
import {Params, PRIMARY_OUTLET} from './shared';
import {createRoot, squashSegmentGroup, UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree';
import {shallowEqual} from './utils/collection';
import {last, shallowEqual} from './utils/collection';


/**
Expand Down Expand Up @@ -187,7 +187,7 @@ class Navigation {
}

const cmdWithOutlet = commands.find(isCommandWithOutlets);
if (cmdWithOutlet && cmdWithOutlet !== commands.at(-1)) {
if (cmdWithOutlet && cmdWithOutlet !== last(commands)) {
throw new RuntimeError(
RuntimeErrorCode.MISPLACED_OUTLETS_COMMAND,
(typeof ngDevMode === 'undefined' || ngDevMode) &&
Expand Down
7 changes: 7 additions & 0 deletions packages/router/src/utils/collection.ts
Expand Up @@ -57,6 +57,13 @@ export function equalArraysOrString(a: string|string[], b: string|string[]) {
}
}

/**
* Return the last element of an array.
*/
export function last<T>(a: T[]): T|null {
return a.length > 0 ? a[a.length - 1] : null;
}

export function wrapIntoObservable<T>(value: T|Promise<T>|Observable<T>): Observable<T> {
if (isObservable(value)) {
return value;
Expand Down
3 changes: 2 additions & 1 deletion packages/router/src/utils/config_matching.ts
Expand Up @@ -15,6 +15,7 @@ import {runCanMatchGuards} from '../operators/check_guards';
import {defaultUrlMatcher, PRIMARY_OUTLET} from '../shared';
import {UrlSegment, UrlSegmentGroup, UrlSerializer} from '../url_tree';

import {last} from './collection';
import {getOrCreateRouteInjectorIfNeeded, getOutlet} from './config';

export interface MatchResult {
Expand Down Expand Up @@ -95,7 +96,7 @@ export function match(
function createWildcardMatchResult(segments: UrlSegment[]): MatchResult {
return {
matched: true,
parameters: segments.at(-1)?.parameters ?? {},
parameters: segments.length > 0 ? last(segments)!.parameters : {},
consumedSegments: segments,
remainingSegments: [],
positionalParamSegments: {},
Expand Down

0 comments on commit f222bee

Please sign in to comment.