mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Simplify implementation of firstOrUndefined and lastOrUndefined (#22572)
This commit is contained in:
@@ -1077,7 +1077,7 @@ namespace ts {
|
||||
* Returns the first element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function firstOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
|
||||
return elementAt(array, 0);
|
||||
return array.length === 0 ? undefined : array[0];
|
||||
}
|
||||
|
||||
export function first<T>(array: ReadonlyArray<T>): T {
|
||||
@@ -1089,7 +1089,7 @@ namespace ts {
|
||||
* Returns the last element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function lastOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
|
||||
return elementAt(array, -1);
|
||||
return array.length === 0 ? undefined : array[array.length - 1];
|
||||
}
|
||||
|
||||
export function last<T>(array: ReadonlyArray<T>): T {
|
||||
|
||||
Reference in New Issue
Block a user