76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
// Add tất cả sp trong data product vào 1 mảng
|
|
import { productList } from '@/data/products';
|
|
|
|
export function getAllProducts() {
|
|
return productList.flatMap((group:any) => group.list);
|
|
}
|
|
|
|
|
|
export function formatTextList(
|
|
text?: string | any[],
|
|
limit = 5
|
|
) {
|
|
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('');
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
}
|
|
|
|
export function formatArticleTime(article_time:string) {
|
|
let day: string;
|
|
let month: string;
|
|
let year: string;
|
|
|
|
if (article_time.toLowerCase().includes('hôm nay')) {
|
|
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());
|
|
} else {
|
|
day = article_time.substring(0,2);
|
|
month = article_time.substring(3,5);
|
|
year = article_time.substring(6,10);
|
|
}
|
|
return `${day}/${month}/${year}`;
|
|
}
|
|
|
|
|