This commit is contained in:
Benjamin Toby
2025-07-20 10:35:54 +01:00
parent dd1d05251d
commit a0a0ab8ee4
99 changed files with 5678 additions and 909 deletions
@@ -0,0 +1,6 @@
export default function twuiCamelToNormalCase(str: string) {
return str
.replace(/([A-Z])/g, " $1")
.trim()
.replace(/\b\w/g, (char) => char.toUpperCase());
}
+3 -2
View File
@@ -49,14 +49,15 @@ export default async function fetchApi<
): Promise<R> {
let data;
const csrfValue = localStorage.getItem(localStorageCSRFKey || "csrf");
const csrfKey = "x-dsql-csrf-key";
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
let finalHeaders = {
"Content-Type": "application/json",
} as FetchHeader;
if (csrf && csrfValue) {
finalHeaders[csrfHeaderKey || "x-csrf-key"] = csrfValue;
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue;
}
if (typeof options === "string") {
+13 -14
View File
@@ -2,36 +2,38 @@ export type ImageInputToBase64FunctionReturn = {
imageBase64?: string;
imageBase64Full?: string;
imageName?: string;
imageType?: string;
};
export type ImageInputToBase64FunctioParam = {
imageInput: HTMLInputElement;
imageInput?: HTMLInputElement;
maxWidth?: number;
mimeType?: string;
file?: File;
};
export default async function imageInputToBase64({
imageInput,
maxWidth,
mimeType,
file,
}: ImageInputToBase64FunctioParam): Promise<ImageInputToBase64FunctionReturn> {
try {
if (!imageInput.files?.[0]) {
throw new Error("No Files found in this image input");
const finalFile = file || imageInput?.files?.[0];
if (!finalFile) {
throw new Error("No Files found");
}
let imagePreviewNode = document.querySelector(
`[data-imagepreview='image']`
);
let imageName = imageInput.files[0].name.replace(/\..*/, "");
let imageName = finalFile.name.replace(/\..*/, "");
let imageDataBase64: string | undefined;
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
const MIME_TYPE = mimeType ? mimeType : finalFile.type;
const QUALITY = 0.95;
const MAX_WIDTH = maxWidth ? maxWidth : null;
const file = imageInput.files[0];
const blobURL = URL.createObjectURL(file);
const blobURL = URL.createObjectURL(finalFile);
const img = new Image();
img.src = blobURL;
@@ -76,6 +78,7 @@ export default async function imageInputToBase64({
imageBase64: imageDataBase64?.replace(/.*?base64,/, ""),
imageBase64Full: imageDataBase64,
imageName: imageName,
imageType: MIME_TYPE,
};
} catch (error: any) {
console.log("Image Processing Error! =>", error.message);
@@ -87,7 +90,3 @@ export default async function imageInputToBase64({
};
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
+6
View File
@@ -0,0 +1,6 @@
export default function twuiNormalizeText(txt: string) {
return txt
.replace(/\n|\r|\n\r/g, " ")
.replace(/ {2,}/g, " ")
.trim();
}
+31
View File
@@ -0,0 +1,31 @@
export default function twuiNumberfy(num: any, decimals?: number): number {
try {
const numberString = String(num)
.replace(/[^0-9\.]/g, "")
.replace(/\.$/, "");
if (!numberString.match(/./)) return 0;
const existingDecimals = numberString.match(/\./)
? numberString.split(".").pop()?.length
: undefined;
const numberfiedNum = Number(numberString);
if (typeof numberfiedNum !== "number") return 0;
if (isNaN(numberfiedNum)) return 0;
if (decimals == 0) {
return Math.round(Number(numberfiedNum));
} else if (decimals) {
return Number(numberfiedNum.toFixed(decimals));
}
if (existingDecimals)
return Number(numberfiedNum.toFixed(existingDecimals));
return Math.round(numberfiedNum);
} catch (error: any) {
console.log(`Numberfy ERROR: ${error.message}`);
return 0;
}
}
@@ -0,0 +1,15 @@
export default function twuiSlugToNormalText(str?: string) {
if (!str) return "";
return str
.toLowerCase()
.replace(/ /g, "-")
.replace(/[^a-z0-9\-]/g, "-")
.replace(/-{2,}/g, "-")
.replace(/[-]/g, " ")
.split(" ")
.map(
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join(" ");
}
+37
View File
@@ -0,0 +1,37 @@
/**
* # Return the slug of a string
*
* @example
* slugify("Hello World") // "hello-world"
* slugify("Yes!") // "yes"
* slugify("Hello!!! World!") // "hello-world"
*/
export default function twuiSlugify(
str?: string,
divider?: "-" | "_" | null,
allowTrailingDash?: boolean | null
): string {
const finalSlugDivider = divider || "-";
try {
if (!str) return "";
let finalStr = String(str)
.trim()
.toLowerCase()
.replace(/ {2,}/g, " ")
.replace(/ /g, finalSlugDivider)
.replace(/[^a-z0-9]/g, finalSlugDivider)
.replace(/-{2,}|_{2,}/g, finalSlugDivider)
.replace(/^-/, "");
if (allowTrailingDash) {
return finalStr;
}
return finalStr.replace(/-$/, "");
} catch (error: any) {
console.log(`Slugify ERROR: ${error.message}`);
return "";
}
}