From 1822051a786bf105c896dee546478ffd2ed7a27a Mon Sep 17 00:00:00 2001 From: puriphatt Date: Thu, 20 Jun 2024 05:01:03 +0000 Subject: [PATCH] feat: utils function move & delete array item --- src/stores/utils/index.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index e379134d..b84572e1 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -39,3 +39,33 @@ export function calculateAge(birthDate: Date | null | string) { return `${years} years ${months !== 0 ? months + ' months' : ''} ${days !== 0 ? days + ' days' : ''} `; } } + +export function moveItemUp(items: unknown[], index: number) { + if (index > 0) { + const temp = items[index]; + items[index] = items[index - 1]; + items[index - 1] = temp; + } +} + +export function moveItemDown(items: unknown[], index: number) { + if (index < items.length - 1) { + const temp = items[index]; + items[index] = items[index + 1]; + items[index + 1] = temp; + } +} + +export function deleteItem(items: unknown[], index: number) { + if (!items) return; + if (index >= 0 && index < items.length) { + items.splice(index, 1); + } +} + +export function formatNumberDecimal(num: number, point: number): string { + return num.toLocaleString('en-US', { + minimumFractionDigits: point, + maximumFractionDigits: point, + }); +}