loigicUtils.ts 895 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export const formatNumber = (val: number) => {
  2. return new Intl.NumberFormat("vi-VN").format(val);
  3. };
  4. export const formatCarriers = (text) => {
  5. if (!text) return "";
  6. return text.replace(/,\s*/g, ", ");
  7. };
  8. export const setWithExpiry = (
  9. key: string,
  10. value: string,
  11. ttlMs?: number | null
  12. ) => {
  13. const now = Date.now();
  14. const item = {
  15. value,
  16. expiry: ttlMs ? now + ttlMs : now + 10 * 60 * 1000, // Default 10 minutes
  17. };
  18. localStorage.setItem(key, JSON.stringify(item));
  19. };
  20. export const getWithExpiry = <T>(key: string): T | null => {
  21. const itemStr = localStorage.getItem(key);
  22. if (!itemStr) return null;
  23. try {
  24. const item = JSON.parse(itemStr);
  25. if (!item.expiry || Date.now() > item.expiry) {
  26. localStorage.removeItem(key);
  27. return null;
  28. }
  29. return item.value as T;
  30. } catch {
  31. localStorage.removeItem(key);
  32. return null;
  33. }
  34. };