-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timestamps are converted to start of the days and filtered to b uniqu…
…e before calculating streaks
- Loading branch information
1 parent
2d33525
commit 631c7ea
Showing
6 changed files
with
202 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,46 +1,54 @@ | |||
import _ from 'lodash'; | |||
import moment from 'moment-timezone'; | import moment from 'moment-timezone'; | ||
import { dayCount } from '../utils/date-utils'; | import { dayCount } from '../utils/date-utils'; | ||
|
|
||
const daysBetween = 1.5; | const daysBetween = 1.5; | ||
|
|
||
export function calcCurrentStreak(cals, timezone = 'UTC') { | export function prepUniqueDays(cals, tz = 'UTC') { | ||
const revCals = cals.slice().reverse(); | |||
|
|
||
if (dayCount([moment().tz(timezone), revCals[0]], timezone) > daysBetween) { | return _(cals) | ||
.map(ts => moment(ts).tz(tz).startOf('day').valueOf()) | |||
.uniq() | |||
.sort() | |||
.value(); | |||
} | |||
|
|||
export function calcCurrentStreak(cals, tz = 'UTC') { | |||
|
|||
let prev = _.last(cals); | |||
if (moment().tz(tz).startOf('day').diff(prev, 'days') > daysBetween) { | |||
return 0; | return 0; | ||
} | } | ||
let currentStreak = 0; | |||
let streakContinues = true; | |||
_.forEachRight(cals, cur => { | |||
if (moment(prev).diff(cur, 'days') < daysBetween) { | |||
prev = cur; | |||
currentStreak++; | |||
} else { | |||
// current streak found | |||
streakContinues = false; | |||
} | |||
return streakContinues; | |||
}); | |||
|
|
||
let streakBroken = false; | return currentStreak; | ||
const lastDayInStreak = revCals | |||
.reduce((current, cal, index) => { | |||
const before = revCals[index === 0 ? 0 : index - 1]; | |||
if ( | |||
!streakBroken && | |||
moment(before).tz(timezone).diff(cal, 'days', true) < daysBetween | |||
) { | |||
return index; | |||
} | |||
streakBroken = true; | |||
return current; | |||
}, 0); | |||
|
|||
const lastTimestamp = revCals[lastDayInStreak]; | |||
return dayCount([moment().tz(timezone), lastTimestamp], timezone); | |||
} | } | ||
|
|
||
export function calcLongestStreak(cals, timezone = 'UTC') { | export function calcLongestStreak(cals, tz = 'UTC') { | ||
|
|||
let tail = cals[0]; | let tail = cals[0]; | ||
const longest = cals.reduce((longest, head, index) => { | const longest = cals.reduce((longest, head, index) => { | ||
const last = cals[index === 0 ? 0 : index - 1]; | const last = cals[index === 0 ? 0 : index - 1]; | ||
// is streak broken | // is streak broken | ||
if (moment(head).tz(timezone).diff(last, 'days', true) > daysBetween) { | if (moment(head).tz(tz).diff(moment(last).tz(tz), 'days') > daysBetween) { | ||
tail = head; | tail = head; | ||
} | } | ||
if (dayCount(longest, timezone) < dayCount([head, tail], timezone)) { | if (dayCount(longest, tz) < dayCount([head, tail], tz)) { | ||
return [head, tail]; | return [head, tail]; | ||
} | } | ||
return longest; | return longest; | ||
}, [cals[0], cals[0]]); | }, [cals[0], cals[0]]); | ||
|
|
||
return dayCount(longest, timezone); | return dayCount(longest, tz); | ||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.