From 9a6f141533b432323564202748ed3d34b4f6dbc5 Mon Sep 17 00:00:00 2001 From: Spedon Date: Tue, 28 Mar 2023 17:00:14 +0800 Subject: [PATCH] optimize reused code fragments --- assets/ts/utils.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/assets/ts/utils.ts b/assets/ts/utils.ts index 4fbff71..51a8f58 100644 --- a/assets/ts/utils.ts +++ b/assets/ts/utils.ts @@ -97,45 +97,45 @@ export const getDeviceType = (): deviceType => { return result } +const autoHide = (e: HTMLImageElement): void => { + e.style.visibility = 'hidden' + e.dataset.status = 'trail' +} + export const pushIndex = ( index: number, indexesArray: number[], stackDepth: number, imagesArray: NodeListOf, imagesArrayLen: number, - invert: boolean = false, - autoHide: boolean = true + invertFlag: boolean = false, + autoHideFlag: boolean = true ): number => { let indexesNum: number = indexesArray.length // create variable overflow to store the tail index let overflow: number - if (!invert) { - // push the tail index out and hide the image + if (!invertFlag) { + // if stack is full, push the tail index out and hide the image if (indexesNum === stackDepth) { // insert indexesArray.push(index) // pop out overflow = indexesArray.shift() as number // auto hide tail image - if (autoHide) { - imagesArray[overflow].style.visibility = 'hidden' - imagesArray[overflow].dataset.status = 'trail' - } + if (autoHideFlag) autoHide(imagesArray[overflow]) } else { indexesArray.push(index) indexesNum += 1 } } else { + // if stack is full, push the tail index out and hide the image if (indexesNum === stackDepth) { // insert indexesArray.unshift(calcImageIndex(index - stackDepth + 1, imagesArrayLen)) // pop out overflow = indexesArray.pop() as number // auto hide tail image - if (autoHide) { - imagesArray[overflow].style.visibility = 'hidden' - imagesArray[overflow].dataset.status = 'trail' - } + if (autoHideFlag) autoHide(imagesArray[overflow]) } else { indexesArray.unshift(calcImageIndex(index - indexesNum + 1, imagesArrayLen)) indexesNum += 1