@hebcal/rest-api 5.1.0 → 5.1.1

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/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/rest-api v5.1.0 */
1
+ /*! @hebcal/rest-api v5.1.1 */
2
2
  import { flags, HDate, HebrewCalendar, Zmanim, Locale as Locale$1, gematriya } from '@hebcal/core';
3
3
  import { getLeyningForParshaHaShavua, getLeyningForHoliday, formatAliyahWithBook } from '@hebcal/leyning';
4
4
  import { getTriennialForParshaHaShavua } from '@hebcal/triennial';
@@ -41,107 +41,106 @@ function yearFromFixed(abs) {
41
41
  const ABS_14SEP1752 = 639797;
42
42
  const ABS_2SEP1752 = 639785;
43
43
  */
44
+ /*
45
+ * Formerly in namespace, now top-level
46
+ */
47
+ /**
48
+ * Returns true if the Gregorian year is a leap year
49
+ * @param year Gregorian year
50
+ */
51
+ function isGregLeapYear(year) {
52
+ return !(year % 4) && (!!(year % 100) || !(year % 400));
53
+ }
54
+ /**
55
+ * Number of days in the Gregorian month for given year
56
+ * @param month Gregorian month (1=January, 12=December)
57
+ * @param year Gregorian year
58
+ */
59
+ function daysInGregMonth(month, year) {
60
+ // 1 based months
61
+ return monthLengths[+isGregLeapYear(year)][month];
62
+ }
63
+ /**
64
+ * Returns true if the object is a Javascript Date
65
+ */
66
+ function isDate(obj) {
67
+ // eslint-disable-next-line no-prototype-builtins
68
+ return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
69
+ }
70
+ /**
71
+ * @private
72
+ * @param year
73
+ * @param month (1-12)
74
+ * @param day (1-31)
75
+ */
76
+ function toFixed(year, month, day) {
77
+ const py = year - 1;
78
+ return (365 * py +
79
+ quotient(py, 4) -
80
+ quotient(py, 100) +
81
+ quotient(py, 400) +
82
+ quotient(367 * month - 362, 12) +
83
+ (month <= 2 ? 0 : isGregLeapYear(year) ? -1 : -2) +
84
+ day);
85
+ }
44
86
  /**
45
- * Gregorian date helper functions.
87
+ * Converts Gregorian date to absolute R.D. (Rata Die) days
88
+ * @param date Gregorian date
89
+ */
90
+ function greg2abs(date) {
91
+ if (!isDate(date)) {
92
+ throw new TypeError(`Argument not a Date: ${date}`);
93
+ }
94
+ const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
95
+ /*
96
+ if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
97
+ throw new RangeError(`Invalid Date: ${date}`);
98
+ }
99
+ */
100
+ return abs;
101
+ }
102
+ /**
103
+ * Converts from Rata Die (R.D. number) to Gregorian date.
104
+ * See the footnote on page 384 of ``Calendrical Calculations, Part II:
105
+ * Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
106
+ * Clamen, Software--Practice and Experience, Volume 23, Number 4
107
+ * (April, 1993), pages 383-404 for an explanation.
108
+ * @param abs - R.D. number of days
109
+ */
110
+ function abs2greg(abs) {
111
+ if (typeof abs !== 'number') {
112
+ throw new TypeError(`Argument not a Number: ${abs}`);
113
+ }
114
+ abs = Math.trunc(abs);
115
+ /*
116
+ if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
117
+ throw new RangeError(`Invalid Date: ${abs}`);
118
+ }
119
+ */
120
+ const year = yearFromFixed(abs);
121
+ const priorDays = abs - toFixed(year, 1, 1);
122
+ const correction = abs < toFixed(year, 3, 1) ? 0 : isGregLeapYear(year) ? 1 : 2;
123
+ const month = quotient(12 * (priorDays + correction) + 373, 367);
124
+ const day = abs - toFixed(year, month, 1) + 1;
125
+ const dt = new Date(year, month - 1, day);
126
+ if (year < 100 && year >= 0) {
127
+ dt.setFullYear(year);
128
+ }
129
+ return dt;
130
+ }
131
+
132
+ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-namespace */
133
+ /**
134
+ * Gregorian date helper functions
46
135
  */
47
136
  var greg;
48
137
  (function (greg) {
49
- /**
50
- * Returns true if the Gregorian year is a leap year
51
- * @param {number} year Gregorian year
52
- * @return {boolean}
53
- */
54
- function isLeapYear(year) {
55
- return !(year % 4) && (!!(year % 100) || !(year % 400));
56
- }
57
- greg.isLeapYear = isLeapYear;
58
- /**
59
- * Number of days in the Gregorian month for given year
60
- * @param {number} month Gregorian month (1=January, 12=December)
61
- * @param {number} year Gregorian year
62
- * @return {number}
63
- */
64
- function daysInMonth(month, year) {
65
- // 1 based months
66
- return monthLengths[+isLeapYear(year)][month];
67
- }
68
- greg.daysInMonth = daysInMonth;
69
- /**
70
- * Returns true if the object is a Javascript Date
71
- * @param {Object} obj
72
- * @return {boolean}
73
- */
74
- function isDate(obj) {
75
- // eslint-disable-next-line no-prototype-builtins
76
- return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
77
- }
78
- greg.isDate = isDate;
79
- /**
80
- * @private
81
- * @param year
82
- * @param month (1-12)
83
- * @param day (1-31)
84
- */
85
- function toFixed(year, month, day) {
86
- const py = year - 1;
87
- return (365 * py +
88
- quotient(py, 4) -
89
- quotient(py, 100) +
90
- quotient(py, 400) +
91
- quotient(367 * month - 362, 12) +
92
- (month <= 2 ? 0 : isLeapYear(year) ? -1 : -2) +
93
- day);
94
- }
95
- /**
96
- * Converts Gregorian date to absolute R.D. (Rata Die) days
97
- * @param {Date} date Gregorian date
98
- * @return {number}
99
- */
100
- function greg2abs(date) {
101
- if (!isDate(date)) {
102
- throw new TypeError(`Argument not a Date: ${date}`);
103
- }
104
- const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
105
- /*
106
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
107
- throw new RangeError(`Invalid Date: ${date}`);
108
- }
109
- */
110
- return abs;
111
- }
112
- greg.greg2abs = greg2abs;
113
- /**
114
- * Converts from Rata Die (R.D. number) to Gregorian date.
115
- * See the footnote on page 384 of ``Calendrical Calculations, Part II:
116
- * Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
117
- * Clamen, Software--Practice and Experience, Volume 23, Number 4
118
- * (April, 1993), pages 383-404 for an explanation.
119
- * @param {number} abs - R.D. number of days
120
- * @return {Date}
121
- */
122
- function abs2greg(abs) {
123
- if (typeof abs !== 'number') {
124
- throw new TypeError(`Argument not a Number: ${abs}`);
125
- }
126
- abs = Math.trunc(abs);
127
- /*
128
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
129
- throw new RangeError(`Invalid Date: ${abs}`);
130
- }
131
- */
132
- const year = yearFromFixed(abs);
133
- const priorDays = abs - toFixed(year, 1, 1);
134
- const correction = abs < toFixed(year, 3, 1) ? 0 : isLeapYear(year) ? 1 : 2;
135
- const month = quotient(12 * (priorDays + correction) + 373, 367);
136
- const day = abs - toFixed(year, month, 1) + 1;
137
- const dt = new Date(year, month - 1, day);
138
- if (year < 100 && year >= 0) {
139
- dt.setFullYear(year);
140
- }
141
- return dt;
142
- }
143
- greg.abs2greg = abs2greg;
144
138
  })(greg || (greg = {}));
139
+ greg.abs2greg = abs2greg;
140
+ greg.daysInMonth = daysInGregMonth;
141
+ greg.greg2abs = greg2abs;
142
+ greg.isDate = isDate;
143
+ greg.isLeapYear = isGregLeapYear;
145
144
 
146
145
  const alefbet = {
147
146
  א: 1,
@@ -179,8 +178,6 @@ for (const [key, val] of Object.entries(alefbet)) {
179
178
  * Similar to `string.padStart(4, '0')` but will also format
180
179
  * negative numbers similar to how the JavaScript date formats
181
180
  * negative year numbers (e.g. `-37` is formatted as `-000037`).
182
- * @param {number} number
183
- * @return {string}
184
181
  */
185
182
  function pad4(number) {
186
183
  if (number < 0) {
@@ -200,8 +197,6 @@ function pad4(number) {
200
197
  /**
201
198
  * Formats a number with leading zeros so the resulting string is 2 digits long.
202
199
  * Similar to `string.padStart(2, '0')`.
203
- * @param {number} number
204
- * @return {string}
205
200
  */
206
201
  function pad2(number) {
207
202
  if (number < 10) {
@@ -211,9 +206,6 @@ function pad2(number) {
211
206
  }
212
207
  /**
213
208
  * Returns YYYY-MM-DD in the local timezone
214
- * @private
215
- * @param {Date} dt
216
- * @return {string}
217
209
  */
218
210
  function isoDateString(dt) {
219
211
  return (pad4(dt.getFullYear()) +
@@ -232,9 +224,9 @@ const noopLocale = {
232
224
  contexts: { '': {} },
233
225
  };
234
226
  const alias = {
235
- 'h': 'he',
236
- 'a': 'ashkenazi',
237
- 's': 'en',
227
+ h: 'he',
228
+ a: 'ashkenazi',
229
+ s: 'en',
238
230
  '': 'en',
239
231
  };
240
232
  /** @private */
@@ -255,12 +247,12 @@ class Locale {
255
247
  /**
256
248
  * Returns translation only if `locale` offers a non-empty translation for `id`.
257
249
  * Otherwise, returns `undefined`.
258
- * @param {string} id Message ID to translate
259
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
260
- * @return {string}
250
+ * @param id Message ID to translate
251
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
261
252
  */
262
253
  static lookupTranslation(id, locale) {
263
- const loc = (typeof locale === 'string' && locales.get(locale.toLowerCase())) || activeLocale;
254
+ const loc = (typeof locale === 'string' && locales.get(locale.toLowerCase())) ||
255
+ activeLocale;
264
256
  const array = loc[id];
265
257
  if ((array === null || array === void 0 ? void 0 : array.length) && array[0].length) {
266
258
  return array[0];
@@ -269,9 +261,8 @@ class Locale {
269
261
  }
270
262
  /**
271
263
  * By default, if no translation was found, returns `id`.
272
- * @param {string} id Message ID to translate
273
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
274
- * @return {string}
264
+ * @param id Message ID to translate
265
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
275
266
  */
276
267
  static gettext(id, locale) {
277
268
  const text = this.lookupTranslation(id, locale);
@@ -282,23 +273,24 @@ class Locale {
282
273
  }
283
274
  /**
284
275
  * Register locale translations.
285
- * @param {string} locale Locale name (i.e.: `'he'`, `'fr'`)
286
- * @param {LocaleData} data parsed data from a `.po` file.
276
+ * @param locale Locale name (i.e.: `'he'`, `'fr'`)
277
+ * @param data parsed data from a `.po` file.
287
278
  */
288
279
  static addLocale(locale, data) {
289
280
  if (typeof locale !== 'string') {
290
281
  throw new TypeError(`Invalid locale name: ${locale}`);
291
282
  }
292
- if (typeof data.contexts !== 'object' || typeof data.contexts[''] !== 'object') {
283
+ if (typeof data.contexts !== 'object' ||
284
+ typeof data.contexts[''] !== 'object') {
293
285
  throw new TypeError(`Locale '${locale}' invalid compact format`);
294
286
  }
295
287
  locales.set(locale.toLowerCase(), data.contexts['']);
296
288
  }
297
289
  /**
298
290
  * Adds a translation to `locale`, replacing any previous translation.
299
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`).
300
- * @param {string} id Message ID to translate
301
- * @param {string | string[]} translation Translation text
291
+ * @param locale Locale name (i.e: `'he'`, `'fr'`).
292
+ * @param id Message ID to translate
293
+ * @param translation Translation text
302
294
  */
303
295
  static addTranslation(locale, id, translation) {
304
296
  if (typeof locale !== 'string') {
@@ -325,8 +317,8 @@ class Locale {
325
317
  }
326
318
  /**
327
319
  * Adds multiple translations to `locale`, replacing any previous translations.
328
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`).
329
- * @param {LocaleData} data parsed data from a `.po` file.
320
+ * @param locale Locale name (i.e: `'he'`, `'fr'`).
321
+ * @param data parsed data from a `.po` file.
330
322
  */
331
323
  static addTranslations(locale, data) {
332
324
  if (typeof locale !== 'string') {
@@ -336,7 +328,8 @@ class Locale {
336
328
  if (!loc) {
337
329
  throw new TypeError(`Unknown locale: ${locale}`);
338
330
  }
339
- if (typeof data.contexts !== 'object' || typeof data.contexts[''] !== 'object') {
331
+ if (typeof data.contexts !== 'object' ||
332
+ typeof data.contexts[''] !== 'object') {
340
333
  throw new TypeError(`Locale '${locale}' invalid compact format`);
341
334
  }
342
335
  const ctx = data.contexts[''];
@@ -346,7 +339,7 @@ class Locale {
346
339
  * Activates a locale. Throws an error if the locale has not been previously added.
347
340
  * After setting the locale to be used, all strings marked for translations
348
341
  * will be represented by the corresponding translation in the specified locale.
349
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`)
342
+ * @param locale Locale name (i.e: `'he'`, `'fr'`)
350
343
  */
351
344
  static useLocale(locale) {
352
345
  const locale0 = locale.toLowerCase();
@@ -360,23 +353,20 @@ class Locale {
360
353
  }
361
354
  /**
362
355
  * Returns the name of the active locale (i.e. 'he', 'ashkenazi', 'fr')
363
- * @return {string}
364
356
  */
365
357
  static getLocaleName() {
366
358
  return activeName;
367
359
  }
368
360
  /**
369
361
  * Returns the names of registered locales
370
- * @return {string[]}
371
362
  */
372
363
  static getLocaleNames() {
373
364
  const keys = Array.from(locales.keys());
374
365
  return keys.sort((a, b) => a.localeCompare(b));
375
366
  }
376
367
  /**
377
- * @param {number} n
378
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
379
- * @return {string}
368
+ * Renders a number in ordinal, such as 1st, 2nd or 3rd
369
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
380
370
  */
381
371
  static ordinal(n, locale) {
382
372
  const locale1 = locale === null || locale === void 0 ? void 0 : locale.toLowerCase();
@@ -403,11 +393,6 @@ class Locale {
403
393
  return n + '.';
404
394
  }
405
395
  }
406
- /**
407
- * @private
408
- * @param {number} n
409
- * @return {string}
410
- */
411
396
  static getEnOrdinal(n) {
412
397
  const s = ['th', 'st', 'nd', 'rd'];
413
398
  const v = n % 100;
@@ -415,8 +400,6 @@ class Locale {
415
400
  }
416
401
  /**
417
402
  * Removes nekudot from Hebrew string
418
- * @param {string} str
419
- * @return {string}
420
403
  */
421
404
  static hebrewStripNikkud(str) {
422
405
  return str.replace(/[\u0590-\u05bd]/g, '').replace(/[\u05bf-\u05c7]/g, '');
@@ -1142,9 +1125,9 @@ function eventToClassicApiObject(ev, options, leyning = true) {
1142
1125
  if (reading) {
1143
1126
  result.leyning = formatLeyningResult(reading);
1144
1127
  const hyear = hd.getFullYear();
1145
- if (isParsha && !il && hyear >= 5745) {
1146
- const triReading = getTriennialForParshaHaShavua(ev);
1147
- if (triReading) {
1128
+ if (isParsha && hyear >= 5745) {
1129
+ const triReading = getTriennialForParshaHaShavua(ev, il);
1130
+ if (triReading === null || triReading === void 0 ? void 0 : triReading.aliyot) {
1148
1131
  result.leyning.triennial = formatAliyot({}, triReading.aliyot);
1149
1132
  }
1150
1133
  }
@@ -1217,6 +1200,18 @@ function formatAliyot(result, aliyot) {
1217
1200
  }
1218
1201
  return result;
1219
1202
  }
1203
+ function formatReasons(result, reason) {
1204
+ for (const num of ['7', '8', 'M']) {
1205
+ if (reason[num]) {
1206
+ const k = num == 'M' ? 'maftir' : num;
1207
+ result[k] += ' | ' + reason[num];
1208
+ }
1209
+ }
1210
+ if (reason.haftara) {
1211
+ result.haftarah += ' | ' + reason.haftara;
1212
+ }
1213
+ return result;
1214
+ }
1220
1215
  function formatLeyningResult(reading) {
1221
1216
  const result = {};
1222
1217
  if (reading.summary) {
@@ -1232,15 +1227,7 @@ function formatLeyningResult(reading) {
1232
1227
  formatAliyot(result, reading.fullkriyah);
1233
1228
  }
1234
1229
  if (reading.reason) {
1235
- for (const num of ['7', '8', 'M']) {
1236
- if (reading.reason[num]) {
1237
- const k = num == 'M' ? 'maftir' : num;
1238
- result[k] += ' | ' + reading.reason[num];
1239
- }
1240
- }
1241
- if (reading.reason.haftara) {
1242
- result.haftarah += ' | ' + reading.reason.haftara;
1243
- }
1230
+ formatReasons(result, reading.reason);
1244
1231
  }
1245
1232
  return result;
1246
1233
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hebcal/rest-api",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "author": "Michael J. Radwin (https://github.com/mjradwin)",
5
5
  "keywords": [
6
6
  "hebcal"
@@ -29,9 +29,9 @@
29
29
  "url": "https://github.com/hebcal/hebcal-rest-api/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@hebcal/core": "^5.4.5",
33
- "@hebcal/leyning": "^8.1.9",
34
- "@hebcal/triennial": "^5.0.5"
32
+ "@hebcal/core": "^5.4.9",
33
+ "@hebcal/leyning": "^8.2.2",
34
+ "@hebcal/triennial": "^5.1.1"
35
35
  },
36
36
  "scripts": {
37
37
  "build:rollup": "rollup -c",
@@ -39,14 +39,14 @@
39
39
  "build": "npm run build:json2js && npm run build:rollup",
40
40
  "prepublish": "npm run build",
41
41
  "pretest": "npm run build",
42
- "readme": "npx jsdoc2md dist/index.js",
42
+ "docs": "typedoc",
43
43
  "test": "jest"
44
44
  },
45
45
  "license": "BSD-2-Clause",
46
46
  "devDependencies": {
47
47
  "@babel/preset-env": "^7.24.7",
48
48
  "@babel/preset-typescript": "^7.24.7",
49
- "@hebcal/learning": "^5.0.8",
49
+ "@hebcal/learning": "^5.1.1",
50
50
  "@rollup/plugin-commonjs": "^26.0.1",
51
51
  "@rollup/plugin-json": "^6.1.0",
52
52
  "@rollup/plugin-node-resolve": "^15.2.3",
@@ -55,10 +55,9 @@
55
55
  "@types/jest": "^29.5.12",
56
56
  "gts": "^5.3.1",
57
57
  "jest": "^29.7.0",
58
- "jsdoc": "^4.0.3",
59
- "jsdoc-to-markdown": "^8.0.1",
60
- "rollup": "^4.18.0",
61
- "ts-jest": "^29.1.5",
58
+ "rollup": "^4.18.1",
59
+ "ts-jest": "^29.2.0",
60
+ "typedoc": "^0.26.3",
62
61
  "typescript": "^5.5.3"
63
62
  }
64
63
  }