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
This commit is contained in:
Andrew Scott
2024-01-22 09:12:44 -08:00
committed by Pawel Kozlowski
parent aeaec8629c
commit bc85551bfc
4 changed files with 14 additions and 3 deletions
@@ -1583,6 +1583,9 @@
{
"name": "last"
},
{
"name": "last3"
},
{
"name": "lastNodeWasCreated"
},
+2 -2
View File
@@ -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';
/**
@@ -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) &&
+7
View File
@@ -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;
+2 -1
View File
@@ -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 {
@@ -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: {},