2026-01-14 17:31:59 +07:00
|
|
|
// Add tất cả sp trong data product vào 1 mảng
|
2026-02-05 17:22:56 +07:00
|
|
|
import { productList } from '@/data/products/productList';
|
2026-01-14 17:31:59 +07:00
|
|
|
|
|
|
|
|
export function getAllProducts() {
|
2026-01-28 17:26:02 +07:00
|
|
|
return productList.flatMap((group: any) => group.list);
|
2026-01-14 17:31:59 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 17:04:10 +07:00
|
|
|
|
|
|
|
|
export function formatTextList(
|
|
|
|
|
text?: string | any[],
|
2026-01-30 17:09:41 +07:00
|
|
|
limit?: number | undefined
|
2026-01-16 17:04:10 +07:00
|
|
|
) {
|
|
|
|
|
if (!text) return '';
|
|
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
// server: trả raw HTML
|
|
|
|
|
return Array.isArray(text)
|
|
|
|
|
? String(text[1] ?? '')
|
|
|
|
|
: String(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const html = Array.isArray(text)
|
|
|
|
|
? String(text[1] ?? '')
|
|
|
|
|
: String(text);
|
|
|
|
|
|
|
|
|
|
if (!html) return '';
|
|
|
|
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
|
const doc = parser.parseFromString(html, 'text/html');
|
|
|
|
|
|
|
|
|
|
const nodes = Array.from(
|
|
|
|
|
doc.body.querySelectorAll('p, div, li')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
|
.slice(0, limit)
|
|
|
|
|
.map(node => `<div class="item">${node.innerHTML}</div>`)
|
|
|
|
|
.join('');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 17:31:59 +07:00
|
|
|
// Format giá
|
|
|
|
|
export function formatPrice(amount: number) {
|
|
|
|
|
return amount.toLocaleString('vi-VN');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tính % giảm giá
|
|
|
|
|
export function calculateDiscount(
|
|
|
|
|
price: number,
|
|
|
|
|
marketPrice: number
|
|
|
|
|
) {
|
|
|
|
|
if (price <= 0 || marketPrice <= price) return 0;
|
|
|
|
|
return Math.ceil(((marketPrice - price) / marketPrice) * 100);
|
2026-01-27 17:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:26:02 +07:00
|
|
|
// format thời gian
|
|
|
|
|
export function formatArticleTime(article_time: string) {
|
2026-01-27 17:02:26 +07:00
|
|
|
let day: string;
|
|
|
|
|
let month: string;
|
|
|
|
|
let year: string;
|
|
|
|
|
|
|
|
|
|
if (article_time.toLowerCase().includes('hôm nay')) {
|
2026-01-28 17:26:02 +07:00
|
|
|
const time = new Date();
|
|
|
|
|
day = (time.getDate() <= 9) ? '0' + time.getDate() : String(time.getDate());
|
|
|
|
|
month = (time.getMonth() + 1 <= 9) ? '0' + (time.getMonth() + 1) : String(time.getMonth() + 1);
|
|
|
|
|
year = String(time.getFullYear());
|
2026-01-27 17:02:26 +07:00
|
|
|
} else {
|
2026-01-28 17:26:02 +07:00
|
|
|
day = article_time.substring(0, 2);
|
|
|
|
|
month = article_time.substring(3, 5);
|
|
|
|
|
year = article_time.substring(6, 10);
|
2026-01-27 17:02:26 +07:00
|
|
|
}
|
|
|
|
|
return `${day}/${month}/${year}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 16:42:40 +07:00
|
|
|
export function formatDate(a:any){
|
|
|
|
|
let dateObj = new Date(parseInt(a)*1000);
|
|
|
|
|
|
|
|
|
|
let year = dateObj.getFullYear();
|
|
|
|
|
let month = ((dateObj.getMonth()+1) <= 9) ? '0' + (dateObj.getMonth()+1) : dateObj.getMonth()+1;
|
|
|
|
|
let date = (dateObj.getDate() < 10) ? '0' + dateObj.getDate() : dateObj.getDate();
|
|
|
|
|
let hour = (dateObj.getHours() < 10) ? '0' + dateObj.getHours() : dateObj.getHours();
|
|
|
|
|
let min = (dateObj.getMinutes() < 10) ? '0' + dateObj.getMinutes() : dateObj.getMinutes();
|
|
|
|
|
let time = `${date}/${month}/${year}, ${hour}:${min}`;
|
|
|
|
|
return time;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:26:02 +07:00
|
|
|
//
|
|
|
|
|
export function normalizeKey(str: string) {
|
|
|
|
|
return str
|
|
|
|
|
.normalize('NFD')
|
|
|
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.replace(/\s+/g, '-');
|
2026-01-30 17:09:41 +07:00
|
|
|
}
|