cal icon indicating copy to clipboard operation
cal copied to clipboard

Expose business calendar `workday` variable via a method so that hooks can use it

Open gavingolden opened this issue 1 year ago • 0 comments

Request

Expose the business calendar's workday array via a method.

Details

workday is currently private so custom workday function hooks are unable to reference it.

I'd like to implement a custom WorkdayFunc while still leveraging the existing workday array pattern. In this case I can't use IsWorkday in the custom hook because it would lead to an infinite loop. It's a small array so exposing it via a method with an array-copy would prevent the underlying array from being modified arbitrarily by the caller.

func (c *BusinessCalendar) GetWorkdays() [7]bool {
  clone := c.workday
  return clone
}

// ...

c := cal.NewBusinessCalendar()
c.SetWorkday(time.Sunday, true)

c.WorkdayFunc = func(date time.Time) bool {
  // Do something custom
  // ...
  return c.GetWorkdays()[date.Weekday()] // <-- here
}

gavingolden avatar Jul 14 '23 14:07 gavingolden