Add junk drawer

This commit is contained in:
2024-10-01 16:57:19 -06:00
parent 7fae07b3c3
commit e021be16a3
10 changed files with 311 additions and 0 deletions

43
src/utils/slug.ts Normal file
View File

@@ -0,0 +1,43 @@
interface SlugOptions {
year: boolean;
month: boolean;
day: boolean;
hour: boolean;
minute: boolean;
second: boolean;
random: boolean;
separator: string;
}
const defaultSlugOptions: SlugOptions = {
year: true,
month: true,
day: true,
hour: true,
minute: true,
second: false,
random: false,
separator: '',
};
export const generateUniqueSlug = (
options: Partial<SlugOptions> = {},
): string => {
const { year, month, day, hour, minute, second, random, separator } = {
...defaultSlugOptions,
...options,
};
const date = new Date();
const parts = [
year ? date.getFullYear() : '',
month ? String(date.getMonth() + 1).padStart(2, '0') : '',
day ? String(date.getDate()).padStart(2, '0') : '',
hour ? String(date.getHours()).padStart(2, '0') : '',
minute ? String(date.getMinutes()).padStart(2, '0') : '',
second ? String(date.getSeconds()).padStart(2, '0') : '',
random ? Math.random().toString(36).substring(2, 15) : '',
].filter(Boolean);
return parts.join(separator);
};