Add junk drawer
This commit is contained in:
43
src/utils/slug.ts
Normal file
43
src/utils/slug.ts
Normal 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);
|
||||
};
|
Reference in New Issue
Block a user