2020-08-25 21:08:42 +03:00
|
|
|
import * as Constants from "~/common/constants";
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
const MINUTE = 60;
|
|
|
|
const HOUR = MINUTE * 60;
|
|
|
|
const DAY = HOUR * 24;
|
|
|
|
const WEEK = DAY * 7;
|
|
|
|
const MONTH = (DAY * 365) / 12;
|
|
|
|
const YEAR = DAY * 365;
|
|
|
|
|
2020-06-08 09:45:53 +03:00
|
|
|
export const copyText = (str) => {
|
2020-06-19 06:57:57 +03:00
|
|
|
const el = document.createElement("textarea");
|
2020-06-08 09:45:53 +03:00
|
|
|
el.value = str;
|
2020-06-19 06:57:57 +03:00
|
|
|
el.setAttribute("readonly", "");
|
|
|
|
el.style.position = "absolute";
|
|
|
|
el.style.left = "-9999px";
|
2020-07-27 04:51:51 +03:00
|
|
|
el.style.visibility = "hidden";
|
|
|
|
el.style.opacity = "0";
|
2020-06-08 09:45:53 +03:00
|
|
|
document.body.appendChild(el);
|
|
|
|
el.select();
|
2020-06-19 06:57:57 +03:00
|
|
|
document.execCommand("copy");
|
2020-06-08 09:45:53 +03:00
|
|
|
document.body.removeChild(el);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-07-29 02:08:38 +03:00
|
|
|
export const getKey = (text) => {
|
|
|
|
if (isEmpty(text)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace("Basic ", "");
|
|
|
|
};
|
|
|
|
|
2020-09-02 11:33:39 +03:00
|
|
|
export const getPresentationName = (user) => {
|
|
|
|
if (!isEmpty(user.data.name)) {
|
|
|
|
return user.data.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.username;
|
|
|
|
};
|
|
|
|
|
2020-08-25 21:08:42 +03:00
|
|
|
export const getCIDGatewayURL = (cid) => {
|
|
|
|
return `https://${cid}.${Constants.gateways.ipfs}`;
|
|
|
|
};
|
|
|
|
|
2020-08-27 07:24:49 +03:00
|
|
|
export const createSlug = (text, base = "untitled") => {
|
2020-07-27 04:51:51 +03:00
|
|
|
if (isEmpty(text)) {
|
2020-08-27 07:24:49 +03:00
|
|
|
return base;
|
2020-07-27 04:51:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const a = "æøåàáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;";
|
|
|
|
const b = "aoaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------";
|
|
|
|
const p = new RegExp(a.split("").join("|"), "g");
|
|
|
|
|
|
|
|
return text
|
|
|
|
.toString()
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\s+/g, "-") // Replace spaces with -
|
|
|
|
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special chars
|
|
|
|
.replace(/&/g, "-and-") // Replace & with 'and'
|
|
|
|
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
|
|
|
|
.replace(/\-\-+/g, "-") // Replace multiple - with single -
|
|
|
|
.replace(/^-+/, "") // Trim - from start of text
|
|
|
|
.replace(/-+$/, ""); // Trim - from end of text
|
|
|
|
};
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
export const hexToRGBA = (hex, alpha = 1) => {
|
2020-06-19 06:57:57 +03:00
|
|
|
hex = hex.replace("#", "");
|
|
|
|
var r = parseInt(
|
|
|
|
hex.length == 3 ? hex.slice(0, 1).repeat(2) : hex.slice(0, 2),
|
|
|
|
16
|
|
|
|
);
|
|
|
|
var g = parseInt(
|
|
|
|
hex.length == 3 ? hex.slice(1, 2).repeat(2) : hex.slice(2, 4),
|
|
|
|
16
|
|
|
|
);
|
|
|
|
var b = parseInt(
|
|
|
|
hex.length == 3 ? hex.slice(2, 3).repeat(2) : hex.slice(4, 6),
|
|
|
|
16
|
|
|
|
);
|
2020-04-09 00:29:13 +03:00
|
|
|
if (alpha) {
|
2020-06-19 06:57:57 +03:00
|
|
|
return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
|
2020-04-09 00:29:13 +03:00
|
|
|
} else {
|
2020-06-19 06:57:57 +03:00
|
|
|
return "rgb(" + r + ", " + g + ", " + b + ")";
|
2020-04-09 00:29:13 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const bytesToSize = (bytes, decimals = 2) => {
|
2020-06-19 06:57:57 +03:00
|
|
|
if (bytes === 0) return "0 Bytes";
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
const k = 1024;
|
|
|
|
const dm = decimals < 0 ? 0 : decimals;
|
2020-06-19 06:57:57 +03:00
|
|
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
|
2020-07-20 04:37:00 +03:00
|
|
|
return `${(bytes / Math.pow(k, i)).toFixed(dm)} ${sizes[i]}`;
|
2020-04-09 00:29:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getRemainingTime = (seconds) => {
|
|
|
|
seconds = seconds > 0 ? seconds : 1;
|
|
|
|
|
|
|
|
let [value, unit] =
|
|
|
|
seconds < MINUTE
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds), "second"]
|
2020-04-09 00:29:13 +03:00
|
|
|
: seconds < HOUR
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds / MINUTE), "minute"]
|
2020-04-09 00:29:13 +03:00
|
|
|
: seconds < DAY
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds / HOUR), "hour"]
|
2020-04-09 00:29:13 +03:00
|
|
|
: seconds < WEEK
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds / DAY), "day"]
|
2020-04-09 00:29:13 +03:00
|
|
|
: seconds < MONTH
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds / WEEK), "week"]
|
2020-04-09 00:29:13 +03:00
|
|
|
: seconds < YEAR
|
2020-06-19 06:57:57 +03:00
|
|
|
? [Math.round(seconds / MONTH), "month"]
|
|
|
|
: [Math.round(seconds / YEAR), "year"];
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
unit = pluralize(unit, value);
|
|
|
|
|
|
|
|
return `${value} ${unit} remaining`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const formatAsFilecoin = (number) => {
|
|
|
|
return `${number} FIL`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isEmpty = (string) => {
|
2020-02-19 09:30:47 +03:00
|
|
|
return !string || !string.toString().trim();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const pluralize = (text, count) => {
|
|
|
|
return count > 1 || count === 0 ? `${text}s` : text;
|
|
|
|
};
|
|
|
|
|
2020-06-19 06:57:57 +03:00
|
|
|
export const elide = (string, length = 140, emptyState = "...") => {
|
2020-02-19 09:30:47 +03:00
|
|
|
if (isEmpty(string)) {
|
|
|
|
return emptyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.length < length) {
|
|
|
|
return string.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${string.substring(0, length)}...`;
|
|
|
|
};
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
export const toDate = (data) => {
|
2020-02-19 09:30:47 +03:00
|
|
|
const date = new Date(data);
|
|
|
|
return `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`;
|
|
|
|
};
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
export const formatNumber = (x) => {
|
2020-06-19 06:57:57 +03:00
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
2020-04-09 00:29:13 +03:00
|
|
|
};
|