bDateController.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* eslint-disable camelcase -- Current API */
  2. import * as luxon from 'luxon';
  3. import {LocalBadiDate} from 'badidate';
  4. /**
  5. * @typedef {PlainObject} DateConfig
  6. * @property {string} latitude
  7. * @property {string} longitude
  8. * @property {string} timezoneId
  9. */
  10. /**
  11. * @param {LuxonDateObject} dte
  12. * @param {DateConfig} dateCfg
  13. * @returns {[LuxonDate, LocalBadiDate]}
  14. */
  15. function createDateObject (dte, {
  16. // Bahjí
  17. latitude = 32.9434,
  18. longitude = 35.0924,
  19. timezoneId = 'Asia/Jerusalem'
  20. } = {}) {
  21. const luxonDate = luxon.DateTime.fromObject(dte).setZone(
  22. timezoneId
  23. );
  24. return [
  25. luxonDate,
  26. new LocalBadiDate(luxonDate, latitude, longitude, timezoneId)
  27. ];
  28. }
  29. /**
  30. * @param {string} tz
  31. * @returns {string|undefined}
  32. */
  33. function sanitizeTimeZone (tz) {
  34. try {
  35. const timeZone = tz.replaceAll(' ', '_');
  36. // eslint-disable-next-line no-new -- Testing
  37. new Intl.DateTimeFormat(undefined, {timeZone});
  38. return timeZone;
  39. } catch (err) {
  40. // Will allow default above to be used
  41. return undefined;
  42. }
  43. }
  44. /* eslint-disable jsdoc/reject-any-type -- Arbitrary */
  45. /**
  46. * @typedef {any} ArbitraryArgumentToConvert
  47. */
  48. /* eslint-enable jsdoc/reject-any-type -- Arbitrary */
  49. /**
  50. * @param {ArbitraryArgumentToConvert} s
  51. * @returns {Float|undefined}
  52. */
  53. function sanitizeFloat (s) {
  54. if (s === '' || s === undefined || s === null || s === false ||
  55. Number.isNaN(Number(s))) {
  56. // Will allow default above to be used
  57. return undefined;
  58. }
  59. return Number.parseFloat(s);
  60. }
  61. /**
  62. * @param {ArbitraryArgumentToConvert} s
  63. * @returns {Integer}
  64. */
  65. function sanitizeInteger (s) {
  66. if (s === '' || s === undefined || s === null || s === false ||
  67. Number.isNaN(Number(s))) {
  68. return 0;
  69. }
  70. return Number.parseInt(s);
  71. }
  72. /**
  73. * @param {Request} req
  74. * @param {Response} res
  75. * @returns {void}
  76. */
  77. function test (req, res) {
  78. res.json({message: 'Hi there'});
  79. }
  80. /**
  81. * @typedef {PlainObject} BadiDateInfo
  82. * @property {string} message
  83. * @property {PlainObject} badi_date
  84. * @property {Integer} badi_date.year
  85. * @property {Integer} badi_date.month
  86. * @property {Integer} badi_date.day
  87. * @property {string} badi_date.month_name
  88. * @property {string} badi_date.timezone_id
  89. * @property {PlainObject} greg_date
  90. * @property {Integer} greg_date.year
  91. * @property {Integer} greg_date.month
  92. * @property {Integer} greg_date.day
  93. * @property {Integer} greg_date.hour
  94. * @property {Integer} greg_date.minute
  95. * @property {Integer} greg_date.second
  96. * @property {Integer} greg_date.timezoneOffset
  97. */
  98. /**
  99. * @typedef {PlainObject} BadiDateResponse
  100. * @property {Date} now
  101. * @property {LocalBadiDate} nowBadi
  102. * @property {BadiDateInfo} json
  103. */
  104. /**
  105. * @param {DateConfig} dateObj
  106. * @returns {BadiDateResponse}
  107. */
  108. const getTodayJSON = function (dateObj = {}) {
  109. const latitude = sanitizeFloat(dateObj.latitude);
  110. const longitude = sanitizeFloat(dateObj.longitude);
  111. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  112. const [now, nowBadi] = createDateObject({}, {
  113. latitude, longitude, timezoneId
  114. });
  115. return {
  116. now,
  117. nowBadi,
  118. json: {
  119. message: 'Today is ' + nowBadi.badiDate.format(),
  120. badi_date: {
  121. year: nowBadi.badiDate.year,
  122. month: nowBadi.badiDate.month,
  123. day: nowBadi.badiDate.day,
  124. month_name: nowBadi.badiDate.format('MM+'),
  125. timezone_id: nowBadi.timezoneId
  126. },
  127. greg_date: {
  128. year: now.year,
  129. month: now.month,
  130. day: now.day,
  131. hour: now.hour,
  132. minute: now.minute,
  133. second: now.second,
  134. timezoneOffset: now.offset
  135. }
  136. }
  137. };
  138. };
  139. /**
  140. * @param {Request} req
  141. * @param {Response} res
  142. * @returns {void}
  143. */
  144. function today (req, res) {
  145. const {json, nowBadi} = getTodayJSON(req.query);
  146. // eslint-disable-next-line no-console -- CLI
  147. console.log('Today: ' + nowBadi.badiDate.format());
  148. res.json(json);
  149. }
  150. /**
  151. * @param {Request} req
  152. * @param {Response} res
  153. * @returns {void}
  154. */
  155. function todayHtml (req, res) {
  156. res.set('content-type', 'text/html;charset=utf-8');
  157. res.end(JSON.stringify(getTodayJSON(req.query).json, null, 2));
  158. }
  159. /**
  160. * @param {Request} req
  161. * @param {Response} res
  162. * @returns {void}
  163. */
  164. function date (req, res) {
  165. const dateInfo = getDate(req.query);
  166. // eslint-disable-next-line no-console -- CLI
  167. console.log(
  168. 'Date: ' + dateInfo.now.toString() + ' -> ' +
  169. dateInfo.nowBadi.badiDate.format()
  170. );
  171. res.json(dateInfo.json);
  172. }
  173. /**
  174. * @typedef {DateConfig} FullDateConfig
  175. * @property {Integer} year
  176. * @property {Integer} month
  177. * @property {Integer} day
  178. * @property {Integer} hour
  179. * @property {Integer} minute
  180. * @property {Integer} second
  181. */
  182. /**
  183. * @param {FullDateConfig} dateObj
  184. * @returns {BadiDateResponse}
  185. */
  186. const getDate = function (dateObj) {
  187. const year = sanitizeInteger(dateObj.year);
  188. const month = sanitizeInteger(dateObj.month);
  189. const day = sanitizeInteger(dateObj.day);
  190. const hour = sanitizeInteger(dateObj.hour);
  191. const minute = sanitizeInteger(dateObj.minute);
  192. const second = sanitizeInteger(dateObj.second);
  193. const latitude = sanitizeFloat(dateObj.latitude);
  194. const longitude = sanitizeFloat(dateObj.longitude);
  195. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  196. const [now, nowBadi] = createDateObject({
  197. year, month, day, hour, minute, second
  198. }, {
  199. latitude, longitude, timezoneId
  200. });
  201. return {
  202. now,
  203. nowBadi,
  204. json: {
  205. message: 'The date is: ' + nowBadi.badiDate.format(),
  206. badi_date: {
  207. year: nowBadi.badiDate.year,
  208. month: nowBadi.badiDate.month,
  209. day: nowBadi.badiDate.day,
  210. month_name: nowBadi.badiDate.format('MM+'),
  211. timezone_id: nowBadi.timezoneId
  212. },
  213. greg_date: {
  214. year: now.year,
  215. month: now.month,
  216. day: now.day,
  217. hour: now.hour,
  218. minute: now.minute,
  219. second: now.second,
  220. timezoneOffset: now.offset
  221. }
  222. }
  223. };
  224. };
  225. export {test, getTodayJSON, today, todayHtml, date, getDate};