Skip to content

Instantly share code, notes, and snippets.

@A-Zak
Created May 3, 2020 18:14
Show Gist options
  • Save A-Zak/178361715e210d5f738ae0bf97fc8306 to your computer and use it in GitHub Desktop.
Save A-Zak/178361715e210d5f738ae0bf97fc8306 to your computer and use it in GitHub Desktop.
import * as moment from 'moment'
type DateMlMeasuremnet = {dateString: string, date: moment.Moment, ml: number}
type DailyMlIntakeSummary = { [key: string]: number }
const absDate = (date: moment.Moment): moment.Moment => {
return (moment(date).set({hour: 0, minute: 0, second: 0, millisecond: 0}))
}
const calculateDalyMlIntake = (inputJson: [DateMlMeasuremnet]): DailyMlIntakeSummary => {
let perDayMls = {}
let addMlToDate = (ml, date) => {
let dateIndex = absDate(date).toString()
if (typeof perDayMls[dateIndex] === "undefined")
perDayMls[dateIndex] = ml
else
perDayMls[dateIndex] = perDayMls[dateIndex] + ml
}
for (var i = 0; i < inputJson.length; i++) {
let currDateMl = inputJson[i]
if (currDateMl.ml == 0)
continue
let prevDateMl = inputJson[i-1]
if (currDateMl.date.isSame(prevDateMl.date, "day")) {
addMlToDate(currDateMl.ml, currDateMl.date)
}
else {
let durationFromLast = currDateMl.date.diff(prevDateMl.date)
let durationThisDay = currDateMl.date.diff(absDate(currDateMl.date))
let durationLastDay = durationFromLast - durationThisDay
let mlThisDay = currDateMl.ml / durationFromLast * durationThisDay
let mlLastDay = currDateMl.ml / durationFromLast * durationLastDay
addMlToDate(mlLastDay, prevDateMl.date)
addMlToDate(mlThisDay, currDateMl.date)
// console.log({
// curr: currDateMl.dateString,
// prev: prevDateMl.dateString,
// durationFromLast,
// durationThisDay,
// durationLastDay,
// mlThisDay,
// mlLastDay
// })
}
}
return perDayMls
}
const getDateFromString = (dateString: string): moment.Moment => {
let date = moment(dateString)
let currMonth = moment().month()
let dateMonth = date.month()
let year = dateMonth > currMonth ? 2019 : 2020
//console.log(dateString, " - currMonth", currMonth, "dateMonth", dateMonth, "year", year)
return moment(dateString).year(year)
}
const parseDateMlMeasuremnet = (dateMlCouple: [string, number]): DateMlMeasuremnet => {
return {
dateString: dateMlCouple[0],
date: getDateFromString(dateMlCouple[0]),
ml: dateMlCouple[1]
}
}
const sheetsDateStringFromMomentString = (dateString: string): string => {
return moment(dateString).format("M/D/YYYY")
}
function mufaDailyMlIntakeParser(input) {
let inputJson = JSON.parse(input)
.map(parseDateMlMeasuremnet)
.filter(dateMl => {
return dateMl.dateString != "" && !isNaN(dateMl.ml)
})
let perDayMls = calculateDalyMlIntake(inputJson)
// console.log(perDayMls)
let sheetsExport = Object.keys(perDayMls)
.map((dateStringKey) => {
return [
sheetsDateStringFromMomentString(dateStringKey),
Math.round(perDayMls[dateStringKey])
]
})
return sheetsExport
}
const main = () => {
let input = `[["Date","ml drunk since last"],["Aug 10, 9:00",0],["Aug 10, 19:50",1150],["Aug 11, 11:40",1860],["Aug 11, 23:58",1610],["aug 12, 9:14",230],["Aug 12, 18:21",750],["Aug 13, 9:18",840],["Aug 13, 12:34",1100],["Aug 13, 17:38",380],["Aug 14, 0:40",2000],["Aug 14, 9:16",500],["Aug 14, 9:56",440],["aug 14, 19:03",800],["Aug 15, 2:39",1590],["aug 15, 9:28",900],["Aug 15, 16:08",700],["Aug 15, 20:43",1020],["",""],["",""],["Aug 19, 10:00",0],["Aug 19, 16:20",1500],["Aug 19, 19:32",500],["aug 20, 01:00",600],["Aug 20, 9:00",1150],["Aug 20, 20:14",1700],["aug 21, 1:00",450],["aug 21, 8:20",400],["",""],["Aug 22, 8:00",0],["Aug 22, 9:10",1120],["Aug 22, 17:30",1240],["Aug 22, 20:12",800],["Aug 23, 1:12",1630],["",""],["Aug 24, 8:34",0],["Aug 24, 12:22",2000],["",""],["",""],["Sep 9, 18:15",0],["Sep 9, 23:02",1600],["Sep 10, 7:50",1700],["Sep 10, 13:20",1450],["Sep 10, 19:07",1900],["Sep 11, 03:08",2000],["Sep 11, 08:04",1700],["Sep 11, 13:09",1830],["Sep 11, 20:05",1780],["",""],["",""],["Sep 28, 9:55",0],["sep 28, 12:40",1700],["sep 28, 17:00",1800],["sep 28, 18:00",300],["Sep 29, 2:45",1600],["Sep 29, 11:30",1970],["Sep 29, 16:45",1840],["Sep 29, 23:00",2000],["sep 30, 00:49",1100],["Sep 30, 10:00",2000],["sep 30, 15:05",1550],["Sep 30, 21:28",1640],["Oct 01, 02:30",1700],["Oct 01, 09:58",1020],["Oct 01, 14:28",1480],["Oct 01, 23:45",1630],["Oct 02, 09:20",2000],["Oct 02, 13:45",1410],["",""],["",""],["",""],["Oct 08, 20:30",0],["oct 09, 01:55",1500],["oct 09, 09:14",1100],["oct 09, 13:20",1100],["oct 09, 19:11",1500],["Oct 09, 20:39",400],["Oct 10, 00:30",1500],["Oct 10, 09:05",1270],["oct 10, 13:00",1500],["",""],["",""],["",""],["",""],["Oct 25, 9:32",0],["Oct 25, 11:55",1200],["Oct 25, 14:33",1100],["Oct 25,19:07",320],["Oct 26, 01:32",2000],["oct 26, 11:00",2000],["Oct 26, 14:02",1450],["",""],["",""],["",""],["Oct 29, 03:15",0],["oct 29, 9:30",1400],["Oct 29, 14:12",1500],["oct 29, 18:54",1500],["",""],["",""],["",""],["",""],["Nov 20, 10:50",0],["Nov 20, 14:42",800],["Nov 20, 16:36",1150],["Nov 20, 20:10",650],["Nov 21, 9:46",1700],["Nov 21, 19:25",2000],["Nov 22, 1:43",700],["Nov 22, 9:23",250],["Nov 22, 12:07",800],["Nov 22, 16:32",670],["Nov 22, 19:24",300],["Nov 23, 11:32",2000],["Nov 23, 18:42",1200],["nov 23, 22:32",630],["Nov 24, 13:02",1700],["Nov 24, 13:56",470],["Nov 24, 17:43",800],["Nov 23, 9:31",1270],["nov 25, 11:45",500],["Nov 25, 19:36",1530],["Nov 26, 2:39",800],["Nov 26, 9:34",230],["Nov 26, 14:04",1050],["Nov 27, 2:41",1500],["Nov 27, 9:31",270],["Nov 27, 15:24",1500],["Nov 27, 18:45",600],["Nov 28, 9:24",1060],["Nov 28, 15:06",1320],["",""],["",""],["",""],["Oct 12,13:55",0],["Oct 12, 16:30",390],["Oct 13, 2:08",1200],["Oct 13, 14:53",960],["Oct 14, 12:14",1570],["Oct 14, 22:30",950],["Oct 15, 2:06",370],["Oct 15, 17:40",1050],["Oct 16, 1:54",750],["Oct 16, 18:45",1650],["Oct 17, 2:01",490],["Oct 17, 10:47",700],["Oct 18, 2:07",1800],["oct 18, 12:27",600],["Oct 18, 23:52",1290],["Oct 19, 12:41",750],["",""],["",""],["",""],["Nov 20, 23:05",0],["Nov 21, 09:00",560],["Nov 21, 20:24",1000],["Nov 23, 11:15",1820],["Nov 24, 15:00",2000],["Nov 25,12:04",1700],["Nov 26, 12:15",1450],["Nov 27, 8:20",840],["",""],["",""],["",""],["",""],["",""],["Mar 7, 11:40",0],["Mar 7, 13:33",800],["Mar 7, 20:30",1130],["Mar 8, 8:35",1610],["Mar 8, 13:20",1150],["Mar 8, 17:35",1240],["Mar 8, 23:27",930],["Mar 9, 9:23",1850],["Mar 9, 16:23",1490],["Mar 10, 1:20",1500],["Mar 10, 10:31",1210],["Mar 10, 18:58",1750],["Mar 11, 00:40",700],["Mar 11, 9:54",1030],["Mar 11, 16:39",1400],["Mar 12, 1:38",1250],["Mar 12, 12:52",1440],["Mar 12, 18:48",1190],["Mar 13, 1:02",950],["Mar 13, 14:19",1700],["Mar 13, 17:14",460],["Mar 14, 2:38",1050],["Mar 14, 13:38",1120],["Mar 14, 16:42",700],["",""],["",""],["",""],["Mar 20, 10:03",0],["Mar 20, 21:02",1252],["Mar 21, 1:58",600],["Mar 21, 12:01",1000],["Mar 22, 2:02",1340],["Mar 22, 11:32",760],["Mar 23, 4:39",1500],["Mar 23, 17:16",1300],["Mar 24, 2:43",1450],["Mar 24, 11:40",450],["Mar 24, 18:29",1310],["Mar 25, 0:54",1090],["Mar 25, 16:15",1100],["Mar 26, 10:58",2000],["Mar 26, 16:53",1260],["Mar 26, 22:25",1100],["Mar 27, 7:05",1350],["Mar 27, 14:45",1280],["Mar 28, 1:57",1070],["Mar 28, 16:55",1150],["Mar 29, 2:30",1700],["Mar 29, 12:17",850],["Mar 30, 00:40",1300],["Mar 30, 10:03",700],["Mar 30, 14:53",990],["Mar 31, 2:32",1600],["Mar 31, 15:20",1600],["Mar 31, 22:52",2000],["Apr 1, 2:43",750],["Apr 1, 10:43",680],["Apr 1, 20:41",1600],["Apr 2, 9:39",1150],["Apr 2, 19:37",2000],["Apr 3, 2:33",1680],["Apr 3, 10:15",380],["Apr 3, 16:59",600],["Apr 3, 20:34",620],["Apr 4, 2:10",980],["Apr 4, 11:01",650],["Apr 4, 18:11",990],["Apr 5, 2:18",1000],["Apr 5, 16:32",1500],["Apr 6, 00:48",500],["Apr 6, 10:58",1600],["Apr 6, 21:25",2000],["Apr 7, 2:21",1520],["Apr 7, 10:39",500],["Apr 7, 18:24",2000],["Apr 8, 3:07",1130],["Apr 8, 19:55",1900],["Apr 9, 1:38",1700],["Apr 9, 10:14",920],["Apr 9, 15:04",1490],["Apr 9, 22:15",1550],["Apr 10, 2:30",720],["",""],["Apr 10, 19:31",1750],["Apr 11, 2:41",1440],["Apr 11, 11:10",1020],["Apr 11, 21:28",2000],["Apr 12, 1:58",1100],["Apr 12, 12:07",1730],["Apr 12, 15:55",1000],["Apr 12, 22:07",1850],["Apr 13, 2:06",900],["Apr 13, 9:48",1000],["",""],["Apr 13, 16:56",1365],["Apr 14, 0:29",2000],["Apr 14, 9:08",1180],["Apr 14, 18:42",2000],["Apr 15, 2:51",2000],["Apr 15, 10:09",1150],["Apr 15, 13:52",1280],["Apr 16, 1:46",2000],["Apr 16, 13:46",2000],["Apr 16, 22:17",1300],["Apr 17, 2:00",850],["Apr 17, 12:56",1550],["Apr 17, 20:11",1550],["Apr 18, 2:16",800],["Apr 18, 10:06",780],["Apr 18, 18:22",2000],["Apr 19, 2:16",2000],["Apr 19, 10:05",850],["Apr 19, 17:33",2000],["Apr 20, 1:25",1570],["Apr 20, 11:48",2000],["Apr 20, 20:53",2000],["Apr 21, 6:30",1830],["Apr 21, 10:30",1320],["Apr 21, 14:32",760],["Apr 21, 22:18",2000],["Apr 22, 3:15",1150],["Apr 22, 15:36",2000],["Apr 22, 21:48",2000],["Apr 23, 9:50",2000],["Apr 23, 13:32",1090],["Apr 24, 00:33",2000],["Apr 24, 3:36",1150],["Apr 24, 13:52",2000],["Apr 24, 19:51",1900],["Apr 25, 0:40",1900],["Apr 25, 16:58",2000],["Apr 26, 0:43",1700],["Apr 26, 11:49",1070],["Apr 26, 22:06",900],["Apr 27, 11:59",1350],["Apr 27, 18:15",2000],["Apr 28, 3:30",710],["Apr 28, 13:44",1180],["Apr 28, 19:33",900],["Apr 29, 6:43",1690],["Apr 29, 13:06",1220],["Apr 30, 0:20",2250],["Apr 30, 15:56",2300],["May 1, 0:42",2000],["May 1, 9:12",400],["May 1, 13:43",1530],["May 1, 19:32",820],["May 2, 11:46",1350],["May 2, 21:11",2000],["May 3, 8:55",1210],["May 3, 13:26",870]]`
// let input = `[["Date","ml drunk since last"],["Aug 10, 9:00",0],["Aug 10, 19:50",1150],["Aug 11, 11:40",1860],["Aug 11, 23:58",1610],["aug 12, 9:14",230],["Aug 12, 18:21",750],["Aug 13, 9:18",840],["Aug 13, 12:34",1100],["Aug 13, 17:38",380],["Aug 14, 0:40",2000],["Aug 14, 9:16",500],["Aug 14, 9:56",440],["aug 14, 19:03",800]]`
// let input = `[["Date","ml drunk since last"],["Aug 9, 9:00",0],["Aug 10, 9:00",1000]]`
let sheetsExport = mufaDailyMlIntakeParser(input)
console.log(sheetsExport)
console.log("EOMain")
}
main()
@mrsufgi
Copy link

mrsufgi commented May 3, 2020

Looks good overall, I would just give it a quick lint to unify your style (for easy maintenance). It will also solve a few let vs const issues.
Also, be aware that moment is mutable :O so pay attention when you set the date and return the same object.

<3

@mrsufgi
Copy link

mrsufgi commented May 3, 2020

const addMlToDate = (ml, date) => {
        const dateIndex = absDate(date).toString();
        const dayMls = perDayMls[dateIndex];
        perDayMls[dateIndex] = dayMls ? dayMls + ml : ml
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment