export const capitalizeFirstLetter = (string) => {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

export const numberWithSpaces = (value) => {
    return value
        .toString()
        .replace(/[^0-9]/g, "")
        .replace(/\B(?=(\d{3})+(?!\d))/g, " ");
};

export const formatBytes = (bytes, decimals) => {
    if (bytes == 0) return "0 Bytes";
    var k = 1024,
        dm = decimals || 2,
        sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
        i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};

export const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

// remove not numbers & not dots
// remove after precision
// remove leading zeros
export function function cleanAsFloat(value, precision) {
    return (
        value
            .replace(/[^\d.]/, "") 
            .replace(new RegExp(`^(\\d+.\\d{0,${precision}})(.*)`), "$1")
            .replace(/^(0*)(\d+)$/, "$2") || 0
    );
}

export function getISODate(date) {
    return new Date(date + " UTC").toISOString().substr(0, 10);
}


export function getNoun(number, one, two, five) {
    let n = Math.abs(number);
    n %= 100;
    if (n >= 5 && n <= 20) {
        return five;
    }
    n %= 10;
    if (n === 1) {
        return one;
    }
    if (n >= 2 && n <= 4) {
        return two;
    }
    return five;
}

export function isOutsideOf(el, selector) {
    return $(el).closest(selector).length === 0;
}


export function getRandomInt(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}

export function addLeadingZero(num, size) {
    num = num.toString();
    while (num.length < size) num = "0" + num;
    return num;
}


export const getThisWeekDates = () => {
    const today = new Date();
    const dayOfWeek = today.getDay();

    let monday = new Date(today);
    monday.setDate(today.getDate() - dayOfWeek + 1);

    let sunday = new Date(today);
    sunday.setDate(monday.getDate() + 6);

    return [monday, sunday];
};

export const getThisMonthDates = () => {
    const today = new Date();

    let firstDay = new Date(today);
    firstDay.setDate(1);

    let lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);

    return [firstDay, lastDay];
};

export const getThisYearDates = () => {
    const today = new Date();

    let firstDay = new Date(today.getFullYear(), 0, 1);

    let lastDay = new Date(today.getFullYear(), 12, 0);

    return [firstDay, lastDay];
};

export function getShortRuDate(date) {
    const day = date.getDate();
    const month = date.getMonth() + 1;

    return `${addLeadingZero(day, 2)}.${addLeadingZero(month, 2)}`;
}

export function addVarsToRoot() {
    const update = () => {
        const scrollbarWidth = xobotyi.scrollbarWidth();
        document.documentElement.style.setProperty(
            "--scrollbar-width",
            `${scrollbarWidth}px`
        );
    };

    window.addEventListener("resize", update);
    window.addEventListener("orientationchange", update);

    update();
}
import hexRgb from "hex-rgb";

export function hexToRgba(hex, opacity) {
    return `rgba(${hexRgb(hex, { format: "array" })
        .slice(0, 3)
        .join(", ")}, ${opacity})`;
}
Рубрики: модуль

0 комментариев

Добавить комментарий

Avatar placeholder

Ваш адрес email не будет опубликован. Обязательные поля помечены *