@@ -72,68 +72,16 @@ export const shas0 = [
72
72
} ) ;
73
73
74
74
/**
75
- * Returns the Daf Yomi for given date
75
+ * Represents a tractate and page number
76
76
*/
77
- export class DafYomi {
77
+ export class DafPage {
78
78
/**
79
79
* Initializes a daf yomi instance
80
- * @param {Date|HDate|number } date Gregorian or Hebrew date
80
+ * @param {string } name
81
+ * @param {number } blatt
81
82
*/
82
- constructor ( date ) {
83
- const cday = getAbsDate ( date ) ;
84
- checkTooEarly ( cday , osday , 'Daf Yomi' ) ;
85
- let cno ;
86
- let dno ;
87
- if ( cday >= nsday ) { // "new" cycle
88
- cno = 8 + Math . floor ( ( cday - nsday ) / 2711 ) ;
89
- dno = ( cday - nsday ) % 2711 ;
90
- } else { // old cycle
91
- cno = 1 + Math . floor ( ( cday - osday ) / 2702 ) ;
92
- dno = ( cday - osday ) % 2702 ;
93
- }
94
-
95
- // Find the daf taking note that the cycle changed slightly after cycle 7.
96
-
97
- let total = 0 ;
98
- let blatt = 0 ;
99
- let count = - 1 ;
100
-
101
- // Fix Shekalim for old cycles
102
- const shortShekalim = cno <= 7 ;
103
- const shas = shortShekalim ? shas0 . slice ( ) : shas0 ;
104
- if ( shortShekalim ) {
105
- shas [ 4 ] = { name : 'Shekalim' , blatt : 13 } ;
106
- }
107
-
108
- // Find the daf
109
- let j = 0 ;
110
- const dafcnt = 40 ;
111
- while ( j < dafcnt ) {
112
- count ++ ;
113
- total = total + shas [ j ] . blatt - 1 ;
114
- if ( dno < total ) {
115
- blatt = ( shas [ j ] . blatt + 1 ) - ( total - dno ) ;
116
- // fiddle with the weird ones near the end
117
- switch ( count ) {
118
- case 36 :
119
- blatt = blatt + 21 ;
120
- break ;
121
- case 37 :
122
- blatt = blatt + 24 ;
123
- break ;
124
- case 38 :
125
- blatt = blatt + 32 ;
126
- break ;
127
- default :
128
- break ;
129
- }
130
- // Bailout
131
- j = 1 + dafcnt ;
132
- }
133
- j ++ ;
134
- }
135
-
136
- this . name = shas [ count ] . name ;
83
+ constructor ( name , blatt ) {
84
+ this . name = name ;
137
85
this . blatt = blatt ;
138
86
}
139
87
/**
@@ -166,6 +114,81 @@ export class DafYomi {
166
114
}
167
115
}
168
116
117
+ /**
118
+ * @private
119
+ * @param {Date|HDate|number } date Gregorian or Hebrew date
120
+ * @return {DafPage }
121
+ */
122
+ function calculateDaf ( date ) {
123
+ const cday = getAbsDate ( date ) ;
124
+ checkTooEarly ( cday , osday , 'Daf Yomi' ) ;
125
+ let cno ;
126
+ let dno ;
127
+ if ( cday >= nsday ) { // "new" cycle
128
+ cno = 8 + Math . floor ( ( cday - nsday ) / 2711 ) ;
129
+ dno = ( cday - nsday ) % 2711 ;
130
+ } else { // old cycle
131
+ cno = 1 + Math . floor ( ( cday - osday ) / 2702 ) ;
132
+ dno = ( cday - osday ) % 2702 ;
133
+ }
134
+
135
+ // Find the daf taking note that the cycle changed slightly after cycle 7.
136
+
137
+ let total = 0 ;
138
+ let blatt = 0 ;
139
+ let count = - 1 ;
140
+
141
+ // Fix Shekalim for old cycles
142
+ const shortShekalim = cno <= 7 ;
143
+ const shas = shortShekalim ? shas0 . slice ( ) : shas0 ;
144
+ if ( shortShekalim ) {
145
+ shas [ 4 ] = { name : 'Shekalim' , blatt : 13 } ;
146
+ }
147
+
148
+ // Find the daf
149
+ let j = 0 ;
150
+ const dafcnt = 40 ;
151
+ while ( j < dafcnt ) {
152
+ count ++ ;
153
+ total = total + shas [ j ] . blatt - 1 ;
154
+ if ( dno < total ) {
155
+ blatt = ( shas [ j ] . blatt + 1 ) - ( total - dno ) ;
156
+ // fiddle with the weird ones near the end
157
+ switch ( count ) {
158
+ case 36 :
159
+ blatt = blatt + 21 ;
160
+ break ;
161
+ case 37 :
162
+ blatt = blatt + 24 ;
163
+ break ;
164
+ case 38 :
165
+ blatt = blatt + 32 ;
166
+ break ;
167
+ default :
168
+ break ;
169
+ }
170
+ // Bailout
171
+ j = 1 + dafcnt ;
172
+ }
173
+ j ++ ;
174
+ }
175
+ return new DafPage ( shas [ count ] . name , blatt ) ;
176
+ }
177
+
178
+ /**
179
+ * Returns the Daf Yomi for given date
180
+ */
181
+ export class DafYomi extends DafPage {
182
+ /**
183
+ * Initializes a daf yomi instance
184
+ * @param {Date|HDate|number } date Gregorian or Hebrew date
185
+ */
186
+ constructor ( date ) {
187
+ const d = calculateDaf ( date ) ;
188
+ super ( d . name , d . blatt ) ;
189
+ }
190
+ }
191
+
169
192
const dafYomiSefaria = {
170
193
'Berachot' : 'Berakhot' ,
171
194
'Rosh Hashana' : 'Rosh Hashanah' ,
@@ -180,14 +203,14 @@ const dafYomiSefaria = {
180
203
} ;
181
204
182
205
/**
183
- * Event wrapper around a DafYomi instance
206
+ * Event wrapper around a DafPage instance
184
207
*/
185
- export class DafYomiEvent extends Event {
208
+ export class DafPageEvent extends Event {
186
209
/**
187
210
* @param {HDate } date
211
+ * @param {DafPage } daf
188
212
*/
189
- constructor ( date ) {
190
- const daf = new DafYomi ( date . greg ( ) ) ;
213
+ constructor ( date , daf ) {
191
214
super ( date , daf . render ( 'en' ) , flags . DAF_YOMI ) ;
192
215
this . daf = daf ;
193
216
}
@@ -197,7 +220,7 @@ export class DafYomiEvent extends Event {
197
220
* @return {string }
198
221
*/
199
222
render ( locale ) {
200
- return Locale . gettext ( 'Daf Yomi' , locale ) + ': ' + this . daf . render ( locale ) ;
223
+ return this . daf . render ( locale ) ;
201
224
}
202
225
/**
203
226
* Returns Daf Yomi name without the 'Daf Yomi: ' prefix (e.g. "Pesachim 107").
@@ -223,6 +246,27 @@ export class DafYomiEvent extends Event {
223
246
return `https://www.sefaria.org/${ name } .${ blatt } a?lang=bi` ;
224
247
}
225
248
}
249
+ }
250
+
251
+ /**
252
+ * Event wrapper around a DafYomi instance
253
+ */
254
+ export class DafYomiEvent extends DafPageEvent {
255
+ /**
256
+ * @param {HDate } date
257
+ */
258
+ constructor ( date ) {
259
+ const daf = new DafYomi ( date . greg ( ) ) ;
260
+ super ( date , daf ) ;
261
+ }
262
+ /**
263
+ * Returns Daf Yomi name including the 'Daf Yomi: ' prefix (e.g. "Daf Yomi: Pesachim 107").
264
+ * @param {string } [locale] Optional locale name (defaults to active locale).
265
+ * @return {string }
266
+ */
267
+ render ( locale ) {
268
+ return Locale . gettext ( 'Daf Yomi' , locale ) + ': ' + this . daf . render ( locale ) ;
269
+ }
226
270
/** @return {string[] } */
227
271
getCategories ( ) {
228
272
return [ 'dafyomi' ] ;
0 commit comments