badiDate.mjs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**
  2. * @license BadiDate v3.0.2
  3. * (c) 2018 Jan Greis
  4. * licensed under MIT
  5. */
  6. import { DateTime, Duration } from 'luxon';
  7. const month = {
  8. 1: 'Bahá',
  9. 2: 'Jalál',
  10. 3: 'Jamál',
  11. 4: '‘Aẓamat',
  12. 5: 'Núr',
  13. 6: 'Raḥmat',
  14. 7: 'Kalimát',
  15. 8: 'Kamál',
  16. 9: 'Asmá’',
  17. 10: '‘Izzat',
  18. 11: 'Ma_sh_íyyat',
  19. 12: '‘Ilm',
  20. 13: 'Qudrat',
  21. 14: 'Qawl',
  22. 15: 'Masá’il',
  23. 16: '_Sh_araf',
  24. 17: 'Sulṭán',
  25. 18: 'Mulk',
  26. 19: '‘Alá’',
  27. 20: 'Ayyám-i-Há',
  28. };
  29. const monthL = {
  30. 1: 'Splendour',
  31. 2: 'Glory',
  32. 3: 'Beauty',
  33. 4: 'Grandeur',
  34. 5: 'Light',
  35. 6: 'Mercy',
  36. 7: 'Words',
  37. 8: 'Perfection',
  38. 9: 'Names',
  39. 10: 'Might',
  40. 11: 'Will',
  41. 12: 'Knowledge',
  42. 13: 'Power',
  43. 14: 'Speech',
  44. 15: 'Questions',
  45. 16: 'Honour',
  46. 17: 'Sovereignty',
  47. 18: 'Dominion',
  48. 19: 'Loftiness',
  49. 20: 'Ayyám-i-Há',
  50. };
  51. const holyDay = {
  52. 1: 'Naw-Rúz',
  53. 2: 'First day of Riḍván',
  54. 3: 'Ninth day of Riḍván',
  55. 4: 'Twelfth day of Riḍván',
  56. 5: 'Declaration of the Báb',
  57. 6: 'Ascension of Bahá’u’lláh',
  58. 7: 'Martyrdom of the Báb',
  59. 8: 'Birth of the Báb',
  60. 9: 'Birth of Bahá’u’lláh',
  61. 10: 'Day of the Covenant',
  62. 11: 'Ascension of ‘Abdu’l-Bahá',
  63. };
  64. // CAREFUL: Numbering corresponds to Badí' week, i.e. 1 is Jalál (-> Saturday)
  65. const weekday = {
  66. 1: 'Jalál',
  67. 2: 'Jamál',
  68. 3: 'Kamál',
  69. 4: 'Fiḍál',
  70. 5: '‘Idál',
  71. 6: 'Istijlál',
  72. 7: 'Istiqlál',
  73. };
  74. const weekdayAbbr3 = {
  75. 1: 'Jal',
  76. 2: 'Jam',
  77. 3: 'Kam',
  78. 4: 'Fiḍ',
  79. 5: '‘Idá',
  80. 6: 'Isj',
  81. 7: 'Isq',
  82. };
  83. const weekdayAbbr2 = {
  84. 1: 'Jl',
  85. 2: 'Jm',
  86. 3: 'Ka',
  87. 4: 'Fi',
  88. 5: '‘Id',
  89. 6: 'Ij',
  90. 7: 'Iq',
  91. };
  92. const weekdayL = {
  93. 1: 'Glory',
  94. 2: 'Beauty',
  95. 3: 'Perfection',
  96. 4: 'Grace',
  97. 5: 'Justice',
  98. 6: 'Majesty',
  99. 7: 'Independence',
  100. };
  101. const yearInVahid = {
  102. 1: 'Alif',
  103. 2: 'Bá’',
  104. 3: 'Ab',
  105. 4: 'Dál',
  106. 5: 'Báb',
  107. 6: 'Váv',
  108. 7: 'Abad',
  109. 8: 'Jád',
  110. 9: 'Bahá',
  111. 10: 'Ḥubb',
  112. 11: 'Bahháj',
  113. 12: 'Javáb',
  114. 13: 'Aḥad',
  115. 14: 'Vahháb',
  116. 15: 'Vidád',
  117. 16: 'Badí‘',
  118. 17: 'Bahí',
  119. 18: 'Abhá',
  120. 19: 'Váḥid',
  121. };
  122. const vahid = 'Váḥid';
  123. const kulliShay = 'Kull-i-_Sh_ay’';
  124. const BE = 'B.E.';
  125. const badiCalendar = 'Badí‘ Calendar';
  126. const unicodeCharForZero = '0';
  127. const defaultFormat = 'd MM+ y BE';
  128. var en = /*#__PURE__*/Object.freeze({
  129. __proto__: null,
  130. month: month,
  131. monthL: monthL,
  132. holyDay: holyDay,
  133. weekday: weekday,
  134. weekdayAbbr3: weekdayAbbr3,
  135. weekdayAbbr2: weekdayAbbr2,
  136. weekdayL: weekdayL,
  137. yearInVahid: yearInVahid,
  138. vahid: vahid,
  139. kulliShay: kulliShay,
  140. BE: BE,
  141. badiCalendar: badiCalendar,
  142. unicodeCharForZero: unicodeCharForZero,
  143. defaultFormat: defaultFormat
  144. });
  145. /* eslint-disable dot-notation, line-comment-position, camelcase, sort-imports */
  146. const badiLocale = { en, default: en };
  147. const setDefaultLanguage = (language) => {
  148. if (badiLocale[language] === undefined) {
  149. // eslint-disable-next-line no-console
  150. console.log('Chosen language does not exist. Setting has not been changed.');
  151. }
  152. else {
  153. badiLocale['default'] = badiLocale[language];
  154. }
  155. };
  156. let underlineFormat = 'css';
  157. const setUnderlineFormat = (format) => {
  158. if (['css', 'u', 'diacritic', 'none'].includes(format)) {
  159. underlineFormat = format;
  160. }
  161. else {
  162. // eslint-disable-next-line no-console
  163. console.log('Invalid underline format. Choose one of ["css", "u", "diacritic", "none"]. ' +
  164. 'Setting has not been changed.');
  165. }
  166. };
  167. const formatTokens = [
  168. ['DDL', 'DD+', 'MML', 'MM+', 'WWL', 'yyv', 'KiS'],
  169. ['dd', 'DD', 'mm', 'MM', 'ww', 'WW', 'yv', 'YV', 'vv', 'kk', 'yy', 'BE', 'BC', 'Va'],
  170. ['d', 'D', 'm', 'M', 'W', 'v', 'k', 'y']
  171. ];
  172. // eslint-disable-next-line complexity
  173. const formatBadiDate = (badiDate, formatString, language) => {
  174. if (!badiDate.isValid) {
  175. return 'Not a valid Badí‘ date';
  176. }
  177. if (typeof language === 'string' && badiLocale[language] === undefined && language.includes('-')) {
  178. language = language.split('-')[0];
  179. }
  180. if (language === undefined || badiLocale[language] === undefined) {
  181. language = 'default';
  182. }
  183. formatString = formatString !== null && formatString !== void 0 ? formatString : formatItemFallback(language, 'defaultFormat');
  184. let formattedDate = '';
  185. const length = formatString.length;
  186. for (let i = 0; i < length; i++) {
  187. // Text wrapped in {} is output as-is. A '{' without a matching '}'
  188. // results in invalid input
  189. if (formatString[i] === '{' && i < length - 1) {
  190. for (let j = i + 1; j <= length; j++) {
  191. if (j === length) {
  192. return 'Invalid formatting string.';
  193. }
  194. if (formatString[j] === '}') {
  195. i = j;
  196. break;
  197. }
  198. formattedDate += formatString[j];
  199. }
  200. }
  201. else {
  202. const next1 = formatString[i];
  203. const next2 = next1 + formatString[i + 1];
  204. const next3 = next2 + formatString[i + 2];
  205. if (formatTokens[0].includes(next3)) {
  206. formattedDate += getFormatItem(badiDate, next3, language);
  207. i += 2;
  208. }
  209. else if (formatTokens[1].includes(next2)) {
  210. formattedDate += getFormatItem(badiDate, next2, language);
  211. i += 1;
  212. }
  213. else if (formatTokens[2].includes(next1)) {
  214. formattedDate += getFormatItem(badiDate, next1, language);
  215. }
  216. else {
  217. formattedDate += next1;
  218. }
  219. }
  220. }
  221. return formattedDate;
  222. };
  223. // eslint-disable-next-line complexity
  224. const getFormatItem = (badiDate, token, language) => {
  225. switch (token) {
  226. // Single character tokens
  227. case 'd':
  228. return digitRewrite(badiDate.day, language);
  229. case 'D':
  230. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day), 3);
  231. case 'm':
  232. return digitRewrite(badiDate.month, language);
  233. case 'M':
  234. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month), 3);
  235. case 'W':
  236. return formatItemFallback(language, 'weekdayAbbr3', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  237. case 'y':
  238. return digitRewrite(badiDate.year, language);
  239. case 'v':
  240. return digitRewrite((Math.floor((badiDate.year - 1) / 19) % 19) + 1, language);
  241. case 'k':
  242. return digitRewrite(Math.floor((badiDate.year - 1) / 361) + 1, language);
  243. // Two character tokens
  244. case 'dd':
  245. return digitRewrite((`0${String(badiDate.day)}`).slice(-2), language);
  246. case 'DD':
  247. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day));
  248. case 'mm':
  249. return digitRewrite((`0${String(badiDate.month)}`).slice(-2), language);
  250. case 'MM':
  251. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month));
  252. case 'ww':
  253. return formatItemFallback(language, 'weekdayAbbr2', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  254. case 'WW':
  255. return formatItemFallback(language, 'weekday', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  256. case 'yy':
  257. return digitRewrite((`00${String(badiDate.year)}`).slice(-3), language);
  258. case 'yv':
  259. return digitRewrite((badiDate.year - 1) % 19 + 1, language);
  260. case 'YV':
  261. return formatItemFallback(language, 'yearInVahid', (badiDate.year - 1) % 19 + 1);
  262. case 'vv':
  263. return digitRewrite((`0${String((Math.floor((badiDate.year - 1) / 19) + 2) % 19 - 1)}`).slice(-2), language);
  264. case 'kk':
  265. return digitRewrite((`0${String(Math.floor((badiDate.year - 1) / 361) + 1)}`).slice(-2), language);
  266. case 'Va':
  267. return formatItemFallback(language, 'vahid');
  268. case 'BE':
  269. return formatItemFallback(language, 'BE');
  270. case 'BC':
  271. return formatItemFallback(language, 'badiCalendar');
  272. // Three character tokens
  273. case 'DDL':
  274. return formatItemFallback(language, 'monthL', badiDate.day);
  275. case 'DD+': {
  276. const day = postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day));
  277. const dayL = formatItemFallback(language, 'monthL', badiDate.day);
  278. if (day === dayL) {
  279. return day;
  280. }
  281. if (badiLocale[language] === badiLocale.fa) {
  282. return `<span dir="rtl">${day} (${dayL})</span>`;
  283. }
  284. return `${day} (${dayL})`;
  285. }
  286. case 'MML':
  287. return formatItemFallback(language, 'monthL', badiDate.month);
  288. case 'MM+': {
  289. const month = postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month));
  290. const monthL = formatItemFallback(language, 'monthL', badiDate.month);
  291. if (month === monthL) {
  292. return month;
  293. }
  294. if (badiLocale[language] === badiLocale.fa) {
  295. return `<span dir="rtl">${month} (${monthL})</span>`;
  296. }
  297. return `${month} (${monthL})`;
  298. }
  299. case 'WWL':
  300. return formatItemFallback(language, 'weekdayL', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  301. case 'yyv':
  302. return digitRewrite((`0${String((badiDate.year - 1) % 19 + 1)}`).slice(-2), language);
  303. case 'KiS':
  304. return postProcessLocaleItem(formatItemFallback(language, 'kulliShay'));
  305. // istanbul ignore next
  306. default:
  307. return '';
  308. }
  309. };
  310. const postProcessLocaleItem = (item, crop) => {
  311. if (crop && crop < item.length) {
  312. let char = 0;
  313. let counter = 0;
  314. while (counter < crop) {
  315. if (!'_’‘'.includes(item[char])) {
  316. counter++;
  317. }
  318. char++;
  319. }
  320. if ('_’‘'.includes(item[char])) {
  321. char++;
  322. }
  323. item = item.slice(0, char);
  324. if (item.split('_').length % 2 === 0) {
  325. item += '_';
  326. }
  327. }
  328. const stringComponents = item.split('_');
  329. for (let i = 1; i < stringComponents.length; i += 2) {
  330. stringComponents[i] = underlineString(stringComponents[i]);
  331. }
  332. return stringComponents.join('');
  333. };
  334. const underlineString = (str) => {
  335. switch (underlineFormat) {
  336. case 'css':
  337. return `<span style="text-decoration:underline">${str}</span>`;
  338. case 'diacritic':
  339. return str.split('').map(char => `${char}\u0332`).join('');
  340. case 'u':
  341. return `<u>${str}</u>`;
  342. case 'none':
  343. return str;
  344. // istanbul ignore next
  345. default:
  346. throw new TypeError('Unexpected underlineFormat');
  347. }
  348. };
  349. const digitRewrite = (number, language) => {
  350. number = String(number);
  351. const unicodeOffset = formatItemFallback(language, 'unicodeCharForZero').charCodeAt(0) - '0'.charCodeAt(0);
  352. if (unicodeOffset === 0) {
  353. return number;
  354. }
  355. const codePoints = [...number].map(num => num.charCodeAt(0) + unicodeOffset);
  356. return String.fromCharCode(...codePoints);
  357. };
  358. const formatItemFallback = (language, category, index) => {
  359. var _a;
  360. if (index === undefined) {
  361. while (badiLocale[language][category] === undefined) {
  362. language = languageFallback(language);
  363. }
  364. return badiLocale[language][category];
  365. }
  366. while (((_a = badiLocale[language][category]) === null || _a === void 0 ? void 0 : _a[index]) === undefined) {
  367. language = languageFallback(language);
  368. }
  369. return badiLocale[language][category][index];
  370. };
  371. const languageFallback = (languageCode) => {
  372. if (languageCode.includes('-')) {
  373. return languageCode.split('-')[0];
  374. // eslint-disable-next-line no-negated-condition
  375. }
  376. else if (languageCode !== 'default') {
  377. return 'default';
  378. }
  379. return 'en';
  380. };
  381. const badiYears = [
  382. 'l4da', 'k4ci', 'k5c7', 'l4d6', 'l4ce', 'k4c4', 'k5d4', 'l4cb', 'l4c1', 'k4cj', 'k5c8', 'l4d7', 'l4cf', 'k4c5',
  383. 'k4d5', 'k5ce', 'l4c2', 'k4d2', 'k4ca', 'k5da', 'l4ch', 'k4c6', 'k4d6', 'k5cf', 'l4c4', 'k4d4', 'k4cc', 'k5c1',
  384. 'l4cj', 'k4c8', 'k4d8', 'k5cg', 'l4c5', 'k4d5', 'k4ce', 'k5c3', 'l4d2', 'k4ca', 'k4d9', 'k5ci', 'l4c6', 'k4d6',
  385. 'k4cf', 'k4c4', 'k5d4', 'k4cb', 'k4bj', 'k4cj', 'k5c9', 'k4d8', 'k4cg', 'k4c6', 'k5d6', 'k4cd', 'k4c2', 'k4d2',
  386. 'k5ca', 'k4d9', 'k4ci', 'k4c7', 'k5d7', 'k4cf', 'k4c4', 'k4d4', 'k5cc', 'k4bj', 'k4cj', 'k4c9', 'k5d9', 'k4cg',
  387. 'k4c6', 'k4d5', 'k5cd', 'k4c2', 'k4d1', 'k4ca', 'k4da', 'j5cj', 'k4c7', 'k4d7', 'k4cf', 'j5c4', 'k4d3', 'k4cb',
  388. 'k4c1', 'k5d1', 'l4c9', 'l4d9', 'l4ch', 'k5c6', 'l4d5', 'l4cd', 'l4c2', 'k5d2', 'l4ca', 'l4da', 'l4cj', 'k5c8',
  389. 'l4d7', 'l4cf', 'l4c4', 'k5d4', 'l4cb', 'l4c1', 'l4d1', 'k5c9', 'l4d8', 'l4cg', 'l4c5', 'k4d5', 'k5ce', 'l4c2',
  390. 'l4d2', 'k4cb', 'k5db', 'l4ci', 'l4c7', 'k4d7', 'k5cf', 'l4c4', 'l4d4', 'k4cc', 'k5c2', 'l4d1', 'l4c9', 'k4d9',
  391. 'k5ch', 'l4c5', 'l4d5', 'k4ce', 'k5c3', 'l4d2', 'l4cb', 'k4da', 'k5ci', 'l4c6', 'l4d6', 'k4cf', 'k5c5', 'l4d4',
  392. 'l4cc', 'k4c1', 'k4d1', 'k5c9', 'l4d8', 'k4cg', 'k4c6', 'k5d6', 'l4ce', 'k4c3', 'k4d3', 'k5cb', 'l4da', 'k4ci',
  393. 'k4c7', 'k5d7', 'l4cf', 'k4c5', 'k4d5', 'k5cd', 'l4c1', 'k4cj', 'k4c9', 'k5d9', 'l4cg', 'k4c6', 'k4d6', 'k5ce',
  394. 'l4c3', 'k4d2', 'k4ca', 'k5bj', 'l4ci', 'k4c7', 'k4d7', 'k4cg', 'k5c5', 'k4d4', 'k4cc', 'k4c1', 'k5d1', 'k4c9',
  395. 'k4d9', 'k4ch', 'k5c7', 'l4d6', 'l4ce', 'l4c3', 'l5d3', 'l4ca', 'l4da', 'l4cj', 'l5c8', 'l4d7', 'l4cg', 'l4c5',
  396. 'l5d4', 'l4cb', 'l4c1', 'l4d1', 'l5ca', 'l4d9', 'l4ch', 'l4c6', 'l5d6', 'l4cd', 'l4c2', 'l4d2', 'l4cb', 'k5c1',
  397. 'l4cj', 'l4c8', 'l4d8', 'k5cg', 'l4c4', 'l4d4', 'l4cc', 'k5c2', 'l4d1', 'l4ca', 'l4da', 'k5ci', 'l4c6', 'l4d5',
  398. 'l4ce', 'k5c3', 'l4d2', 'l4cb', 'l4db', 'k5cj', 'l4c8', 'l4d7', 'l4cf', 'k5c5', 'l4d4', 'l4cc', 'l4c2', 'k5d2',
  399. 'l4c9', 'l4d9', 'l4ch', 'k4c6', 'k5d6', 'l4ce', 'l4c3', 'k4d3', 'k5cc', 'l4db', 'l4cj', 'k4c8', 'k5d8', 'l4cf',
  400. 'l4c4', 'k4d5', 'k5cd', 'l4c2', 'l4d2', 'k4ca', 'k5d9', 'l4cg', 'l4c6', 'k4d6', 'k5cf', 'l4c3', 'l4d3', 'k4cb',
  401. 'k5bj', 'l4ci', 'l4c7', 'k4d7', 'k5cg', 'l4c5', 'l4d5', 'k4cd', 'k4c2', 'k5d2', 'l4c9', 'k4d9', 'k4ch', 'k5c7',
  402. 'l4d6', 'k4cf', 'k4c4', 'k5d4', 'l4cb', 'l4bj', 'l4cj', 'l5c8', 'm4d7', 'l4cg', 'l4c5', 'l5d5', 'm4cc', 'l4c1',
  403. 'l4d1', 'l5ca', 'm4d9', 'l4ch', 'l4c7', 'l5d7', 'm4ce', 'l4c3', 'l4d3', 'l5cb', 'm4bi', 'l4ci', 'l4c8', 'l4d8',
  404. 'l5ch', 'l4c5', 'l4d5', 'l4cd', 'l5c2', 'l4d1', 'l4c9', 'l4da', 'l5ci', 'l4c7', 'l4d7', 'l4cf', 'l5c4', 'l4d2',
  405. 'l4cb', 'l4bj', 'l5d1', 'l4c8', 'l4d8', 'l4cg', 'l5c5', 'l4d4', 'l4cc', 'l4c2', 'l5d2', 'l4c9', 'l4da', 'l4ci',
  406. ];
  407. class BadiDate {
  408. constructor(date) {
  409. this._holyDay = undefined;
  410. this._valid = true;
  411. this._invalidReason = undefined;
  412. try {
  413. if (this._isDateObject(date)) {
  414. this._gregorianDate = DateTime.fromObject({ year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate(), zone: 'UTC' });
  415. }
  416. else if (DateTime.isDateTime(date)) {
  417. this._gregorianDate = DateTime.fromObject({ year: date.year, month: date.month, day: date.day, zone: 'UTC' });
  418. }
  419. else if (this._isYearMonthDay(date) || this._isYearHolyDayNumber(date)) {
  420. this._setFromBadiDate(date);
  421. }
  422. else {
  423. throw new TypeError('Unrecognized input format');
  424. }
  425. if (this._year === undefined) {
  426. // We haven't set the Badí' date yet
  427. this._setFromGregorianDate();
  428. }
  429. this._setHolyDay();
  430. }
  431. catch (err) {
  432. this._setInvalid(err);
  433. }
  434. Object.freeze(this);
  435. }
  436. format(formatString, language) {
  437. return formatBadiDate(this, formatString, language);
  438. }
  439. _isDateObject(arg) {
  440. return Object.prototype.toString.call(arg) === '[object Date]';
  441. }
  442. _isYearMonthDay(arg) {
  443. return typeof arg.year === 'number' && typeof arg.month === 'number' &&
  444. typeof arg.day === 'number';
  445. }
  446. _isYearHolyDayNumber(arg) {
  447. return typeof arg.year === 'number' && arg.month === undefined &&
  448. arg.day === undefined && typeof arg.holyDayNumber === 'number';
  449. }
  450. _notInValidGregorianDateRange(datetime) {
  451. const lowerBound = DateTime.fromObject({ year: 1844, month: 3, day: 21, zone: 'UTC' });
  452. const upperBound = DateTime.fromObject({ year: 2351, month: 3, day: 20, zone: 'UTC' });
  453. return datetime < lowerBound || datetime > upperBound;
  454. }
  455. _setFromGregorianDate() {
  456. if (this._notInValidGregorianDateRange(this._gregorianDate)) {
  457. throw new RangeError('Input date outside of valid range (1844-03-21 - 2351-03-20)');
  458. }
  459. const gregorianYear = this._gregorianDate.year;
  460. const oldImplementationCutoff = DateTime.fromObject({ year: 2015, month: 3, day: 21, zone: 'UTC' });
  461. if (this._gregorianDate < oldImplementationCutoff) {
  462. const { month, day } = this._gregorianDate;
  463. if (month < 3 || (month === 3 && day < 21)) {
  464. this._nawRuz = DateTime.fromObject({ year: gregorianYear - 1, month: 3, day: 21, zone: 'UTC' });
  465. this._year = gregorianYear - 1844;
  466. }
  467. else {
  468. this._nawRuz = DateTime.fromObject({ year: gregorianYear, month: 3, day: 21, zone: 'UTC' });
  469. this._year = gregorianYear - 1843;
  470. }
  471. this._setOldAyyamiHaLength();
  472. this._yearTwinBirthdays = [12, 5, 13, 9];
  473. }
  474. else {
  475. this._year = gregorianYear - 1843;
  476. this._setBadiYearInfo(true);
  477. }
  478. this._setBadiMonthAndDay();
  479. }
  480. /**
  481. * Set Badí' month and day from Gregorian date
  482. */
  483. _setBadiMonthAndDay() {
  484. const dayOfBadiYear = this._dayOfYear(this._gregorianDate);
  485. if (dayOfBadiYear < 343) {
  486. this._month = Math.floor((dayOfBadiYear - 1) / 19 + 1);
  487. this._day = (dayOfBadiYear - 1) % 19 + 1;
  488. }
  489. else if (dayOfBadiYear < 343 + this._ayyamiHaLength) {
  490. this._month = 20;
  491. this._day = dayOfBadiYear - 342;
  492. }
  493. else {
  494. this._month = 19;
  495. this._day = dayOfBadiYear - (342 + this._ayyamiHaLength);
  496. }
  497. }
  498. _setFromBadiDate(date) {
  499. this._year = date.year;
  500. if (this._year < 1 || this._year > 507) {
  501. throw new RangeError('Input date outside of valid range (1 - 507 B.E.)');
  502. }
  503. else if (this._year < 172) {
  504. this._nawRuz = DateTime.fromObject({ year: 1843 + this._year, month: 3, day: 21, zone: 'UTC' });
  505. this._setOldAyyamiHaLength();
  506. this._yearTwinBirthdays = [12, 5, 13, 9];
  507. }
  508. else {
  509. this._setBadiYearInfo();
  510. }
  511. if (this._isYearMonthDay(date)) {
  512. this._month = date.month;
  513. this._day = date.day;
  514. if (this._month === 20 && this._day > this._ayyamiHaLength) {
  515. // If only off by one day, we'll bubble up so that 5th Ayyám-i-Há in a year with only 4 days of
  516. // Ayyám-i-Há can be salvaged
  517. if (this._day - this._ayyamiHaLength === 1) {
  518. this._month = 19;
  519. this._day = 1;
  520. }
  521. else {
  522. throw new TypeError('Input numbers do not designate a valid date');
  523. }
  524. }
  525. if (this._month < 1 || this._month > 20 || this._day < 1 || this.day > 19) {
  526. throw new TypeError('Input numbers do not designate a valid date');
  527. }
  528. }
  529. else {
  530. if (date.holyDayNumber < 1 || date.holyDayNumber > 11) {
  531. throw new TypeError('Input numbers do not designate a valid Holy Day');
  532. }
  533. this._holyDay = date.holyDayNumber;
  534. [this._month, this._day] = this._holyDayMapping()[this._holyDay];
  535. }
  536. this._gregorianDate = this._nawRuz.plus(Duration.fromObject({ days: this._dayOfYear([this._year, this._month, this._day]) - 1 }));
  537. }
  538. _setOldAyyamiHaLength() {
  539. if (DateTime.fromObject({ year: this._nawRuz.year + 1 }).isInLeapYear) {
  540. this._ayyamiHaLength = 5;
  541. }
  542. else {
  543. this._ayyamiHaLength = 4;
  544. }
  545. }
  546. _setBadiYearInfo(fromGregorianDate = false) {
  547. let yearData = this._extractBadiYearInfo();
  548. if (fromGregorianDate && this._gregorianDate < yearData.nawRuz) {
  549. this._year -= 1;
  550. yearData = this._extractBadiYearInfo();
  551. }
  552. this._nawRuz = yearData.nawRuz;
  553. this._ayyamiHaLength = yearData.ayyamiHaLength;
  554. this._yearTwinBirthdays = yearData.twinBirthdays;
  555. }
  556. _extractBadiYearInfo() {
  557. let nawRuz, ayyamiHaLength, twinBirthdays;
  558. // Check whether data needs to be unpacked or exists in the verbose version
  559. // istanbul ignore else
  560. if (badiYears[0] === 'l4da') {
  561. const components = badiYears[this._year - 172].split('');
  562. nawRuz = DateTime.fromObject({ year: this._year - 172 + 2015, month: 3, day: parseInt(components[0], 36), zone: 'UTC' });
  563. ayyamiHaLength = parseInt(components[1], 36);
  564. const TB1 = [parseInt(components[2], 36), parseInt(components[3], 36)];
  565. const TB2 = TB1[1] < 19 ? [TB1[0], TB1[1] + 1] : [TB1[0] + 1, 1];
  566. twinBirthdays = [TB1[0], TB1[1], TB2[0], TB2[1]];
  567. }
  568. else {
  569. ({ nawRuz, ayyamiHaLength, twinBirthdays } = badiYears[this._year]);
  570. nawRuz = DateTime.fromISO(nawRuz, { zone: 'UTC' });
  571. }
  572. return { nawRuz, ayyamiHaLength, twinBirthdays };
  573. }
  574. _dayOfYear(date) {
  575. // Naw-Rúz is day 1
  576. if (Array.isArray(date)) {
  577. // We have a Badí' date
  578. if (date[1] < 19) {
  579. return 19 * (date[1] - 1) + date[2];
  580. }
  581. else if (date[1] === 20) {
  582. return 342 + date[2];
  583. }
  584. // date[1] === 19
  585. return 342 + this._ayyamiHaLength + date[2];
  586. }
  587. return date.diff(this._nawRuz).as('days') + 1;
  588. }
  589. _setInvalid(invalidReason) {
  590. this._gregorianDate = DateTime.invalid('Not a valid Badí‘ date');
  591. this._year = NaN;
  592. this._month = NaN;
  593. this._day = NaN;
  594. this._ayyamiHaLength = NaN;
  595. this._nawRuz = DateTime.invalid('Not a valid Badí‘ date');
  596. this._valid = false;
  597. this._invalidReason = invalidReason;
  598. }
  599. _setHolyDay() {
  600. const mapping = this._holyDayMapping();
  601. this._holyDay = parseInt(Object.keys(mapping)
  602. .find(key => mapping[key][0] === this._month && mapping[key][1] === this._day), 10);
  603. }
  604. _holyDayMapping() {
  605. return {
  606. [1 /* NawRuz */]: [1, 1],
  607. [2 /* FirstRidvan */]: [2, 13],
  608. [3 /* NinthRidvan */]: [3, 2],
  609. [4 /* TwelfthRidvan */]: [3, 5],
  610. [5 /* DeclarationOfTheBab */]: [4, this._year < 172 ? 7 : 8],
  611. [6 /* AscensionOfBahaullah */]: [4, 13],
  612. [7 /* MartyrdomOfTheBab */]: [6, this._year < 172 ? 16 : 17],
  613. [8 /* BirthOfTheBab */]: [this._yearTwinBirthdays[0], this._yearTwinBirthdays[1]],
  614. [9 /* BirthOfBahaullah */]: [this._yearTwinBirthdays[2], this._yearTwinBirthdays[3]],
  615. [10 /* DayOfTheCovenant */]: [14, 4],
  616. [11 /* AscensionOfAbdulBaha */]: [14, 6],
  617. };
  618. }
  619. _leapYearsBefore() {
  620. let leapYearsBefore = Math.floor(Math.min(this.year - 1, 171) / 4);
  621. if (this.year > 172) {
  622. // istanbul ignore else
  623. if (badiYears[0] === 'l4da') {
  624. leapYearsBefore += badiYears.slice(0, this.year - 172).filter(entry => entry[1] === '5').length;
  625. }
  626. else {
  627. leapYearsBefore += Object.entries(badiYears)
  628. .filter(([year, data]) => parseInt(year, 10) < this.year &&
  629. data.ayyamiHaLength === 5).length;
  630. }
  631. }
  632. return leapYearsBefore;
  633. }
  634. holyDay(language = undefined) {
  635. if (!this._holyDay) {
  636. return '';
  637. }
  638. if (language === undefined || badiLocale[language] === undefined) {
  639. language = 'default';
  640. }
  641. return formatItemFallback(language, 'holyDay', this._holyDay);
  642. }
  643. valueOf() {
  644. return this._dayOfYear([this.year, this.month, this.day]) + this._leapYearsBefore() + (this.year - 1) * 365;
  645. }
  646. equals(other) {
  647. return this.isValid && other.isValid && this.valueOf() === other.valueOf();
  648. }
  649. get isValid() {
  650. return this._valid;
  651. }
  652. get invalidReason() {
  653. return this._invalidReason;
  654. }
  655. get day() {
  656. return this._day;
  657. }
  658. get month() {
  659. return this._month;
  660. }
  661. get year() {
  662. return this._year;
  663. }
  664. // number of the Badí' weekday between 1 (Jalál ~> Saturday) and 7 (Istiqlál ~> Friday).
  665. get weekday() {
  666. return (this._gregorianDate.weekday + 1) % 7 + 1;
  667. }
  668. get yearInVahid() {
  669. return (this._year - 1) % 19 + 1;
  670. }
  671. get vahid() {
  672. return (Math.floor((this._year - 1) / 19) % 19) + 1;
  673. }
  674. get kullIShay() {
  675. return Math.floor((this._year - 1) / 361) + 1;
  676. }
  677. // Gregorian date on whose sunset the Badí' date ends.
  678. get gregorianDate() {
  679. return this._gregorianDate;
  680. }
  681. get ayyamiHaLength() {
  682. return this._ayyamiHaLength;
  683. }
  684. get holyDayNumber() {
  685. return this._holyDay ? this._holyDay : undefined;
  686. }
  687. get workSuspended() {
  688. return this._holyDay ? this.holyDayNumber < 10 : undefined;
  689. }
  690. get nextMonth() {
  691. let { year, month } = this;
  692. switch (month) {
  693. case 18:
  694. month = 20;
  695. break;
  696. case 19:
  697. month = 1;
  698. year += 1;
  699. break;
  700. case 20:
  701. month = 19;
  702. break;
  703. default:
  704. month += 1;
  705. }
  706. return new BadiDate({ year, month, day: 1 });
  707. }
  708. get previousMonth() {
  709. let { year, month } = this;
  710. switch (month) {
  711. case 1:
  712. month = 19;
  713. year -= 1;
  714. break;
  715. case 19:
  716. month = 20;
  717. break;
  718. case 20:
  719. month = 18;
  720. break;
  721. default:
  722. month -= 1;
  723. }
  724. return new BadiDate({ year, month, day: 1 });
  725. }
  726. get nextDay() {
  727. if (this._day === 19 || (this._month === 20 && this._day === this._ayyamiHaLength)) {
  728. return this.nextMonth;
  729. }
  730. return new BadiDate({ year: this._year, month: this._month, day: this._day + 1 });
  731. }
  732. get previousDay() {
  733. if (this._day === 1) {
  734. const { previousMonth } = this;
  735. let day = 19;
  736. if (this._month === 19) {
  737. day = this._ayyamiHaLength;
  738. }
  739. return new BadiDate({
  740. year: previousMonth.year,
  741. month: previousMonth.month,
  742. day,
  743. });
  744. }
  745. return new BadiDate({ year: this._year, month: this._month, day: this._day - 1 });
  746. }
  747. }
  748. const badiDateSettings = (settings) => {
  749. if (settings.defaultLanguage) {
  750. setDefaultLanguage(settings.defaultLanguage);
  751. }
  752. if (settings.underlineFormat) {
  753. setUnderlineFormat(settings.underlineFormat);
  754. }
  755. };
  756. export { BadiDate, badiDateSettings };