| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  | import compose from 'lodash/fp/compose'; | 
					
						
							|  |  |  | import map from 'lodash/fp/map'; | 
					
						
							|  |  |  | import sortBy from 'lodash/fp/sortBy'; | 
					
						
							|  |  |  | import trans from 'lodash/fp/transform'; | 
					
						
							|  |  |  | import last from 'lodash/fp/last'; | 
					
						
							|  |  |  | import forEachRight from 'lodash/fp/forEachRight'; | 
					
						
							| 
									
										
										
										
											2016-01-19 21:11:20 -05:00
										 |  |  | import moment from 'moment-timezone'; | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  | import { dayCount } from '../utils/date-utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  | const transform = trans.convert({ cap: false }); | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  | const hoursBetween = 24; | 
					
						
							|  |  |  | const hoursDay = 24; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function prepUniqueDaysByHours(cals, tz = 'UTC') { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let prev = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // compose goes bottom to top (map > sortBy > transform)
 | 
					
						
							|  |  |  |   return compose( | 
					
						
							|  |  |  |     transform((data, cur, i) => { | 
					
						
							|  |  |  |       if (i < 1) { | 
					
						
							|  |  |  |         data.push(cur); | 
					
						
							|  |  |  |         prev = cur; | 
					
						
							|  |  |  |       } else if ( | 
					
						
							|  |  |  |         moment(cur) | 
					
						
							|  |  |  |           .tz(tz) | 
					
						
							|  |  |  |           .diff(moment(prev).tz(tz).startOf('day'), 'hours') | 
					
						
							|  |  |  |         >= hoursDay | 
					
						
							|  |  |  |       ) { | 
					
						
							|  |  |  |         data.push(cur); | 
					
						
							|  |  |  |         prev = cur; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, []), | 
					
						
							|  |  |  |     sortBy(e => e), | 
					
						
							|  |  |  |     map(ts => moment(ts).tz(tz).startOf('hours').valueOf()) | 
					
						
							|  |  |  |   )(cals); | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function calcCurrentStreak(cals, tz = 'UTC') { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  |   let prev = last(cals); | 
					
						
							|  |  |  |   if ( | 
					
						
							|  |  |  |     moment() | 
					
						
							|  |  |  |       .tz(tz) | 
					
						
							|  |  |  |       .startOf('day') | 
					
						
							|  |  |  |       .diff(moment(prev).tz(tz), 'hours') | 
					
						
							|  |  |  |     > hoursBetween | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  |   let currentStreak = 0; | 
					
						
							|  |  |  |   let streakContinues = true; | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  |   forEachRight(cur => { | 
					
						
							|  |  |  |     if ( | 
					
						
							|  |  |  |       moment(prev) | 
					
						
							|  |  |  |         .tz(tz) | 
					
						
							|  |  |  |         .startOf('day') | 
					
						
							|  |  |  |         .diff(moment(cur).tz(tz), 'hours') | 
					
						
							|  |  |  |       <= hoursBetween | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  |       prev = cur; | 
					
						
							|  |  |  |       currentStreak++; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       // current streak found
 | 
					
						
							|  |  |  |       streakContinues = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return streakContinues; | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  |   })(cals); | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  |   return currentStreak; | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  | export function calcLongestStreak(cals, tz = 'UTC') { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  |   let tail = cals[0]; | 
					
						
							|  |  |  |   const longest = cals.reduce((longest, head, index) => { | 
					
						
							|  |  |  |     const last = cals[index === 0 ? 0 : index - 1]; | 
					
						
							|  |  |  |     // is streak broken
 | 
					
						
							| 
									
										
										
										
											2017-11-03 06:17:13 -04:00
										 |  |  |     if (moment(head).tz(tz).startOf('day').diff(moment(last).tz(tz), 'hours') | 
					
						
							|  |  |  |         > hoursBetween) { | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  |       tail = head; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  |     if (dayCount(longest, tz) < dayCount([head, tail], tz)) { | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  |       return [head, tail]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return longest; | 
					
						
							|  |  |  |   }, [cals[0], cals[0]]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 09:10:18 -08:00
										 |  |  |   return dayCount(longest, tz); | 
					
						
							| 
									
										
										
										
											2015-12-10 14:52:09 -08:00
										 |  |  | } |