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 #123

Open
tshemsedinov opened this issue Jul 30, 2022 · 1 comment
Open

Optimize parseEvery & nextEvent #123

tshemsedinov opened this issue Jul 30, 2022 · 1 comment

Comments

@tshemsedinov
Copy link
Member

I don't like how this functions are implemented now:

  • parseEvery:

    metautil/lib/utilities.js

    Lines 234 to 274 in a0bc899

    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 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);
    continue;
    }
    if (isOrdinal(part)) {
    DD = parseInt(part);
    continue;
    }
    if (part.length === YEAR_LEN) {
    YY = parseInt(part);
    continue;
    }
    if (MM === -1) {
    MM = parseMonth(part);
    if (MM > -1) continue;
    }
    if (wd === -1) {
    wd = parseDay(part);
    if (wd > -1) continue;
    }
    const unit = part.slice(-1);
    const mult = DURATION_UNITS[unit];
    if (typeof mult === 'number') {
    const value = parseInt(part);
    if (!isNaN(value)) ms += value * mult;
    }
    }
    return { YY, MM, DD, wd, hh, mm, ms: ms > 0 ? ms * 1000 : -1 };
    };
  • nextEvent:

    metautil/lib/utilities.js

    Lines 276 to 310 in a0bc899

    const nextEvent = (every, date = 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;
    ms *= 1000;
    if (every.ms > -1) ms += every.ms;
    return ms;
    };

    But we need tests before optimization: Add tests for parseEvery & nextEvent #122
@AlexBelch
Copy link
Contributor

@tshemsedinov
I want try to refactor this funcs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants