@hebcal/core 4.5.1 → 5.0.0-rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/hdate0.mjs DELETED
@@ -1,465 +0,0 @@
1
- /*! @hebcal/core v4.5.1 */
2
- /*
3
- * More minimal greg routines
4
- */
5
-
6
- /** @private */
7
- const lengths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
8
- /** @private */
9
- const monthLengths = [
10
- lengths,
11
- lengths.slice(),
12
- ];
13
- monthLengths[1][2] = 29;
14
-
15
- /**
16
- * @private
17
- * @param {number} x
18
- * @param {number} y
19
- * @return {number}
20
- */
21
- function mod(x, y) {
22
- return x - y * Math.floor(x / y);
23
- }
24
-
25
- /**
26
- * @private
27
- * @param {number} x
28
- * @param {number} y
29
- * @return {number}
30
- */
31
- function quotient(x, y) {
32
- return Math.floor(x / y);
33
- }
34
-
35
- /**
36
- * Returns true if the Gregorian year is a leap year
37
- * @private
38
- * @param {number} year Gregorian year
39
- * @return {boolean}
40
- */
41
- function isLeapYear$1(year) {
42
- return !(year % 4) && (!!(year % 100) || !(year % 400));
43
- }
44
-
45
- /**
46
- * Returns true if the object is a Javascript Date
47
- * @private
48
- * @param {Object} obj
49
- * @return {boolean}
50
- */
51
- function isDate(obj) {
52
- return typeof obj === 'object' && Date.prototype === obj.__proto__;
53
- }
54
-
55
- /*
56
- const ABS_14SEP1752 = 639797;
57
- const ABS_2SEP1752 = 639785;
58
- */
59
-
60
- /**
61
- * Converts Gregorian date to absolute R.D. (Rata Die) days
62
- * @private
63
- * @param {Date} date Gregorian date
64
- * @return {number}
65
- */
66
- function greg2abs(date) {
67
- if (!isDate(date)) {
68
- throw new TypeError(`Argument not a Date: ${date}`);
69
- }
70
- const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
71
- /*
72
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
73
- throw new RangeError(`Invalid Date: ${date}`);
74
- }
75
- */
76
- return abs;
77
- }
78
-
79
- /**
80
- * @private
81
- * @param {number} abs - R.D. number of days
82
- * @return {number}
83
- */
84
- function yearFromFixed(abs) {
85
- const l0 = abs - 1;
86
- const n400 = quotient(l0, 146097);
87
- const d1 = mod(l0, 146097);
88
- const n100 = quotient(d1, 36524);
89
- const d2 = mod(d1, 36524);
90
- const n4 = quotient(d2, 1461);
91
- const d3 = mod(d2, 1461);
92
- const n1 = quotient(d3, 365);
93
- const year = 400 * n400 + 100 * n100 + 4 * n4 + n1;
94
- return n100 != 4 && n1 != 4 ? year + 1 : year;
95
- }
96
-
97
- /**
98
- * @private
99
- * @param {number} year
100
- * @param {number} month (1-12)
101
- * @param {number} day (1-31)
102
- * @return {number}
103
- */
104
- function toFixed(year, month, day) {
105
- const py = year - 1;
106
- return 365 * py +
107
- quotient(py, 4) -
108
- quotient(py, 100) +
109
- quotient(py, 400) +
110
- quotient((367 * month - 362), 12) +
111
- (month <= 2 ? 0 : (isLeapYear$1(year) ? -1 : -2)) +
112
- day;
113
- }
114
-
115
- /**
116
- * Converts from Rata Die (R.D. number) to Gregorian date.
117
- * See the footnote on page 384 of ``Calendrical Calculations, Part II:
118
- * Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
119
- * Clamen, Software--Practice and Experience, Volume 23, Number 4
120
- * (April, 1993), pages 383-404 for an explanation.
121
- * @private
122
- * @param {number} abs - R.D. number of days
123
- * @return {Date}
124
- */
125
- function abs2greg(abs) {
126
- if (typeof abs !== 'number') {
127
- throw new TypeError(`Argument not a Number: ${abs}`);
128
- }
129
- abs = Math.trunc(abs);
130
- /*
131
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
132
- throw new RangeError(`Invalid Date: ${abs}`);
133
- }
134
- */
135
- const year = yearFromFixed(abs);
136
- const priorDays = abs - toFixed(year, 1, 1);
137
- const correction = abs < toFixed(year, 3, 1) ? 0 : (isLeapYear$1(year) ? 1 : 2);
138
- const month = quotient((12 * (priorDays + correction) + 373), 367);
139
- const day = abs - toFixed(year, month, 1) + 1;
140
- const dt = new Date(year, month - 1, day);
141
- if (year < 100 && year >= 0) {
142
- dt.setFullYear(year);
143
- }
144
- return dt;
145
- }
146
-
147
- /*
148
- * More minimal HDate
149
- */
150
-
151
- const NISAN = 1;
152
- const IYYAR = 2;
153
- // const SIVAN = 3;
154
- const TAMUZ = 4;
155
- // const AV = 5;
156
- const ELUL = 6;
157
- const TISHREI = 7;
158
- const CHESHVAN = 8;
159
- const KISLEV = 9;
160
- const TEVET = 10;
161
- // const SHVAT = 11;
162
- const ADAR_I = 12;
163
- const ADAR_II = 13;
164
-
165
- /**
166
- * Hebrew months of the year (NISAN=1, TISHREI=7)
167
- * @readonly
168
- * @enum {number}
169
- */
170
- const months = {
171
- /** Nissan / ניסן */
172
- NISAN: 1,
173
- /** Iyyar / אייר */
174
- IYYAR: 2,
175
- /** Sivan / סיון */
176
- SIVAN: 3,
177
- /** Tamuz (sometimes Tammuz) / תמוז */
178
- TAMUZ: 4,
179
- /** Av / אב */
180
- AV: 5,
181
- /** Elul / אלול */
182
- ELUL: 6,
183
- /** Tishrei / תִּשְׁרֵי */
184
- TISHREI: 7,
185
- /** Cheshvan / חשון */
186
- CHESHVAN: 8,
187
- /** Kislev / כסלו */
188
- KISLEV: 9,
189
- /** Tevet / טבת */
190
- TEVET: 10,
191
- /** Sh'vat / שבט */
192
- SHVAT: 11,
193
- /** Adar or Adar Rishon / אדר */
194
- ADAR_I: 12,
195
- /** Adar Sheini (only on leap years) / אדר ב׳ */
196
- ADAR_II: 13,
197
- };
198
-
199
- const monthNames0 = [
200
- '',
201
- 'Nisan',
202
- 'Iyyar',
203
- 'Sivan',
204
- 'Tamuz',
205
- 'Av',
206
- 'Elul',
207
- 'Tishrei',
208
- 'Cheshvan',
209
- 'Kislev',
210
- 'Tevet',
211
- 'Sh\'vat',
212
- ];
213
-
214
- /**
215
- * Transliterations of Hebrew month names.
216
- * Regular years are index 0 and leap years are index 1.
217
- * @private
218
- */
219
- const monthNames = [
220
- monthNames0.concat([
221
- 'Adar',
222
- 'Nisan',
223
- ]),
224
- monthNames0.concat([
225
- 'Adar I',
226
- 'Adar II',
227
- 'Nisan',
228
- ]),
229
- ];
230
-
231
- const edCache = Object.create(null);
232
-
233
- const EPOCH = -1373428;
234
- // Avg year length in the cycle (19 solar years with 235 lunar months)
235
- const AVG_HEBYEAR_DAYS = 365.24682220597794;
236
-
237
- /**
238
- * @private
239
- * @param {any} n
240
- * @param {string} name
241
- */
242
- function assertNumber(n, name) {
243
- if (typeof n !== 'number' || isNaN(n)) {
244
- throw new TypeError(`invalid parameter '${name}' not a number: ${n}`);
245
- }
246
- }
247
-
248
- /**
249
- * Converts Hebrew date to R.D. (Rata Die) fixed days.
250
- * R.D. 1 is the imaginary date Monday, January 1, 1 on the Gregorian
251
- * Calendar.
252
- * @private
253
- * @param {number} year Hebrew year
254
- * @param {number} month Hebrew month
255
- * @param {number} day Hebrew date (1-30)
256
- * @return {number}
257
- */
258
- function hebrew2abs(year, month, day) {
259
- assertNumber(year, 'year');
260
- assertNumber(month, 'month');
261
- assertNumber(day, 'day');
262
-
263
- if (year < 1) {
264
- throw new RangeError(`hebrew2abs: invalid year ${year}`);
265
- }
266
-
267
- let tempabs = day;
268
-
269
- if (month < TISHREI) {
270
- for (let m = TISHREI; m <= monthsInYear(year); m++) {
271
- tempabs += daysInMonth(m, year);
272
- }
273
- for (let m = NISAN; m < month; m++) {
274
- tempabs += daysInMonth(m, year);
275
- }
276
- } else {
277
- for (let m = TISHREI; m < month; m++) {
278
- tempabs += daysInMonth(m, year);
279
- }
280
- }
281
-
282
- return EPOCH + elapsedDays(year) + tempabs - 1;
283
- }
284
-
285
- /**
286
- * @private
287
- * @param {number} year
288
- * @return {number}
289
- */
290
- function newYear(year) {
291
- return EPOCH + elapsedDays(year);
292
- }
293
-
294
- /**
295
- * Converts absolute R.D. days to Hebrew date
296
- * @private
297
- * @param {number} abs absolute R.D. days
298
- * @return {SimpleHebrewDate}
299
- */
300
- function abs2hebrew(abs) {
301
- assertNumber(abs, 'abs');
302
- abs = Math.trunc(abs);
303
- if (abs <= EPOCH) {
304
- throw new RangeError(`abs2hebrew: ${abs} is before epoch`);
305
- }
306
- // first, quickly approximate year
307
- let year = Math.floor((abs - EPOCH) / AVG_HEBYEAR_DAYS);
308
- while (newYear(year) <= abs) {
309
- ++year;
310
- }
311
- --year;
312
-
313
- let month = abs < hebrew2abs(year, 1, 1) ? 7 : 1;
314
- while (abs > hebrew2abs(year, month, daysInMonth(month, year))) {
315
- ++month;
316
- }
317
-
318
- const day = 1 + abs - hebrew2abs(year, month, 1);
319
- return {yy: year, mm: month, dd: day};
320
- }
321
-
322
- /**
323
- * Returns true if Hebrew year is a leap year
324
- * @private
325
- * @param {number} year Hebrew year
326
- * @return {boolean}
327
- */
328
- function isLeapYear(year) {
329
- return (1 + year * 7) % 19 < 7;
330
- }
331
-
332
- /**
333
- * Number of months in this Hebrew year (either 12 or 13 depending on leap year)
334
- * @private
335
- * @param {number} year Hebrew year
336
- * @return {number}
337
- */
338
- function monthsInYear(year) {
339
- return 12 + isLeapYear(year); // boolean is cast to 1 or 0
340
- }
341
-
342
- /**
343
- * Number of days in Hebrew month in a given year (29 or 30)
344
- * @private
345
- * @param {number} month Hebrew month (e.g. months.TISHREI)
346
- * @param {number} year Hebrew year
347
- * @return {number}
348
- */
349
- function daysInMonth(month, year) {
350
- switch (month) {
351
- case IYYAR:
352
- case TAMUZ:
353
- case ELUL:
354
- case TEVET:
355
- case ADAR_II:
356
- return 29;
357
- }
358
- if ((month === ADAR_I && !isLeapYear(year)) ||
359
- (month === CHESHVAN && !longCheshvan(year)) ||
360
- (month === KISLEV && shortKislev(year))) {
361
- return 29;
362
- } else {
363
- return 30;
364
- }
365
- }
366
-
367
- /**
368
- * Returns a transliterated string name of Hebrew month in year,
369
- * for example 'Elul' or 'Cheshvan'.
370
- * @private
371
- * @param {number} month Hebrew month (e.g. months.TISHREI)
372
- * @param {number} year Hebrew year
373
- * @return {string}
374
- */
375
- function getMonthName(month, year) {
376
- assertNumber(month, 'month');
377
- assertNumber(year, 'year');
378
- if (month < 1 || month > 14) {
379
- throw new TypeError(`bad month argument ${month}`);
380
- }
381
- return monthNames[+isLeapYear(year)][month];
382
- }
383
-
384
- /**
385
- * Days from sunday prior to start of Hebrew calendar to mean
386
- * conjunction of Tishrei in Hebrew YEAR
387
- * @private
388
- * @param {number} year Hebrew year
389
- * @return {number}
390
- */
391
- function elapsedDays(year) {
392
- const elapsed = edCache[year] = edCache[year] || elapsedDays0(year);
393
- return elapsed;
394
- }
395
-
396
- /**
397
- * Days from sunday prior to start of Hebrew calendar to mean
398
- * conjunction of Tishrei in Hebrew YEAR
399
- * @private
400
- * @param {number} year Hebrew year
401
- * @return {number}
402
- */
403
- function elapsedDays0(year) {
404
- const prevYear = year - 1;
405
- const mElapsed = 235 * Math.floor(prevYear / 19) + // Months in complete 19 year lunar (Metonic) cycles so far
406
- 12 * (prevYear % 19) + // Regular months in this cycle
407
- Math.floor(((prevYear % 19) * 7 + 1) / 19); // Leap months this cycle
408
-
409
- const pElapsed = 204 + 793 * (mElapsed % 1080);
410
-
411
- const hElapsed = 5 +
412
- 12 * mElapsed +
413
- 793 * Math.floor(mElapsed / 1080) +
414
- Math.floor(pElapsed / 1080);
415
-
416
- const parts = (pElapsed % 1080) + 1080 * (hElapsed % 24);
417
-
418
- const day = 1 + 29 * mElapsed + Math.floor(hElapsed / 24);
419
- const altDay = day +
420
- (parts >= 19440 ||
421
- (2 === day % 7 && parts >= 9924 && !isLeapYear(year)) ||
422
- (1 === day % 7 && parts >= 16789 && isLeapYear(prevYear)));
423
-
424
- return altDay + (altDay % 7 === 0 || altDay % 7 === 3 || altDay % 7 === 5);
425
- }
426
-
427
- /**
428
- * Number of days in the hebrew YEAR.
429
- * A common Hebrew calendar year can have a length of 353, 354 or 355 days
430
- * A leap Hebrew calendar year can have a length of 383, 384 or 385 days
431
- * @private
432
- * @param {number} year Hebrew year
433
- * @return {number}
434
- */
435
- function daysInYear(year) {
436
- return elapsedDays(year + 1) - elapsedDays(year);
437
- }
438
-
439
- /**
440
- * true if Cheshvan is long in Hebrew year
441
- * @private
442
- * @param {number} year Hebrew year
443
- * @return {boolean}
444
- */
445
- function longCheshvan(year) {
446
- return daysInYear(year) % 10 === 5;
447
- }
448
-
449
- /**
450
- * true if Kislev is short in Hebrew year
451
- * @private
452
- * @param {number} year Hebrew year
453
- * @return {boolean}
454
- */
455
- function shortKislev(year) {
456
- return daysInYear(year) % 10 === 3;
457
- }
458
-
459
- const hdate = {
460
- abs2hebrew, daysInMonth, daysInYear, getMonthName, hebrew2abs,
461
- isLeapYear, longCheshvan, months,
462
- monthsInYear, shortKislev,
463
- };
464
-
465
- export { abs2greg, greg2abs, hdate };