feat: slugify, rate limit ed env helper con test
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
export function getEnv(name: string, fallback?: string): string {
|
||||
const v = process.env[name] ?? (import.meta.env ? import.meta.env[name] : undefined) ?? fallback;
|
||||
if (v === undefined) throw new Error(`Variabile d'ambiente mancante: ${name}`);
|
||||
return v;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
const buckets = new Map<string, number[]>();
|
||||
|
||||
export function rateLimit(key: string, max: number, windowMs: number): boolean {
|
||||
const now = Date.now();
|
||||
const recent = (buckets.get(key) ?? []).filter((t) => now - t < windowMs);
|
||||
if (recent.length >= max) {
|
||||
buckets.set(key, recent);
|
||||
return false;
|
||||
}
|
||||
recent.push(now);
|
||||
buckets.set(key, recent);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function _resetBuckets(): void {
|
||||
buckets.clear();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export function slugify(input: string): string {
|
||||
return input
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[̀-ͯ]/g, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
Reference in New Issue
Block a user