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

Broken toString() conversion when using string in byweekday parameter #493

Open
AntonyChiossi opened this issue Feb 7, 2022 · 5 comments

Comments

@AntonyChiossi
Copy link

Reporting an issue

Thank you for taking an interest in rrule! Please include the following in
your report:

  • version used: 2.6.8
  • OS: any
  • expected: when ByWeekday is specified as WeekdayStr (a enum string like 'MO') rule.toString() should not fail printing undefined
const { RRule, RRuleSet, rrulestr } = require('rrule');
 
// Create a rule:
const rule = new RRule({
  freq: RRule.WEEKLY,
  interval: 5,
  byweekday: ['SA'],
  dtstart: new Date(Date.UTC(2012, 1, 1, 10, 30)),
  until: new Date(Date.UTC(2012, 12, 31))
})

console.log(rule.toString());

"DTSTART:20120201T103000Z\nRRULE:FREQ=WEEKLY;INTERVAL=5;BYDAY=undefined;UNTIL=20130131T000000Z"

@KrisLau
Copy link

KrisLau commented Mar 30, 2022

Try using rrulestr #335 (comment) instead

@Meligy
Copy link

Meligy commented Aug 1, 2022

I have the same problem, and the comment before does not help. RRuleSet takes an RRule still, and the toString() seems to still have the same issue as RRule, and using .exrule() instead of .rrule() didn't seem to make a difference.

@ekilah
Copy link
Contributor

ekilah commented Oct 3, 2022

seems like the tests in my older PR #371 (that allowed using e.g. 'MO') should cover the toString output, and there is a bug to fix here

@equal-matt
Copy link

equal-matt commented Nov 16, 2022

I'm seeing this too. I've tried both v2.7.0 and v2.7.1 now. For me, the workaround was to always normalise the weekday representations as the Weekday type. Here's my file in case anyone else needs them:

import { ByWeekday, RRule, Weekday, WeekdayStr } from "rrule";
import isDefined from "../isDefined"; // this is a type guard

function numberToWeekdayString(input: number): Weekday {
  switch (input) {
    case 0:
      return RRule.MO;
    case 1:
      return RRule.TU;
    case 2:
      return RRule.WE;
    case 3:
      return RRule.TH;
    case 4:
      return RRule.FR;
    case 5:
      return RRule.SA;
    case 6:
      return RRule.SU;
    default:
      throw `Unknown weekday number ${input}`;
  }
}

const WEEKDAY_STRINGS = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
function stringToWeekday(input: typeof WEEKDAY_STRINGS[number]): Weekday {
  switch (input) {
    case "MO":
      return RRule.MO;
    case "TU":
      return RRule.TU;
    case "WE":
      return RRule.WE;
    case "TH":
      return RRule.TH;
    case "FR":
      return RRule.FR;
    case "SA":
      return RRule.SA;
    case "SU":
      return RRule.SU;
    default:
      throw `Unknown weekday number ${input}`;
  }
}

type NormaliseByWeekdayInput =
  | ByWeekday
  | string
  | {
      weekday: number;
    }
  | null
  | undefined;

export function normaliseByWeekdayItem(
  input: NormaliseByWeekdayInput
): Weekday | undefined {
  if (!input) return undefined;
  if (typeof input === "object") {
    return numberToWeekdayString(input.weekday);
  }
  if (typeof input === "number") {
    return numberToWeekdayString(input);
  }
  if (typeof input === "string") {
    return stringToWeekday(input);
  }
  return input;
}

export function normaliseByWeekday(
  input: NormaliseByWeekdayInput | NormaliseByWeekdayInput[]
) {
  if (Array.isArray(input))
    return input.map(normaliseByWeekdayItem).filter(isDefined);
  return normaliseByWeekdayItem(input);
}

@ice-cap0
Copy link

Getting this bug too, rule.toString doesn't parse byweekday properly when you pass it WeekdayStr[], e.g. ["MO", "FR"].

Should be fixed either in the code or in the docs but in the meantime, simple fix

  byweekday: selectedWeekdayStrs.map(b => RRule[b])

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

No branches or pull requests

6 participants