From bdade7978a563d8f0fbecf50367080eed3a1c7a2 Mon Sep 17 00:00:00 2001 From: MarcoMadera Date: Mon, 4 Sep 2023 15:06:34 -0700 Subject: [PATCH] yesididntneedthatfix --- utils/applyLyricLinePositionAndColor.ts | 7 ++++--- utils/findIndexOrLast.ts | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 utils/findIndexOrLast.ts diff --git a/utils/applyLyricLinePositionAndColor.ts b/utils/applyLyricLinePositionAndColor.ts index 7eeb78eb..01c5b5ca 100644 --- a/utils/applyLyricLinePositionAndColor.ts +++ b/utils/applyLyricLinePositionAndColor.ts @@ -1,3 +1,4 @@ +import { findIndexOrLast } from "./findIndexOrLast"; import { LINE_HEIGHT, LYRICS_PADDING_LEFT, @@ -14,9 +15,9 @@ export function applyLyricLinePositionAndColor( containerHeight: number ): void { const containerMiddle = containerHeight / 2; - const currentLineIndex = Math.max( - allLines.findIndex((line) => line.type === "current"), - allLines.length - 1 + const currentLineIndex = findIndexOrLast( + allLines, + (line) => line.type === "current" ); allLines.forEach((line, index) => { diff --git a/utils/findIndexOrLast.ts b/utils/findIndexOrLast.ts new file mode 100644 index 00000000..9a31ffdf --- /dev/null +++ b/utils/findIndexOrLast.ts @@ -0,0 +1,7 @@ +export function findIndexOrLast( + arr: T[], + predicate: (element: T) => boolean +): number { + const index = arr.findIndex(predicate); + return index !== -1 ? index : arr.length - 1; +}