File "index.ts"

Full Path: /var/www/html/gitep_front/src/shared/lib/index.ts
File size: 2.12 KB
MIME-type: text/x-java
Charset: utf-8

import dayjs from "dayjs";


export const copyText = async (text: string) => {
    try {
        if (navigator.clipboard && navigator.clipboard.writeText) {
            await navigator.clipboard.writeText(text);
            return;
        }

        const textArea = document.createElement('textarea');
        textArea.value = text;
        textArea.style.position = 'fixed';
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();

        try {
            const success = document.execCommand('copy');
            if (!success) {
                throw new Error('Copy command failed');
            }
        } finally {
            document.body.removeChild(textArea);
        }
    } catch (err) {
        console.error('Ошибка копирования:', err);
        throw err;
    }
};

export const formatDate = (dateString: string, format = "DD.MM.YYYY HH:mm") => {
    if (!dateString) return "";
    return dayjs(dateString).format(format);
};

export const formatNumber = (value: string | number | null) => {
  if (value === null) return '0,00';

  const cleanedValue = typeof value === 'string' ? value.replace(/\s+/g, '') : value;
  const num = Number(cleanedValue);

  return isNaN(num)
    ? '0,00'
    : new Intl.NumberFormat('ru-RU', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }).format(Math.abs(num));
};

export const formatWholeSum = (value: string | number | null) => {
  if (value === null) return '0';

  const cleanedValue = typeof value === 'string' ? value.replace(/\s+/g, '') : value;
  const num = Number(cleanedValue);

  return isNaN(num)
    ? '0'
    : new Intl.NumberFormat('ru-RU', {
        maximumFractionDigits: 0,
      }).format(Math.abs(num));
};

export const formatNumberDDS = (value: string | number | null) => {
  if (value === null) return '-';

  const cleanedValue = typeof value === 'string' ? value.replace(/\s+/g, '') : value;
  const num = Number(cleanedValue);

  return isNaN(num)
    ? '-'
    : new Intl.NumberFormat('ru-RU', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }).format(Math.abs(num));
};