Simplify implementation of firstOrUndefined and lastOrUndefined (#22572)

This commit is contained in:
Andy
2018-03-14 12:40:48 -07:00
committed by GitHub
parent 5f1668e53d
commit 1736741e54
+2 -2
View File
@@ -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 {