54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
|
export function numberToEuroFormat(number) {
|
||
|
let formatter = new Intl.NumberFormat('de', { style: 'currency', currency: 'EUR' });
|
||
|
return formatter.format(number);
|
||
|
}
|
||
|
|
||
|
export function isJson(string) {
|
||
|
try { JSON.parse(string); }
|
||
|
catch (error) { return false; }
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
export function isEncoded(string) {
|
||
|
try { atob(string); }
|
||
|
catch (error) { return false; }
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
export function fetchData(method = null, url, data = null) {
|
||
|
|
||
|
const csrf = $('meta[name="csrf-token"]').attr('content');
|
||
|
const urlBase = document.querySelector('meta[name="url-base"]').getAttribute("content");
|
||
|
|
||
|
const urlPattern = /^(https?:\/\/|ftp:\/\/)[^\s/$.?#].[^\s]*$/i;
|
||
|
const pathPattern = /^(\/|[^\/\s]+\/)[^\s]*$/;
|
||
|
|
||
|
if (pathPattern.test(url)) url = urlBase + (url.startsWith('/') ? "" : "/") + url;
|
||
|
else if (!urlPattern.test(url)) throw new Error("Invalid URL.");
|
||
|
|
||
|
return fetch(url, {
|
||
|
method: method ?? 'GET',
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json',
|
||
|
'X-CSRF-TOKEN': csrf
|
||
|
},
|
||
|
body: JSON.stringify(data ?? {})
|
||
|
})
|
||
|
.then(response => response.json())
|
||
|
.catch(error => console.error("Error fetching data:", error));
|
||
|
}
|
||
|
|
||
|
export function url(path = "") {
|
||
|
let protocol = window.location.protocol;
|
||
|
let host = window.location.hostname;
|
||
|
let port = window.location.port;
|
||
|
let pathk = window.location.pathname;
|
||
|
|
||
|
let baseUrl = protocol + "//" + host + (port ? ':' + port : '');
|
||
|
|
||
|
let pathArray = pathk.split('/');
|
||
|
pathArray.pop();
|
||
|
let directoryOnly = pathArray.join('/');
|
||
|
|
||
|
return baseUrl + directoryOnly + '/' + path;
|
||
|
}
|