weekdayRange.js
 1  "use strict";
 2  Object.defineProperty(exports, "__esModule", { value: true });
 3  const util_1 = require("./util");
 4  const weekdays = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
 5  /**
 6   * Only the first parameter is mandatory. Either the second, the third, or both
 7   * may be left out.
 8   *
 9   * If only one parameter is present, the function yeilds a true value on the
10   * weekday that the parameter represents. If the string "GMT" is specified as
11   * a second parameter, times are taken to be in GMT, otherwise in local timezone.
12   *
13   * If both wd1 and wd1 are defined, the condition is true if the current weekday
14   * is in between those two weekdays. Bounds are inclusive. If the "GMT" parameter
15   * is specified, times are taken to be in GMT, otherwise the local timezone is
16   * used.
17   *
18   * Valid "weekday strings" are:
19   *
20   *     SUN MON TUE WED THU FRI SAT
21   *
22   * Examples:
23   *
24   * ``` js
25   * weekdayRange("MON", "FRI")
26   * true Monday trhough Friday (local timezone).
27   *
28   * weekdayRange("MON", "FRI", "GMT")
29   * same as above, but GMT timezone.
30   *
31   * weekdayRange("SAT")
32   * true on Saturdays local time.
33   *
34   * weekdayRange("SAT", "GMT")
35   * true on Saturdays GMT time.
36   *
37   * weekdayRange("FRI", "MON")
38   * true Friday through Monday (note, order does matter!).
39   * ```
40   *
41   *
42   * @param {String} wd1 one of the weekday strings.
43   * @param {String} wd2 one of the weekday strings.
44   * @param {String} gmt is either the string: GMT or is left out.
45   * @return {Boolean}
46   */
47  function weekdayRange(wd1, wd2, gmt) {
48      let useGMTzone = false;
49      let wd1Index = -1;
50      let wd2Index = -1;
51      let wd2IsGmt = false;
52      if (util_1.isGMT(gmt)) {
53          useGMTzone = true;
54      }
55      else if (util_1.isGMT(wd2)) {
56          useGMTzone = true;
57          wd2IsGmt = true;
58      }
59      wd1Index = weekdays.indexOf(wd1);
60      if (!wd2IsGmt && isWeekday(wd2)) {
61          wd2Index = weekdays.indexOf(wd2);
62      }
63      let todaysDay = getTodaysDay(useGMTzone);
64      let result = false;
65      if (wd2Index < 0) {
66          result = todaysDay == wd1Index;
67      }
68      else {
69          if (wd1Index <= wd2Index) {
70              result = valueInRange(wd1Index, todaysDay, wd2Index);
71          }
72          else {
73              result =
74                  valueInRange(wd1Index, todaysDay, 6) ||
75                      valueInRange(0, todaysDay, wd2Index);
76          }
77      }
78      return result;
79  }
80  exports.default = weekdayRange;
81  function getTodaysDay(gmt) {
82      return gmt ? new Date().getUTCDay() : new Date().getDay();
83  }
84  // start <= value <= finish
85  function valueInRange(start, value, finish) {
86      return start <= value && value <= finish;
87  }
88  function isWeekday(v) {
89      return weekdays.indexOf(v) !== -1;
90  }
91  //# sourceMappingURL=weekdayRange.js.map