mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-15 19:00:33 +01:00
35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
|
export function formatDate(dateString) {
|
||
|
const date = isNaN(new Date(dateString).getTime())
|
||
|
? new Date()
|
||
|
: new Date(dateString);
|
||
|
const options = { year: "numeric", month: "short", day: "numeric" };
|
||
|
const formattedDate = date.toLocaleDateString("en-US", options);
|
||
|
return formattedDate;
|
||
|
}
|
||
|
|
||
|
export function getFileExtension(path) {
|
||
|
const match = path.match(/[^\/\\&\?]+\.\w{1,4}(?=([\?&].*$|$))/);
|
||
|
return match ? match[0].split(".").pop() : "file";
|
||
|
}
|
||
|
|
||
|
export function truncate(str, n) {
|
||
|
const fileExtensionPattern = /(\..+)$/;
|
||
|
const extensionMatch = str.match(fileExtensionPattern);
|
||
|
|
||
|
if (str.length <= n) return str;
|
||
|
|
||
|
if (extensionMatch && extensionMatch[1]) {
|
||
|
const extension = extensionMatch[1];
|
||
|
const nameWithoutExtension = str.replace(fileExtensionPattern, "");
|
||
|
const truncationPoint = Math.max(0, n - extension.length - 4);
|
||
|
const truncatedName =
|
||
|
nameWithoutExtension.substr(0, truncationPoint) +
|
||
|
"..." +
|
||
|
nameWithoutExtension.slice(-4);
|
||
|
|
||
|
return truncatedName + extension;
|
||
|
} else {
|
||
|
return str.length > n ? str.substr(0, n - 8) + "..." + str.slice(-4) : str;
|
||
|
}
|
||
|
}
|