Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize parseEvery & nextEvent #125

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 49 additions & 36 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,64 +235,75 @@ const parseDay = (s) => {
const YEAR_LEN = 4;

const parseEvery = (s = '') => {
let YY = -1;
let MM = -1;
let DD = -1;
let wd = -1;
let hh = -1;
let mm = -1;
let ms = 0;
const every = { YY: -1, MM: -1, DD: -1, wd: -1, hh: -1, mm: -1, ms: 0 };

const parts = s.split(' ');
for (const part of parts) {
if (part.includes(':')) {
const [h, m] = split(part, ':');
if (h !== '') hh = parseInt(h);
mm = m === '' ? 0 : parseInt(m);
if (h !== '') every.hh = parseInt(h);
every.mm = m === '' ? 0 : parseInt(m);
continue;
}
if (isOrdinal(part)) {
DD = parseInt(part);
every.DD = parseInt(part);
continue;
}
if (part.length === YEAR_LEN) {
YY = parseInt(part);
every.YY = parseInt(part);
continue;
}
if (MM === -1) {
MM = parseMonth(part);
if (MM > -1) continue;
if (every.MM === -1) {
every.MM = parseMonth(part);
if (every.MM > -1) continue;
}
if (wd === -1) {
wd = parseDay(part);
if (wd > -1) continue;
if (every.wd === -1) {
every.wd = parseDay(part);
if (every.wd > -1) continue;
}
const unit = part.slice(-1);
const mult = DURATION_UNITS[unit];

const mult = DURATION_UNITS[part.slice(-1)];
if (typeof mult === 'number') {
const value = parseInt(part);
if (!isNaN(value)) ms += value * mult;
if (!isNaN(value)) every.ms += value * mult;
}
}
return { YY, MM, DD, wd, hh, mm, ms: ms > 0 ? ms * 1000 : -1 };

return { ...every, ms: every.ms > 0 ? every.ms * 1000 : -1 };
};

const nextEvent = (every, date = new Date()) => {
const isMoreLess = (a, b) => {
if (a === -1) return -2;
if (a > b) return 0;
if (a < b) return -1;
return 1;
};

const getTimeMs = (every, hh, mm) => {
let ms = 0;

if (every.hh > -1) ms += (every.hh - hh) * DURATION_UNITS.h;
if (every.mm > -1) ms += (every.mm - mm) * DURATION_UNITS.m;
ms *= 1000;
if (every.ms > -1) ms += every.ms;

return ms;
};

const nextEvent = (every, date = new Date()) => {
const YY = date.getUTCFullYear();
const MM = date.getUTCMonth() + 1;
const DD = date.getUTCDate();
const wd = date.getUTCDay() + 1;
const hh = date.getUTCHours();
const mm = date.getUTCMinutes();
if (every.YY > -1) {
if (every.YY < YY) return -1;
if (every.YY > YY) return 0;
if (every.MM > -1) {
if (every.MM < MM) return -1;
if (every.MM > MM) return 0;
if (every.DD > -1) {
if (every.DD < DD) return -1;
if (every.DD > DD) return 0;

let res = isMoreLess(every.YY, YY);
if (res > 0) {
res = isMoreLess(every.MM, MM);
if (res > 0) {
res = isMoreLess(every.DD, DD);
if (res > 0) {
if (every.hh > -1) {
if (every.hh < hh) return -1;
if (every.hh === hh) {
Expand All @@ -302,14 +313,16 @@ const nextEvent = (every, date = new Date()) => {
}
}
}

if (res === -1 || res === 0) {
return res;
}

if (every.MM > -1 && every.MM !== MM) return 0;
if (every.DD > -1 && every.DD !== DD) return 0;
if (every.wd > -1 && every.wd !== wd) return 0;
if (every.hh > -1) ms += (every.hh - hh) * DURATION_UNITS.h;
if (every.mm > -1) ms += (every.mm - mm) * DURATION_UNITS.m;
ms *= 1000;
if (every.ms > -1) ms += every.ms;
return ms;

return getTimeMs({ hh: every.hh, mm: every.mm, ms: every.ms }, hh, mm);
};

const makePrivate = (instance) => {
Expand Down