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.
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user