Skip to content

Commit

Permalink
Optimize nextEvent
Browse files Browse the repository at this point in the history
PR-URL: #161
  • Loading branch information
tshemsedinov committed Jun 18, 2023
1 parent a4d32be commit b385d66
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions lib/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ const parseDay = (s) => {

const ORDINAL = ['st', 'nd', 'rd', 'th'];

const isOrdinal = (s) => {
for (const d of ORDINAL) {
if (s.endsWith(d)) return true;
}
return false;
};
const isOrdinal = (s) => ORDINAL.some((d) => s.endsWith(d));

const YEAR_LEN = 4;

Expand Down Expand Up @@ -132,39 +127,34 @@ const parseEvery = (s = '') => {
return { YY, MM, DD, wd, hh, mm, ms: ms > 0 ? ms * 1000 : -1 };
};

const nextEvent = (every, date = new Date()) => {
const nextEvent = (ev, d = new Date()) => {
let ms = 0;
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;
if (every.hh > -1) {
if (every.hh < hh) return -1;
if (every.hh === hh) {
if (every.mm > -1 && every.mm < mm) return -1;
}
}
}
}
}
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;
const Y = d.getUTCFullYear();
const M = d.getUTCMonth() + 1;
const D = d.getUTCDate();
const w = d.getUTCDay() + 1;
const h = d.getUTCHours();
const m = d.getUTCMinutes();

const iY = ev.YY > -1;
const iM = ev.MM > -1;
const iD = ev.DD > -1;
const iw = ev.wd > -1;
const ih = ev.hh > -1;
const im = ev.mm > -1;
const ims = ev.ms > -1;

if (iY && (ev.YY < Y || ev.YY > Y)) return ev.YY < Y ? -1 : 0;
if (iM && (ev.MM < M || ev.MM > M || ev.MM !== M)) return ev.MM < M ? -1 : 0;
if (iD && (ev.DD < D || ev.DD > D || ev.DD !== D)) return ev.DD < D ? -1 : 0;
if (iw && ev.wd !== w) return 0;
if (ih && (ev.hh < h || (ev.hh === h && im && ev.mm < m))) return -1;

if (ih) ms += (ev.hh - h) * DURATION_UNITS.h;
if (im) ms += (ev.mm - m) * DURATION_UNITS.m;

ms *= 1000;
if (every.ms > -1) ms += every.ms;
if (ims) ms += ev.ms;
return ms;
};

Expand Down

1 comment on commit b385d66

@AntiHero
Copy link

@AntiHero AntiHero commented on b385d66 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

мне кажется, @tshemsedinov, Вы бы написали код не хуже, взять тот же метод .some, лично я не помню, когда последний раз на практике его использовал, такое просто вылетает из головы (в моём случае)), а с нейросетью такое не прокатит)

P.S. а можно попробовать вот это:

if (iY && (ev.YY < Y || ev.YY > Y)) return ev.YY < Y ? -1 : 0;

заменить на это ?

if (iY && ev.YY !== Y) return ev.YY < Y ? -1 : 0;

мне кажется Chat подобавлял пару лишних логических сравнений или мб это я туплю)

Please sign in to comment.