(raw: string | null, now: Date)
| 32 | |
| 33 | // Anything stored for another day, or broken, starts a fresh day. |
| 34 | export const parsePatchyDay = (raw: string | null, now: Date): PatchyDay => { |
| 35 | const fresh = createPatchyDay(now); |
| 36 | if (!raw) { |
| 37 | return fresh; |
| 38 | } |
| 39 | try { |
| 40 | const stored = JSON.parse(raw) as Partial<PatchyDay>; |
| 41 | if (!stored || stored.day !== fresh.day) { |
| 42 | return fresh; |
| 43 | } |
| 44 | return { |
| 45 | day: fresh.day, |
| 46 | reads: isCount(stored.reads) ? stored.reads : 0, |
| 47 | upvotes: isCount(stored.upvotes) ? stored.upvotes : 0, |
| 48 | bookmarks: isCount(stored.bookmarks) ? stored.bookmarks : 0, |
| 49 | }; |
| 50 | } catch { |
| 51 | return fresh; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | const counterFor: Partial< |
| 56 | Record< |
no test coverage detected