Skip to content

Commit bc85551

Browse files
atscottpkozlowski-opensource
authored andcommitted
fix(router): revert commit that replaced last helper with native Array.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
1 parent aeaec86 commit bc85551

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,9 @@
15831583
{
15841584
"name": "last"
15851585
},
1586+
{
1587+
"name": "last3"
1588+
},
15861589
{
15871590
"name": "lastNodeWasCreated"
15881591
},

packages/router/src/create_url_tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {RuntimeErrorCode} from './errors';
1212
import {ActivatedRouteSnapshot} from './router_state';
1313
import {Params, PRIMARY_OUTLET} from './shared';
1414
import {createRoot, squashSegmentGroup, UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree';
15-
import {shallowEqual} from './utils/collection';
15+
import {last, shallowEqual} from './utils/collection';
1616

1717

1818
/**
@@ -187,7 +187,7 @@ class Navigation {
187187
}
188188

189189
const cmdWithOutlet = commands.find(isCommandWithOutlets);
190-
if (cmdWithOutlet && cmdWithOutlet !== commands.at(-1)) {
190+
if (cmdWithOutlet && cmdWithOutlet !== last(commands)) {
191191
throw new RuntimeError(
192192
RuntimeErrorCode.MISPLACED_OUTLETS_COMMAND,
193193
(typeof ngDevMode === 'undefined' || ngDevMode) &&

packages/router/src/utils/collection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export function equalArraysOrString(a: string|string[], b: string|string[]) {
5757
}
5858
}
5959

60+
/**
61+
* Return the last element of an array.
62+
*/
63+
export function last<T>(a: T[]): T|null {
64+
return a.length > 0 ? a[a.length - 1] : null;
65+
}
66+
6067
export function wrapIntoObservable<T>(value: T|Promise<T>|Observable<T>): Observable<T> {
6168
if (isObservable(value)) {
6269
return value;

packages/router/src/utils/config_matching.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {runCanMatchGuards} from '../operators/check_guards';
1515
import {defaultUrlMatcher, PRIMARY_OUTLET} from '../shared';
1616
import {UrlSegment, UrlSegmentGroup, UrlSerializer} from '../url_tree';
1717

18+
import {last} from './collection';
1819
import {getOrCreateRouteInjectorIfNeeded, getOutlet} from './config';
1920

2021
export interface MatchResult {
@@ -95,7 +96,7 @@ export function match(
9596
function createWildcardMatchResult(segments: UrlSegment[]): MatchResult {
9697
return {
9798
matched: true,
98-
parameters: segments.at(-1)?.parameters ?? {},
99+
parameters: segments.length > 0 ? last(segments)!.parameters : {},
99100
consumedSegments: segments,
100101
remainingSegments: [],
101102
positionalParamSegments: {},

0 commit comments

Comments
 (0)