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) {
  const min = Math.ceil(min);
  const max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}

export function addLeadingZero(num, size) {
    num = num.toString();
    while (num.length < size) num = "0" + num;
    return num;
}
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 не будет опубликован. Обязательные поля помечены *