2023-08-06 17:47:26 +02:00
|
|
|
import { resources } from "@lib/resources.ts";
|
|
|
|
|
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, "");
|
|
|
|
|
2023-08-04 22:35:25 +02:00
|
|
|
fileName = fileName.replaceAll(":", "");
|
|
|
|
|
2023-08-01 17:50:00 +02:00
|
|
|
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);
|
2023-08-04 22:35:25 +02:00
|
|
|
return ["youtu.be", "youtube.com", "www.youtube.com"].includes(
|
|
|
|
url.hostname,
|
|
|
|
);
|
2023-08-02 17:21:03 +02:00
|
|
|
} catch (_err) {
|
2023-08-02 15:56:33 +02:00
|
|
|
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(/^\//, "");
|
|
|
|
}
|
2023-08-04 22:35:25 +02:00
|
|
|
|
|
|
|
export async function hash(message: string) {
|
|
|
|
const data = new TextEncoder().encode(message);
|
|
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
|
|
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
return hashHex;
|
|
|
|
}
|
|
|
|
// Helper function to calculate SHA-256 hash
|
|
|
|
export async function sha256(input: string) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const data = encoder.encode(input);
|
|
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
|
|
return base64urlencode(new Uint8Array(hashBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to encode a byte array as a URL-safe base64 string
|
|
|
|
function base64urlencode(data: Uint8Array) {
|
|
|
|
const base64 = btoa(String.fromCharCode(...data));
|
|
|
|
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
|
|
}
|
|
|
|
export function getCookie(name: string): string | null {
|
|
|
|
if (typeof document === "undefined") return null;
|
|
|
|
const nameLenPlus = name.length + 1;
|
|
|
|
return document.cookie
|
|
|
|
.split(";")
|
|
|
|
.map((c) => c.trim())
|
|
|
|
.filter((cookie) => {
|
|
|
|
return cookie.substring(0, nameLenPlus) === `${name}=`;
|
|
|
|
})
|
|
|
|
.map((cookie) => {
|
|
|
|
return decodeURIComponent(cookie.substring(nameLenPlus));
|
|
|
|
})[0] || null;
|
|
|
|
}
|
2023-08-06 17:47:26 +02:00
|
|
|
|
|
|
|
const resourcePrefixes = Object.values(resources).map((v) => v.prefix).filter(
|
|
|
|
(s) => s.length > 2,
|
|
|
|
);
|
|
|
|
export const isLocalImage = (src: string) =>
|
|
|
|
resourcePrefixes.some((p) => src.startsWith(p));
|
2023-08-10 19:16:03 +02:00
|
|
|
|
|
|
|
export const isString = (input: string | undefined): input is string => {
|
|
|
|
return typeof input === "string";
|
|
|
|
};
|