feat: utils function move & delete array item

This commit is contained in:
puriphatt 2024-06-20 05:01:03 +00:00
parent fc002b9ca0
commit 1822051a78

View file

@ -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,
});
}