@hebcal/rest-api 5.1.0 → 5.1.2

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.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @hebcal/rest-api v5.1.0 */
1
+ /*! @hebcal/rest-api v5.1.2 */
2
2
  'use strict';
3
3
 
4
4
  var core = require('@hebcal/core');
@@ -43,107 +43,106 @@ function yearFromFixed(abs) {
43
43
  const ABS_14SEP1752 = 639797;
44
44
  const ABS_2SEP1752 = 639785;
45
45
  */
46
+ /*
47
+ * Formerly in namespace, now top-level
48
+ */
49
+ /**
50
+ * Returns true if the Gregorian year is a leap year
51
+ * @param year Gregorian year
52
+ */
53
+ function isGregLeapYear(year) {
54
+ return !(year % 4) && (!!(year % 100) || !(year % 400));
55
+ }
46
56
  /**
47
- * Gregorian date helper functions.
57
+ * Number of days in the Gregorian month for given year
58
+ * @param month Gregorian month (1=January, 12=December)
59
+ * @param year Gregorian year
60
+ */
61
+ function daysInGregMonth(month, year) {
62
+ // 1 based months
63
+ return monthLengths[+isGregLeapYear(year)][month];
64
+ }
65
+ /**
66
+ * Returns true if the object is a Javascript Date
67
+ */
68
+ function isDate(obj) {
69
+ // eslint-disable-next-line no-prototype-builtins
70
+ return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
71
+ }
72
+ /**
73
+ * @private
74
+ * @param year
75
+ * @param month (1-12)
76
+ * @param day (1-31)
77
+ */
78
+ function toFixed(year, month, day) {
79
+ const py = year - 1;
80
+ return (365 * py +
81
+ quotient(py, 4) -
82
+ quotient(py, 100) +
83
+ quotient(py, 400) +
84
+ quotient(367 * month - 362, 12) +
85
+ (month <= 2 ? 0 : isGregLeapYear(year) ? -1 : -2) +
86
+ day);
87
+ }
88
+ /**
89
+ * Converts Gregorian date to absolute R.D. (Rata Die) days
90
+ * @param date Gregorian date
91
+ */
92
+ function greg2abs(date) {
93
+ if (!isDate(date)) {
94
+ throw new TypeError(`Argument not a Date: ${date}`);
95
+ }
96
+ const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
97
+ /*
98
+ if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
99
+ throw new RangeError(`Invalid Date: ${date}`);
100
+ }
101
+ */
102
+ return abs;
103
+ }
104
+ /**
105
+ * Converts from Rata Die (R.D. number) to Gregorian date.
106
+ * See the footnote on page 384 of ``Calendrical Calculations, Part II:
107
+ * Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
108
+ * Clamen, Software--Practice and Experience, Volume 23, Number 4
109
+ * (April, 1993), pages 383-404 for an explanation.
110
+ * @param abs - R.D. number of days
111
+ */
112
+ function abs2greg(abs) {
113
+ if (typeof abs !== 'number') {
114
+ throw new TypeError(`Argument not a Number: ${abs}`);
115
+ }
116
+ abs = Math.trunc(abs);
117
+ /*
118
+ if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
119
+ throw new RangeError(`Invalid Date: ${abs}`);
120
+ }
121
+ */
122
+ const year = yearFromFixed(abs);
123
+ const priorDays = abs - toFixed(year, 1, 1);
124
+ const correction = abs < toFixed(year, 3, 1) ? 0 : isGregLeapYear(year) ? 1 : 2;
125
+ const month = quotient(12 * (priorDays + correction) + 373, 367);
126
+ const day = abs - toFixed(year, month, 1) + 1;
127
+ const dt = new Date(year, month - 1, day);
128
+ if (year < 100 && year >= 0) {
129
+ dt.setFullYear(year);
130
+ }
131
+ return dt;
132
+ }
133
+
134
+ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-namespace */
135
+ /**
136
+ * Gregorian date helper functions
48
137
  */
49
138
  var greg;
50
139
  (function (greg) {
51
- /**
52
- * Returns true if the Gregorian year is a leap year
53
- * @param {number} year Gregorian year
54
- * @return {boolean}
55
- */
56
- function isLeapYear(year) {
57
- return !(year % 4) && (!!(year % 100) || !(year % 400));
58
- }
59
- greg.isLeapYear = isLeapYear;
60
- /**
61
- * Number of days in the Gregorian month for given year
62
- * @param {number} month Gregorian month (1=January, 12=December)
63
- * @param {number} year Gregorian year
64
- * @return {number}
65
- */
66
- function daysInMonth(month, year) {
67
- // 1 based months
68
- return monthLengths[+isLeapYear(year)][month];
69
- }
70
- greg.daysInMonth = daysInMonth;
71
- /**
72
- * Returns true if the object is a Javascript Date
73
- * @param {Object} obj
74
- * @return {boolean}
75
- */
76
- function isDate(obj) {
77
- // eslint-disable-next-line no-prototype-builtins
78
- return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
79
- }
80
- greg.isDate = isDate;
81
- /**
82
- * @private
83
- * @param year
84
- * @param month (1-12)
85
- * @param day (1-31)
86
- */
87
- function toFixed(year, month, day) {
88
- const py = year - 1;
89
- return (365 * py +
90
- quotient(py, 4) -
91
- quotient(py, 100) +
92
- quotient(py, 400) +
93
- quotient(367 * month - 362, 12) +
94
- (month <= 2 ? 0 : isLeapYear(year) ? -1 : -2) +
95
- day);
96
- }
97
- /**
98
- * Converts Gregorian date to absolute R.D. (Rata Die) days
99
- * @param {Date} date Gregorian date
100
- * @return {number}
101
- */
102
- function greg2abs(date) {
103
- if (!isDate(date)) {
104
- throw new TypeError(`Argument not a Date: ${date}`);
105
- }
106
- const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
107
- /*
108
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
109
- throw new RangeError(`Invalid Date: ${date}`);
110
- }
111
- */
112
- return abs;
113
- }
114
- greg.greg2abs = greg2abs;
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
- * @param {number} abs - R.D. number of days
122
- * @return {Date}
123
- */
124
- function abs2greg(abs) {
125
- if (typeof abs !== 'number') {
126
- throw new TypeError(`Argument not a Number: ${abs}`);
127
- }
128
- abs = Math.trunc(abs);
129
- /*
130
- if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
131
- throw new RangeError(`Invalid Date: ${abs}`);
132
- }
133
- */
134
- const year = yearFromFixed(abs);
135
- const priorDays = abs - toFixed(year, 1, 1);
136
- const correction = abs < toFixed(year, 3, 1) ? 0 : isLeapYear(year) ? 1 : 2;
137
- const month = quotient(12 * (priorDays + correction) + 373, 367);
138
- const day = abs - toFixed(year, month, 1) + 1;
139
- const dt = new Date(year, month - 1, day);
140
- if (year < 100 && year >= 0) {
141
- dt.setFullYear(year);
142
- }
143
- return dt;
144
- }
145
- greg.abs2greg = abs2greg;
146
140
  })(greg || (greg = {}));
141
+ greg.abs2greg = abs2greg;
142
+ greg.daysInMonth = daysInGregMonth;
143
+ greg.greg2abs = greg2abs;
144
+ greg.isDate = isDate;
145
+ greg.isLeapYear = isGregLeapYear;
147
146
 
148
147
  const alefbet = {
149
148
  א: 1,
@@ -181,8 +180,6 @@ for (const [key, val] of Object.entries(alefbet)) {
181
180
  * Similar to `string.padStart(4, '0')` but will also format
182
181
  * negative numbers similar to how the JavaScript date formats
183
182
  * negative year numbers (e.g. `-37` is formatted as `-000037`).
184
- * @param {number} number
185
- * @return {string}
186
183
  */
187
184
  function pad4(number) {
188
185
  if (number < 0) {
@@ -202,8 +199,6 @@ function pad4(number) {
202
199
  /**
203
200
  * Formats a number with leading zeros so the resulting string is 2 digits long.
204
201
  * Similar to `string.padStart(2, '0')`.
205
- * @param {number} number
206
- * @return {string}
207
202
  */
208
203
  function pad2(number) {
209
204
  if (number < 10) {
@@ -213,9 +208,6 @@ function pad2(number) {
213
208
  }
214
209
  /**
215
210
  * Returns YYYY-MM-DD in the local timezone
216
- * @private
217
- * @param {Date} dt
218
- * @return {string}
219
211
  */
220
212
  function isoDateString(dt) {
221
213
  return (pad4(dt.getFullYear()) +
@@ -234,9 +226,9 @@ const noopLocale = {
234
226
  contexts: { '': {} },
235
227
  };
236
228
  const alias = {
237
- 'h': 'he',
238
- 'a': 'ashkenazi',
239
- 's': 'en',
229
+ h: 'he',
230
+ a: 'ashkenazi',
231
+ s: 'en',
240
232
  '': 'en',
241
233
  };
242
234
  /** @private */
@@ -257,12 +249,12 @@ class Locale {
257
249
  /**
258
250
  * Returns translation only if `locale` offers a non-empty translation for `id`.
259
251
  * Otherwise, returns `undefined`.
260
- * @param {string} id Message ID to translate
261
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
262
- * @return {string}
252
+ * @param id Message ID to translate
253
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
263
254
  */
264
255
  static lookupTranslation(id, locale) {
265
- const loc = (typeof locale === 'string' && locales.get(locale.toLowerCase())) || activeLocale;
256
+ const loc = (typeof locale === 'string' && locales.get(locale.toLowerCase())) ||
257
+ activeLocale;
266
258
  const array = loc[id];
267
259
  if ((array === null || array === void 0 ? void 0 : array.length) && array[0].length) {
268
260
  return array[0];
@@ -271,9 +263,8 @@ class Locale {
271
263
  }
272
264
  /**
273
265
  * By default, if no translation was found, returns `id`.
274
- * @param {string} id Message ID to translate
275
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
276
- * @return {string}
266
+ * @param id Message ID to translate
267
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
277
268
  */
278
269
  static gettext(id, locale) {
279
270
  const text = this.lookupTranslation(id, locale);
@@ -284,23 +275,24 @@ class Locale {
284
275
  }
285
276
  /**
286
277
  * Register locale translations.
287
- * @param {string} locale Locale name (i.e.: `'he'`, `'fr'`)
288
- * @param {LocaleData} data parsed data from a `.po` file.
278
+ * @param locale Locale name (i.e.: `'he'`, `'fr'`)
279
+ * @param data parsed data from a `.po` file.
289
280
  */
290
281
  static addLocale(locale, data) {
291
282
  if (typeof locale !== 'string') {
292
283
  throw new TypeError(`Invalid locale name: ${locale}`);
293
284
  }
294
- if (typeof data.contexts !== 'object' || typeof data.contexts[''] !== 'object') {
285
+ if (typeof data.contexts !== 'object' ||
286
+ typeof data.contexts[''] !== 'object') {
295
287
  throw new TypeError(`Locale '${locale}' invalid compact format`);
296
288
  }
297
289
  locales.set(locale.toLowerCase(), data.contexts['']);
298
290
  }
299
291
  /**
300
292
  * Adds a translation to `locale`, replacing any previous translation.
301
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`).
302
- * @param {string} id Message ID to translate
303
- * @param {string | string[]} translation Translation text
293
+ * @param locale Locale name (i.e: `'he'`, `'fr'`).
294
+ * @param id Message ID to translate
295
+ * @param translation Translation text
304
296
  */
305
297
  static addTranslation(locale, id, translation) {
306
298
  if (typeof locale !== 'string') {
@@ -327,8 +319,8 @@ class Locale {
327
319
  }
328
320
  /**
329
321
  * Adds multiple translations to `locale`, replacing any previous translations.
330
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`).
331
- * @param {LocaleData} data parsed data from a `.po` file.
322
+ * @param locale Locale name (i.e: `'he'`, `'fr'`).
323
+ * @param data parsed data from a `.po` file.
332
324
  */
333
325
  static addTranslations(locale, data) {
334
326
  if (typeof locale !== 'string') {
@@ -338,7 +330,8 @@ class Locale {
338
330
  if (!loc) {
339
331
  throw new TypeError(`Unknown locale: ${locale}`);
340
332
  }
341
- if (typeof data.contexts !== 'object' || typeof data.contexts[''] !== 'object') {
333
+ if (typeof data.contexts !== 'object' ||
334
+ typeof data.contexts[''] !== 'object') {
342
335
  throw new TypeError(`Locale '${locale}' invalid compact format`);
343
336
  }
344
337
  const ctx = data.contexts[''];
@@ -348,7 +341,7 @@ class Locale {
348
341
  * Activates a locale. Throws an error if the locale has not been previously added.
349
342
  * After setting the locale to be used, all strings marked for translations
350
343
  * will be represented by the corresponding translation in the specified locale.
351
- * @param {string} locale Locale name (i.e: `'he'`, `'fr'`)
344
+ * @param locale Locale name (i.e: `'he'`, `'fr'`)
352
345
  */
353
346
  static useLocale(locale) {
354
347
  const locale0 = locale.toLowerCase();
@@ -362,23 +355,20 @@ class Locale {
362
355
  }
363
356
  /**
364
357
  * Returns the name of the active locale (i.e. 'he', 'ashkenazi', 'fr')
365
- * @return {string}
366
358
  */
367
359
  static getLocaleName() {
368
360
  return activeName;
369
361
  }
370
362
  /**
371
363
  * Returns the names of registered locales
372
- * @return {string[]}
373
364
  */
374
365
  static getLocaleNames() {
375
366
  const keys = Array.from(locales.keys());
376
367
  return keys.sort((a, b) => a.localeCompare(b));
377
368
  }
378
369
  /**
379
- * @param {number} n
380
- * @param {string} [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
381
- * @return {string}
370
+ * Renders a number in ordinal, such as 1st, 2nd or 3rd
371
+ * @param [locale] Optional locale name (i.e: `'he'`, `'fr'`). Defaults to active locale.
382
372
  */
383
373
  static ordinal(n, locale) {
384
374
  const locale1 = locale === null || locale === void 0 ? void 0 : locale.toLowerCase();
@@ -405,11 +395,6 @@ class Locale {
405
395
  return n + '.';
406
396
  }
407
397
  }
408
- /**
409
- * @private
410
- * @param {number} n
411
- * @return {string}
412
- */
413
398
  static getEnOrdinal(n) {
414
399
  const s = ['th', 'st', 'nd', 'rd'];
415
400
  const v = n % 100;
@@ -417,8 +402,6 @@ class Locale {
417
402
  }
418
403
  /**
419
404
  * Removes nekudot from Hebrew string
420
- * @param {string} str
421
- * @return {string}
422
405
  */
423
406
  static hebrewStripNikkud(str) {
424
407
  return str.replace(/[\u0590-\u05bd]/g, '').replace(/[\u05bf-\u05c7]/g, '');
@@ -817,20 +800,30 @@ function getEventCategories(ev) {
817
800
  function renderTitleWithoutTime(ev) {
818
801
  return typeof ev.eventTime === 'undefined' ? ev.render() : ev.renderBrief();
819
802
  }
803
+ function shortLocationName(options) {
804
+ const loc = options.location;
805
+ if (!loc) {
806
+ return null;
807
+ }
808
+ if (options.preferAsciiName) {
809
+ const asciiname = loc.asciiname;
810
+ if (typeof asciiname === 'string') {
811
+ return asciiname;
812
+ }
813
+ }
814
+ return loc.getShortName();
815
+ }
820
816
  /**
821
817
  * Generates a title like "Hebcal 2020 Israel" or "Hebcal May 1993 Providence"
822
818
  */
823
819
  function getCalendarTitle(events, options) {
824
820
  let title = 'Hebcal';
825
- const location = options.location;
826
- const locationName = location === null || location === void 0 ? void 0 : location.getName();
821
+ const locationName = shortLocationName(options);
827
822
  if (options.yahrzeit) {
828
823
  title += ' Yahrzeits and Anniversaries';
829
824
  }
830
825
  else if (locationName) {
831
- const comma = locationName.indexOf(',');
832
- const name = (comma == -1) ? locationName : locationName.substring(0, comma);
833
- title += ' ' + name;
826
+ title += ' ' + locationName;
834
827
  }
835
828
  else if (options.il) {
836
829
  title += ' Israel';
@@ -1144,9 +1137,9 @@ function eventToClassicApiObject(ev, options, leyning$1 = true) {
1144
1137
  if (reading) {
1145
1138
  result.leyning = formatLeyningResult(reading);
1146
1139
  const hyear = hd.getFullYear();
1147
- if (isParsha && !il && hyear >= 5745) {
1148
- const triReading = triennial.getTriennialForParshaHaShavua(ev);
1149
- if (triReading) {
1140
+ if (isParsha && hyear >= 5745) {
1141
+ const triReading = triennial.getTriennialForParshaHaShavua(ev, il);
1142
+ if (triReading === null || triReading === void 0 ? void 0 : triReading.aliyot) {
1150
1143
  result.leyning.triennial = formatAliyot({}, triReading.aliyot);
1151
1144
  }
1152
1145
  }
@@ -1219,6 +1212,18 @@ function formatAliyot(result, aliyot) {
1219
1212
  }
1220
1213
  return result;
1221
1214
  }
1215
+ function formatReasons(result, reason) {
1216
+ for (const num of ['7', '8', 'M']) {
1217
+ if (reason[num]) {
1218
+ const k = num == 'M' ? 'maftir' : num;
1219
+ result[k] += ' | ' + reason[num];
1220
+ }
1221
+ }
1222
+ if (reason.haftara) {
1223
+ result.haftarah += ' | ' + reason.haftara;
1224
+ }
1225
+ return result;
1226
+ }
1222
1227
  function formatLeyningResult(reading) {
1223
1228
  const result = {};
1224
1229
  if (reading.summary) {
@@ -1234,15 +1239,7 @@ function formatLeyningResult(reading) {
1234
1239
  formatAliyot(result, reading.fullkriyah);
1235
1240
  }
1236
1241
  if (reading.reason) {
1237
- for (const num of ['7', '8', 'M']) {
1238
- if (reading.reason[num]) {
1239
- const k = num == 'M' ? 'maftir' : num;
1240
- result[k] += ' | ' + reading.reason[num];
1241
- }
1242
- }
1243
- if (reading.reason.haftara) {
1244
- result.haftarah += ' | ' + reading.reason.haftara;
1245
- }
1242
+ formatReasons(result, reading.reason);
1246
1243
  }
1247
1244
  return result;
1248
1245
  }