Chore/prettify for joda

This commit is contained in:
Sofiya Tepikin
2022-07-27 17:57:41 +00:00
parent 1bd00a7ec2
commit 4e8c0080e6
6 changed files with 130 additions and 128 deletions
+28 -29
View File
@@ -30,7 +30,9 @@ export default function config(opts) {
}
function getLastMensesStartForDay(targetDateString) {
return cycleStartsSortedByDate.find(start => start.date <= targetDateString)
return cycleStartsSortedByDate.find(
(start) => start.date <= targetDateString
)
}
function getCycleDayNumber(targetDateString) {
@@ -55,25 +57,25 @@ export default function config(opts) {
}
function getCyclesBefore(targetCycleStartDay) {
const startFromHere = cycleStartsSortedByDate.findIndex(start => {
const startFromHere = cycleStartsSortedByDate.findIndex((start) => {
return start.date < targetCycleStartDay.date
})
if (startFromHere < 0) return null
return cycleStartsSortedByDate
.slice(startFromHere)
.map(start => getCycleByStartDay(start))
// filter the ones exceeding maxCycleLength, those are null
.filter(cycle => cycle)
return (
cycleStartsSortedByDate
.slice(startFromHere)
.map((start) => getCycleByStartDay(start))
// filter the ones exceeding maxCycleLength, those are null
.filter((cycle) => cycle)
)
}
function findIndexOfDay(day, daysSortedByDate) {
return daysSortedByDate.findIndex(d => d.date === day.date)
return daysSortedByDate.findIndex((d) => d.date === day.date)
}
function getNextCycleStartDay(startDay, cycleStartsSortedByDate) {
const cycleStartIndex = findIndexOfDay(
startDay,
cycleStartsSortedByDate)
const cycleStartIndex = findIndexOfDay(startDay, cycleStartsSortedByDate)
return cycleStartsSortedByDate[cycleStartIndex - 1]
}
@@ -82,8 +84,7 @@ export default function config(opts) {
}
function getCycleLength(startDate, endDate) {
return LocalDate.parse(startDate)
.until(LocalDate.parse(endDate), DAYS)
return LocalDate.parse(startDate).until(LocalDate.parse(endDate), DAYS)
}
function isValidCycle(startDate, endDate) {
@@ -109,19 +110,16 @@ export default function config(opts) {
}
if (isValidCycle(startDay.date, cycleEndDate)) {
const cycleStartIndex = findIndexOfDay(
startDay,
cycleDaysSortedByDate
)
return cycleDaysSortedByDate
.slice(cycleEndIndex, cycleStartIndex + 1)
const cycleStartIndex = findIndexOfDay(startDay, cycleDaysSortedByDate)
return cycleDaysSortedByDate.slice(cycleEndIndex, cycleStartIndex + 1)
}
return null
}
function getCycleForDay(dayOrDate, todayDate) {
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const dateString =
typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStartForDay(dateString)
if (!cycleStart) return null
return getCycleByStartDay(cycleStart, todayDate)
@@ -138,9 +136,9 @@ export default function config(opts) {
const localDate = LocalDate.parse(cycleDay.date)
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
const bleedingDays = bleedingDaysSortedByDate
const index = bleedingDays.findIndex(day => day.date === cycleDay.date)
const index = bleedingDays.findIndex((day) => day.date === cycleDay.date)
const candidates = bleedingDays.slice(index + 1)
return !candidates.some(day => {
return !candidates.some((day) => {
return day.date >= threshold && !day.bleeding.exclude
})
}
@@ -151,9 +149,9 @@ export default function config(opts) {
// changes
function getMensesDaysRightAfter(cycleDay) {
const bleedingDays = bleedingDaysSortedByDate
.filter(d => !d.bleeding.exclude)
.filter((d) => !d.bleeding.exclude)
.reverse()
const firstFollowingBleedingDayIndex = bleedingDays.findIndex(day => {
const firstFollowingBleedingDayIndex = bleedingDays.findIndex((day) => {
return day.date > cycleDay.date
})
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
@@ -179,13 +177,13 @@ export default function config(opts) {
function getAllCycleLengths() {
return cycleStartsSortedByDate
.map(day => LocalDate.parse(day.date))
.map((day) => LocalDate.parse(day.date))
.map((cycleStart, i, startsAsLocalDates) => {
if (i === cycleStartsSortedByDate.length - 1) return null
const prevCycleStart = startsAsLocalDates[i + 1]
return prevCycleStart.until(cycleStart, DAYS)
})
.filter(length => length && length <= maxCycleLength)
.filter((length) => length && length <= maxCycleLength)
}
function getPredictedMenses() {
@@ -198,12 +196,14 @@ export default function config(opts) {
let periodStartVariation
if (cycleInfo.stdDeviation === null) {
periodStartVariation = 2
} else if (cycleInfo.stdDeviation < 1.5) { // threshold is chosen a little arbitrarily
} else if (cycleInfo.stdDeviation < 1.5) {
// threshold is chosen a little arbitrarily
periodStartVariation = 1
} else {
periodStartVariation = 2
}
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
if (periodDistance - 5 < periodStartVariation) {
// otherwise predictions overlap
return []
}
const allMensesStarts = cycleStartsSortedByDate
@@ -222,7 +222,6 @@ export default function config(opts) {
return predictedMenses
}
return {
getCycleDayNumber,
getCycleForDay,
+16 -11
View File
@@ -4,7 +4,7 @@ import {
getSchema,
tryToImportWithDelete,
tryToImportWithoutDelete,
updateCycleStartsForAllCycleDays
updateCycleStartsForAllCycleDays,
} from '../../db'
import getColumnNamesForCsv from './get-csv-column-names'
import { LocalDate } from 'js-joda'
@@ -12,7 +12,7 @@ import labels from '../../i18n/en/settings'
export default async function importCsv(csv, deleteFirst) {
const parseFuncs = {
bool: val => {
bool: (val) => {
if (val.toLowerCase() === 'true') return true
if (val.toLowerCase() === 'false') return false
return val
@@ -20,7 +20,7 @@ export default async function importCsv(csv, deleteFirst) {
int: parseNumberIfPossible,
float: parseNumberIfPossible,
double: parseNumberIfPossible,
string: val => val
string: (val) => val,
}
function parseNumberIfPossible(val) {
@@ -36,12 +36,12 @@ export default async function importCsv(csv, deleteFirst) {
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
const path = colName.split('.')
const dbType = getDbType(schema.CycleDay, path)
acc[colName] = item => {
acc[colName] = (item) => {
if (item === '') return null
return parseFuncs[dbType](item)
}
return acc
}, {})
}, {}),
}
const cycleDays = await csvParser(config)
@@ -62,9 +62,11 @@ export default async function importCsv(csv, deleteFirst) {
function validateHeaders(headers) {
const expectedHeaders = getColumnNamesForCsv()
if (!headers.every(header => {
return expectedHeaders.indexOf(header) > -1
})) {
if (
!headers.every((header) => {
return expectedHeaders.indexOf(header) > -1
})
) {
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
throw new Error(msg)
}
@@ -76,7 +78,7 @@ function putNullForEmptySymptoms(data) {
function replaceWithNullIfAllPropertiesAreNull(obj) {
Object.keys(obj).forEach((key) => {
if (!isObject(obj[key])) return
if (Object.values(obj[key]).every(val => val === null)) {
if (Object.values(obj[key]).every((val) => val === null)) {
obj[key] = null
return
}
@@ -97,8 +99,11 @@ function throwIfFutureData(cycleDays) {
for (const i in cycleDays) {
const day = cycleDays[i]
// notes are allowed for future dates but everything else isn't
if (day.date > today && Object.keys(day).some(symptom => symptom != 'date' && symptom != 'note')) {
if (
day.date > today &&
Object.keys(day).some((symptom) => symptom != 'date' && symptom != 'note')
) {
throw new Error(labels.import.errors.futureEdit)
}
}
}
}