Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
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;
|
||
}
|