2023-01-25 14:15:34 +01:00
|
|
|
import {format, parse, formatDistance as formatDistanceDateFns } from "date-fns";
|
2024-03-25 18:37:08 +01:00
|
|
|
import {API_DATETIME_FORMAT} from "./constants";
|
2023-01-14 09:51:46 +01:00
|
|
|
|
2023-01-19 18:12:16 +01:00
|
|
|
function createDownload(name, data) {
|
|
|
|
const url = window.URL.createObjectURL(new Blob([data]));
|
|
|
|
const link = document.createElement('a');
|
|
|
|
link.href = url;
|
|
|
|
link.setAttribute('download', name);
|
|
|
|
link.setAttribute("target", "_blank");
|
|
|
|
document.body.appendChild(link);
|
|
|
|
link.click();
|
|
|
|
link.remove();
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
function humanReadableSize(bytes, dp = 1) {
|
|
|
|
const thresh = 1024;
|
|
|
|
|
|
|
|
if (Math.abs(bytes) < thresh) {
|
|
|
|
return bytes + ' B';
|
|
|
|
}
|
|
|
|
|
|
|
|
const units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
|
|
let u = -1;
|
|
|
|
const r = 10**dp;
|
|
|
|
|
|
|
|
do {
|
|
|
|
bytes /= thresh;
|
|
|
|
++u;
|
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
|
|
|
|
|
|
return bytes.toFixed(dp) + ' ' + units[u];
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeParameter = (key) => {
|
|
|
|
const url = new URL(window.location);
|
|
|
|
url.searchParams.delete(key);
|
|
|
|
window.history.replaceState(null, '', url);
|
|
|
|
}
|
|
|
|
|
|
|
|
const getParameter = (key) => {
|
|
|
|
const url = new URL(window.location);
|
|
|
|
if (url.searchParams.has(key)) {
|
|
|
|
return decodeURIComponent(url.searchParams.get(key));
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:37:57 +01:00
|
|
|
const getCookie = (name) => {
|
|
|
|
let cookieString = decodeURIComponent(document.cookie);
|
|
|
|
let match = cookieString.match(new RegExp('(^| )' + name + '=([^;]+)'));
|
|
|
|
if (match) {
|
|
|
|
return match[2];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
const encodeText = (str) => {
|
|
|
|
return Uint8Array.from(str, c => c.charCodeAt(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
const decodeText = (buffer) => {
|
|
|
|
return String.fromCharCode(...new Uint8Array(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
const getBaseUrl = () => {
|
|
|
|
return window.location.protocol + "//" + window.location.host;
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
const toDate = (apiDate, apiFormat = API_DATETIME_FORMAT) => {
|
|
|
|
if (apiDate === null) {
|
|
|
|
return "";
|
|
|
|
} else if (!(apiDate instanceof Date)) {
|
2023-01-14 09:51:46 +01:00
|
|
|
if (!isNaN(apiDate)) {
|
2023-01-15 00:32:17 +01:00
|
|
|
apiDate = new Date(apiDate * 1000);
|
2023-01-14 09:51:46 +01:00
|
|
|
} else {
|
2023-01-25 14:15:34 +01:00
|
|
|
apiDate = parse(apiDate, apiFormat, new Date());
|
2023-01-14 09:51:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
return apiDate;
|
2023-01-14 09:51:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
const formatDate = (L, apiDate) => {
|
|
|
|
return format(toDate(apiDate), L("general.datefns_date_format", "YYY/MM/dd"));
|
|
|
|
}
|
2023-01-14 09:51:46 +01:00
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
const formatDateTime = (L, apiDate, precise=false) => {
|
2023-01-16 21:47:23 +01:00
|
|
|
let dateFormat = precise ?
|
2023-01-25 14:15:34 +01:00
|
|
|
L("general.datefns_datetime_format_precise", "YYY/MM/dd HH:mm:ss") :
|
|
|
|
L("general.datefns_datetime_format", "YYY/MM/dd HH:mm");
|
|
|
|
return format(toDate(apiDate), dateFormat);
|
2023-01-14 09:51:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
function formatDistance(dateFns, apiDate) {
|
|
|
|
return formatDistanceDateFns(toDate(apiDate), new Date(), { addSuffix: true, locale: dateFns });
|
|
|
|
}
|
|
|
|
|
2023-01-14 09:51:46 +01:00
|
|
|
const upperFirstChars = (str) => {
|
|
|
|
return str.split(" ")
|
|
|
|
.map(block => block.charAt(0).toUpperCase() + block.substring(1))
|
|
|
|
.join(" ");
|
|
|
|
}
|
|
|
|
|
2023-01-15 00:32:17 +01:00
|
|
|
const isInt = (value) => {
|
|
|
|
return !isNaN(value) &&
|
|
|
|
parseInt(Number(value)) === value &&
|
|
|
|
!isNaN(parseInt(value, 10));
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:37:57 +01:00
|
|
|
export { humanReadableSize, removeParameter, getParameter, getCookie,
|
2023-01-25 14:15:34 +01:00
|
|
|
encodeText, decodeText, getBaseUrl,
|
|
|
|
formatDate, formatDateTime, formatDistance,
|
|
|
|
upperFirstChars, isInt, createDownload };
|