weekdayRange.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ((0, util_1.isGMT)(gmt)) {
  53. useGMTzone = true;
  54. }
  55. else if ((0, 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. const todaysDay = getTodaysDay(useGMTzone);
  64. let result;
  65. if (wd2Index < 0) {
  66. result = todaysDay === wd1Index;
  67. }
  68. else if (wd1Index <= wd2Index) {
  69. result = valueInRange(wd1Index, todaysDay, wd2Index);
  70. }
  71. else {
  72. result =
  73. valueInRange(wd1Index, todaysDay, 6) ||
  74. valueInRange(0, todaysDay, wd2Index);
  75. }
  76. return result;
  77. }
  78. exports.default = weekdayRange;
  79. function getTodaysDay(gmt) {
  80. return gmt ? new Date().getUTCDay() : new Date().getDay();
  81. }
  82. // start <= value <= finish
  83. function valueInRange(start, value, finish) {
  84. return start <= value && value <= finish;
  85. }
  86. function isWeekday(v) {
  87. if (!v)
  88. return false;
  89. return weekdays.includes(v);
  90. }
  91. //# sourceMappingURL=weekdayRange.js.map