mirror of
https://github.com/haexhub/haex-hub.git
synced 2025-12-18 15:00:52 +01:00
zwischenstand
This commit is contained in:
106
utils/helper.ts
106
utils/helper.ts
@ -1,30 +1,32 @@
|
||||
import type { LocationQueryValue, RouteLocationRawI18n } from 'vue-router'
|
||||
|
||||
export const bytesToBase64DataUrlAsync = async (
|
||||
bytes: Uint8Array,
|
||||
type = "application/octet-stream"
|
||||
type = 'application/octet-stream',
|
||||
) => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const reader = Object.assign(new FileReader(), {
|
||||
onload: () => resolve(reader.result),
|
||||
onerror: () => reject(reader.error),
|
||||
});
|
||||
reader.readAsDataURL(new File([new Blob([bytes])], "", { type }));
|
||||
});
|
||||
};
|
||||
})
|
||||
reader.readAsDataURL(new File([new Blob([bytes])], '', { type }))
|
||||
})
|
||||
}
|
||||
|
||||
export const blobToImageAsync = (blob: Blob): Promise<HTMLImageElement> => {
|
||||
return new Promise((resolve) => {
|
||||
console.log("transform blob", blob);
|
||||
const url = URL.createObjectURL(blob);
|
||||
let img = new Image();
|
||||
console.log('transform blob', blob)
|
||||
const url = URL.createObjectURL(blob)
|
||||
const img = new Image()
|
||||
img.onload = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
resolve(img);
|
||||
};
|
||||
img.src = url;
|
||||
});
|
||||
};
|
||||
URL.revokeObjectURL(url)
|
||||
resolve(img)
|
||||
}
|
||||
img.src = url
|
||||
})
|
||||
}
|
||||
|
||||
export const deepToRaw = <T extends Record<string, any>>(sourceObj: T): T => {
|
||||
/* export const deepToRaw = <T extends Record<string, any>>(sourceObj: T): T => {
|
||||
const objectIterator = (input: any): any => {
|
||||
if (Array.isArray(input)) {
|
||||
return input.map((item) => objectIterator(item));
|
||||
@ -42,74 +44,78 @@ export const deepToRaw = <T extends Record<string, any>>(sourceObj: T): T => {
|
||||
};
|
||||
|
||||
return objectIterator(sourceObj);
|
||||
};
|
||||
}; */
|
||||
|
||||
export const readableFileSize = (sizeInByte: number | string = 0) => {
|
||||
if (!sizeInByte) {
|
||||
return "0 KB";
|
||||
return '0 KB'
|
||||
}
|
||||
const size = typeof sizeInByte === "string" ? parseInt(sizeInByte) : sizeInByte;
|
||||
const sizeInKb = size / 1024;
|
||||
const sizeInMb = sizeInKb / 1024;
|
||||
const sizeInGb = sizeInMb / 1024;
|
||||
const sizeInTb = sizeInGb / 1024;
|
||||
const size =
|
||||
typeof sizeInByte === 'string' ? parseInt(sizeInByte) : sizeInByte
|
||||
const sizeInKb = size / 1024
|
||||
const sizeInMb = sizeInKb / 1024
|
||||
const sizeInGb = sizeInMb / 1024
|
||||
const sizeInTb = sizeInGb / 1024
|
||||
|
||||
if (sizeInTb > 1) return `${sizeInTb.toFixed(2)} TB`;
|
||||
if (sizeInGb > 1) return `${sizeInGb.toFixed(2)} GB`;
|
||||
if (sizeInMb > 1) return `${sizeInMb.toFixed(2)} MB`;
|
||||
if (sizeInTb > 1) return `${sizeInTb.toFixed(2)} TB`
|
||||
if (sizeInGb > 1) return `${sizeInGb.toFixed(2)} GB`
|
||||
if (sizeInMb > 1) return `${sizeInMb.toFixed(2)} MB`
|
||||
|
||||
return `${sizeInKb.toFixed(2)} KB`;
|
||||
};
|
||||
|
||||
import type { LocationQueryValue, RouteLocationRawI18n } from "vue-router";
|
||||
return `${sizeInKb.toFixed(2)} KB`
|
||||
}
|
||||
|
||||
export const getSingleRouteParam = (
|
||||
param: string | string[] | LocationQueryValue | LocationQueryValue[]
|
||||
param: string | string[] | LocationQueryValue | LocationQueryValue[],
|
||||
): string => {
|
||||
const _param = Array.isArray(param) ? param.at(0) ?? "" : param ?? "";
|
||||
const _param = Array.isArray(param) ? param.at(0) ?? '' : param ?? ''
|
||||
//console.log('found param', _param, param);
|
||||
return decodeURIComponent(_param);
|
||||
};
|
||||
return decodeURIComponent(_param)
|
||||
}
|
||||
|
||||
export const isRouteActive = (to: RouteLocationRawI18n, exact: boolean = false) =>
|
||||
export const isRouteActive = (
|
||||
to: RouteLocationRawI18n,
|
||||
exact: boolean = false,
|
||||
) =>
|
||||
computed(() => {
|
||||
const found = useRouter()
|
||||
.getRoutes()
|
||||
.find((route) => route.name === useLocaleRoute()(to)?.name);
|
||||
.find((route) => route.name === useLocaleRoute()(to)?.name)
|
||||
//console.log('found route', found, useRouter().currentRoute.value, to);
|
||||
return exact
|
||||
? found?.name === useRouter().currentRoute.value.name
|
||||
: found?.name === useRouter().currentRoute.value.name ||
|
||||
found?.children.some((child) => child.name === useRouter().currentRoute.value.name);
|
||||
});
|
||||
found?.children.some(
|
||||
(child) => child.name === useRouter().currentRoute.value.name,
|
||||
)
|
||||
})
|
||||
|
||||
export const isKey = <T extends object>(x: T, k: PropertyKey): k is keyof T => {
|
||||
return k in x;
|
||||
};
|
||||
return k in x
|
||||
}
|
||||
|
||||
export const filterAsync = async <T>(
|
||||
arr: T[],
|
||||
predicate: (value: T, index: number, array: T[]) => Promise<boolean>
|
||||
predicate: (value: T, index: number, array: T[]) => Promise<boolean>,
|
||||
) => {
|
||||
// 1. Mappe jedes Element auf ein Promise, das zu true/false auflöst
|
||||
const results = await Promise.all(arr.map(predicate));
|
||||
const results = await Promise.all(arr.map(predicate))
|
||||
|
||||
// 2. Filtere das ursprüngliche Array basierend auf den Ergebnissen
|
||||
return arr.filter((_value, index) => results[index]);
|
||||
};
|
||||
return arr.filter((_value, index) => results[index])
|
||||
}
|
||||
|
||||
export const stringToHex = (str: string) =>
|
||||
str
|
||||
.split("")
|
||||
.map((char) => char.charCodeAt(0).toString(16).padStart(2, "0"))
|
||||
.join(""); // Join array into a single string
|
||||
.split('')
|
||||
.map((char) => char.charCodeAt(0).toString(16).padStart(2, '0'))
|
||||
.join('') // Join array into a single string
|
||||
|
||||
export const hexToString = (hex: string) => {
|
||||
if (!hex) return "";
|
||||
if (!hex) return ''
|
||||
const parsedValue = hex
|
||||
.match(/.{1,2}/g) // Split hex into pairs
|
||||
?.map((byte) => String.fromCharCode(parseInt(byte, 16))) // Convert hex to char
|
||||
.join(""); // Join array into a single string
|
||||
.join('') // Join array into a single string
|
||||
|
||||
return parsedValue ? parsedValue : "";
|
||||
};
|
||||
return parsedValue ? parsedValue : ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user