Skip to content

Instantly share code, notes, and snippets.

@CharlyJazz
Created April 7, 2020 00:18
Show Gist options
  • Select an option

  • Save CharlyJazz/60ade29934af3e49ad9d6f147cc60612 to your computer and use it in GitHub Desktop.

Select an option

Save CharlyJazz/60ade29934af3e49ad9d6f147cc60612 to your computer and use it in GitHub Desktop.
algoritmo muy malo para obtener datos del ciclo menstrual
export const EN_PERIODO = "EN PERIODO";
export const OVULANDO = "OVULANDO";
export const INFERTIL = "INFERTIL";
const getGetOrdinal = function(n) {
var s = ["th", "st", "nd", "rd"],
v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
const asyncGetPeriodInformation = (
dateString,
longMenstrualCycle,
longPeriodDays
) =>
new Promise((resolve, reject) => {
if (
!Number.isInteger(longPeriodDays) ||
!Number.isInteger(longMenstrualCycle)
) {
return reject({
error: "longMenstrualCycle and longPeriodDays must be integers"
});
}
if (
!/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/.test(dateString)
) {
return reject({ error: "Date format is not valid" });
}
const DATE_STRING = dateString;
const LMC = longMenstrualCycle; // LONG MENSTRUAL CYCLE
const LPD = longPeriodDays; // LONG PERIOD DAYS
const CD = new Date(); // Dia de hoy
const LSTPD = new Date(DATE_STRING); // Se le resta el LPD Para tener el primer dia de sangrado
if (LSTPD.getTime() > CD.getTime()) {
return reject({ error: "Date more big than today" });
}
const getDaysBetween = (d1, d2) =>
(d2.getTime() - d1.getTime()) / (1000 * 3600 * 24);
const getCurrentPhase = (
currentDate = CD,
lastPeriodDayOfLastCycle = LSTPD,
longMenstrualCycle = LMC,
longPeriodDays = LPD
) => {
const _lastPeriodDayOfLastCycle = lastPeriodDayOfLastCycle;
const lastFirstDayInPeriod = new Date(
_lastPeriodDayOfLastCycle.setDate(
_lastPeriodDayOfLastCycle.getDate() - longPeriodDays
)
);
const daysBetween = Math.floor(
getDaysBetween(lastFirstDayInPeriod, currentDate)
);
return daysBetween <= longMenstrualCycle
? daysBetween
: Math.floor(daysBetween % longMenstrualCycle);
};
const currentDayInCycle = getCurrentPhase();
const lastDayOfFirstInfertil = LPD + 6;
const lastDayOvulando = lastDayOfFirstInfertil + 6;
let CURRENT_STATE = EN_PERIODO;
for (let dia = 1; dia <= LMC; dia++) {
if (dia <= LPD && currentDayInCycle === dia) {
// en periodo
break;
} else if (
dia > LPD &&
dia <= lastDayOfFirstInfertil &&
currentDayInCycle === dia
) {
// dias infertiles
CURRENT_STATE = INFERTIL;
break;
} else if (
dia > lastDayOfFirstInfertil &&
dia <= lastDayOvulando &&
currentDayInCycle === dia
) {
// ovulandito
CURRENT_STATE = OVULANDO;
break;
} else if (dia > lastDayOvulando) {
// dias infertiles
CURRENT_STATE = INFERTIL;
break;
}
}
const cuantosDiasFaltanParaNuevoCiclo = (currentDay, cycleDays) => {
return cycleDays - currentDay;
};
const getDaysLeftToOvulation = (currentDay, cycleDays) => {
if (currentDay <= lastDayOfFirstInfertil) {
return lastDayOfFirstInfertil - currentDay + 1;
} else {
return cuantosDiasFaltanParaNuevoCiclo(currentDay, cycleDays) + 11;
}
};
const informationAbout = {
daysLeftToNewCycle: cuantosDiasFaltanParaNuevoCiclo(
currentDayInCycle,
LMC
)
};
if (CURRENT_STATE === EN_PERIODO) {
informationAbout["periodOrdinalDay"] = getGetOrdinal(currentDayInCycle);
}
if (CURRENT_STATE === INFERTIL || CURRENT_STATE === EN_PERIODO) {
informationAbout["daysLeftToOvulation"] = getDaysLeftToOvulation(
currentDayInCycle,
LMC
);
}
return resolve({
currentState: CURRENT_STATE,
currentDay: currentDayInCycle,
...informationAbout
});
});
export default asyncGetPeriodInformation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment