Files
teknav-bak/app/backend/src/lib/persian.ts
T
arsam 1a53a3f5f6 Import source: app/backend/src/lib/persian
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
2026-09-18 23:19:28 +03:30

35 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function normalizeFa(input: string) {
return input
.replace(/[\u200c\u0640]/g, '')
.replace(/[\u064b-\u0652]/g, '')
.replace(/[يى]/g, 'ی')
.replace(/ك/g, 'ک')
.replace(/[أإآٱ]/g, 'ا')
.replace(/\s+/g, ' ')
.trim()
.toLowerCase();
}
export function computeStreak(streakCount: number, lastReadAt: Date | null, now: Date): number {
if (!lastReadAt) return 1;
const lastDate = new Date(lastReadAt);
lastDate.setHours(0, 0, 0, 0);
const today = new Date(now);
today.setHours(0, 0, 0, 0);
const diffDays = Math.floor((today.getTime() - lastDate.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays === 1) return streakCount + 1;
if (diffDays > 1) return 1;
return streakCount;
}
export function currentStreakIfAlive(streakCount: number, lastReadAt: Date | null, now: Date): number {
if (!lastReadAt) return 0;
const lastDate = new Date(lastReadAt);
lastDate.setHours(0, 0, 0, 0);
const today = new Date(now);
today.setHours(0, 0, 0, 0);
const diffDays = Math.floor((today.getTime() - lastDate.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays > 1) return 0;
return streakCount;
}