index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import ToText, { DateFormatter, GetText } from './totext'
  2. import parseText from './parsetext'
  3. import { RRule } from '../rrule'
  4. import { Frequency } from '../types'
  5. import ENGLISH, { Language } from './i18n'
  6. /* !
  7. * rrule.js - Library for working with recurrence rules for calendar dates.
  8. * https://github.com/jakubroztocil/rrule
  9. *
  10. * Copyright 2010, Jakub Roztocil and Lars Schoning
  11. * Licenced under the BSD licence.
  12. * https://github.com/jakubroztocil/rrule/blob/master/LICENCE
  13. *
  14. */
  15. /**
  16. *
  17. * Implementation of RRule.fromText() and RRule::toText().
  18. *
  19. *
  20. * On the client side, this file needs to be included
  21. * when those functions are used.
  22. *
  23. */
  24. // =============================================================================
  25. // fromText
  26. // =============================================================================
  27. /**
  28. * Will be able to convert some of the below described rules from
  29. * text format to a rule object.
  30. *
  31. *
  32. * RULES
  33. *
  34. * Every ([n])
  35. * day(s)
  36. * | [weekday], ..., (and) [weekday]
  37. * | weekday(s)
  38. * | week(s)
  39. * | month(s)
  40. * | [month], ..., (and) [month]
  41. * | year(s)
  42. *
  43. *
  44. * Plus 0, 1, or multiple of these:
  45. *
  46. * on [weekday], ..., (or) [weekday] the [monthday], [monthday], ... (or) [monthday]
  47. *
  48. * on [weekday], ..., (and) [weekday]
  49. *
  50. * on the [monthday], [monthday], ... (and) [monthday] (day of the month)
  51. *
  52. * on the [nth-weekday], ..., (and) [nth-weekday] (of the month/year)
  53. *
  54. *
  55. * Plus 0 or 1 of these:
  56. *
  57. * for [n] time(s)
  58. *
  59. * until [date]
  60. *
  61. * Plus (.)
  62. *
  63. *
  64. * Definitely no supported for parsing:
  65. *
  66. * (for year):
  67. * in week(s) [n], ..., (and) [n]
  68. *
  69. * on the [yearday], ..., (and) [n] day of the year
  70. * on day [yearday], ..., (and) [n]
  71. *
  72. *
  73. * NON-TERMINALS
  74. *
  75. * [n]: 1, 2 ..., one, two, three ..
  76. * [month]: January, February, March, April, May, ... December
  77. * [weekday]: Monday, ... Sunday
  78. * [nth-weekday]: first [weekday], 2nd [weekday], ... last [weekday], ...
  79. * [monthday]: first, 1., 2., 1st, 2nd, second, ... 31st, last day, 2nd last day, ..
  80. * [date]:
  81. * - [month] (0-31(,) ([year])),
  82. * - (the) 0-31.(1-12.([year])),
  83. * - (the) 0-31/(1-12/([year])),
  84. * - [weekday]
  85. *
  86. * [year]: 0000, 0001, ... 01, 02, ..
  87. *
  88. * Definitely not supported for parsing:
  89. *
  90. * [yearday]: first, 1., 2., 1st, 2nd, second, ... 366th, last day, 2nd last day, ..
  91. *
  92. * @param {String} text
  93. * @return {Object, Boolean} the rule, or null.
  94. */
  95. const fromText = function (text: string, language: Language = ENGLISH) {
  96. return new RRule(parseText(text, language) || undefined)
  97. }
  98. const common = [
  99. 'count',
  100. 'until',
  101. 'interval',
  102. 'byweekday',
  103. 'bymonthday',
  104. 'bymonth',
  105. ]
  106. ToText.IMPLEMENTED = []
  107. ToText.IMPLEMENTED[Frequency.HOURLY] = common
  108. ToText.IMPLEMENTED[Frequency.MINUTELY] = common
  109. ToText.IMPLEMENTED[Frequency.DAILY] = ['byhour'].concat(common)
  110. ToText.IMPLEMENTED[Frequency.WEEKLY] = common
  111. ToText.IMPLEMENTED[Frequency.MONTHLY] = common
  112. ToText.IMPLEMENTED[Frequency.YEARLY] = ['byweekno', 'byyearday'].concat(common)
  113. // =============================================================================
  114. // Export
  115. // =============================================================================
  116. const toText = function (
  117. rrule: RRule,
  118. gettext?: GetText,
  119. language?: Language,
  120. dateFormatter?: DateFormatter
  121. ) {
  122. return new ToText(rrule, gettext, language, dateFormatter).toString()
  123. }
  124. const { isFullyConvertible } = ToText
  125. export interface Nlp {
  126. fromText: typeof fromText
  127. parseText: typeof parseText
  128. isFullyConvertible: typeof isFullyConvertible
  129. toText: typeof toText
  130. }
  131. export { fromText, parseText, isFullyConvertible, toText }