Skip to content

Commit

Permalink
init: start issue metarhia#123, refactor func parseEvery
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander BELCHITSKIY committed Aug 11, 2022
1 parent 346ae4c commit b45f81e
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,45 +235,43 @@ 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 };

every.ms = every.ms > 0 ? every.ms * 1000 : -1;

return every;
};

const nextEvent = (every, date = new Date()) => {
Expand Down

0 comments on commit b45f81e

Please sign in to comment.