2023-08-01 03:15:15 +02:00
|
|
|
export function formatDate(date: Date): string {
|
|
|
|
const options = { year: "numeric", month: "long", day: "numeric" } as const;
|
|
|
|
return new Intl.DateTimeFormat("en-US", options).format(date);
|
|
|
|
}
|
2023-08-01 17:50:00 +02:00
|
|
|
|
|
|
|
export function safeFileName(inputString: string): string {
|
|
|
|
// Convert the string to lowercase
|
|
|
|
let fileName = inputString.toLowerCase();
|
|
|
|
|
|
|
|
// Replace spaces with underscores
|
|
|
|
fileName = fileName.replace(/ /g, "_");
|
|
|
|
|
|
|
|
// Remove characters that are not safe for file names
|
|
|
|
fileName = fileName.replace(/[^\w.-]/g, "");
|
|
|
|
|
|
|
|
return fileName;
|
|
|
|
}
|
2023-08-02 01:58:03 +02:00
|
|
|
|
|
|
|
export function extractHashTags(inputString: string) {
|
|
|
|
const hashtags = [];
|
|
|
|
|
|
|
|
for (
|
2023-08-02 15:05:35 +02:00
|
|
|
const [hashtag] of inputString.matchAll(/(?:^|\s)#\S*(?<!\))/g)
|
2023-08-02 01:58:03 +02:00
|
|
|
) {
|
2023-08-02 15:05:35 +02:00
|
|
|
const cleaned = hashtag.replace(/\#/g, "").trim();
|
|
|
|
if (cleaned.length > 2) {
|
|
|
|
hashtags.push(cleaned);
|
|
|
|
}
|
2023-08-02 01:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hashtags;
|
|
|
|
}
|
2023-08-02 15:56:33 +02:00
|
|
|
|
|
|
|
export const isYoutubeLink = (link: string) => {
|
|
|
|
try {
|
|
|
|
const url = new URL(link);
|
|
|
|
return ["youtu.be", "youtube.com","www.youtube.com" ].includes(url.hostname);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function extractYoutubeId(link: string) {
|
|
|
|
const url = new URL(link);
|
|
|
|
if (url.searchParams.has("v")) {
|
|
|
|
const id = url.searchParams.get("v");
|
|
|
|
|
|
|
|
if (id?.length && id.length > 4) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.pathname.replace(/^\//, "");
|
|
|
|
}
|