Selaa lähdekoodia

fix: for date and today, convert to user timezone, not server timezone; change `now` to Luxon Date; make month 1-based

Also:
- chore: updates deps and devDeps.
Brett Zamir 2 vuotta sitten
vanhempi
commit
f8226fa0c1
5 muutettua tiedostoa jossa 476 lisäystä ja 297 poistoa
  1. 6 1
      .eslintrc.cjs
  2. 34 27
      api/controllers/bDateController.js
  3. 1 1
      bin/bahai-date-api.js
  4. 21 21
      package.json
  5. 414 247
      pnpm-lock.yaml

+ 6 - 1
.eslintrc.cjs

@@ -3,7 +3,12 @@
 module.exports = {
   extends: ['ash-nazg/sauron-node-overrides'],
   parserOptions: {
-    ecmaVersion: 2021
+    ecmaVersion: 2022
+  },
+  settings: {
+    jsdoc: {
+      mode: 'typescript'
+    }
   },
   rules: {
     'import/no-commonjs': 0,

+ 34 - 27
api/controllers/bDateController.js

@@ -10,9 +10,9 @@ import {LocalBadiDate} from 'badidate';
  */
 
 /**
- * @param {Date} dte
+ * @param {LuxonDateObject} dte
  * @param {DateConfig} dateCfg
- * @returns {LocalBadiDate}
+ * @returns {[LuxonDate, LocalBadiDate]}
  */
 function createDateObject (dte, {
   // Bahjí
@@ -20,8 +20,13 @@ function createDateObject (dte, {
   longitude = 35.0924,
   timezoneId = 'Asia/Jerusalem'
 } = {}) {
-  const luxonDate = luxon.DateTime.fromJSDate(dte);
-  return new LocalBadiDate(luxonDate, latitude, longitude, timezoneId);
+  const luxonDate = luxon.DateTime.fromObject(dte).setZone(
+    timezoneId
+  );
+  return [
+    luxonDate,
+    new LocalBadiDate(luxonDate, latitude, longitude, timezoneId)
+  ];
 }
 
 /**
@@ -40,7 +45,11 @@ function sanitizeTimeZone (tz) {
 }
 
 /**
- * @param {any} s
+ * @typedef {any} ArbitraryArgumentToConvert
+ */
+
+/**
+ * @param {ArbitraryArgumentToConvert} s
  * @returns {Float|undefined}
  */
 function sanitizeFloat (s) {
@@ -53,7 +62,7 @@ function sanitizeFloat (s) {
 }
 
 /**
- * @param {any} s
+ * @param {ArbitraryArgumentToConvert} s
  * @returns {Integer}
  */
 function sanitizeInteger (s) {
@@ -104,13 +113,11 @@ function test (req, res) {
  * @returns {BadiDateResponse}
  */
 const getTodayJSON = function (dateObj = {}) {
-  const now = new Date();
-
   const latitude = sanitizeFloat(dateObj.latitude);
   const longitude = sanitizeFloat(dateObj.longitude);
   const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
 
-  const nowBadi = createDateObject(now, {
+  const [now, nowBadi] = createDateObject({}, {
     latitude, longitude, timezoneId
   });
 
@@ -127,13 +134,13 @@ const getTodayJSON = function (dateObj = {}) {
         timezone_id: nowBadi.timezoneId
       },
       greg_date: {
-        year: now.getFullYear(),
-        month: now.getMonth() + 1,
-        day: now.getDate(),
-        hour: now.getHours(),
-        minute: now.getMinutes(),
-        second: now.getSeconds(),
-        timezoneOffset: now.getTimezoneOffset()
+        year: now.year,
+        month: now.month,
+        day: now.day,
+        hour: now.hour,
+        minute: now.minute,
+        second: now.second,
+        timezoneOffset: now.offset
       }
     }
   };
@@ -192,19 +199,19 @@ function date (req, res) {
  */
 const getDate = function (dateObj) {
   const year = sanitizeInteger(dateObj.year);
-  const month = sanitizeInteger(dateObj.month) - 1;
+  const month = sanitizeInteger(dateObj.month);
   const day = sanitizeInteger(dateObj.day);
   const hour = sanitizeInteger(dateObj.hour);
   const minute = sanitizeInteger(dateObj.minute);
   const second = sanitizeInteger(dateObj.second);
 
-  const now = new Date(year, month, day, hour, minute, second);
-
   const latitude = sanitizeFloat(dateObj.latitude);
   const longitude = sanitizeFloat(dateObj.longitude);
   const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
 
-  const nowBadi = createDateObject(now, {
+  const [now, nowBadi] = createDateObject({
+    year, month, day, hour, minute, second
+  }, {
     latitude, longitude, timezoneId
   });
 
@@ -221,13 +228,13 @@ const getDate = function (dateObj) {
         timezone_id: nowBadi.timezoneId
       },
       greg_date: {
-        year,
-        month: month + 1,
-        day,
-        hour,
-        minute,
-        second,
-        timezoneOffset: now.getTimezoneOffset()
+        year: now.year,
+        month: now.month,
+        day: now.day,
+        hour: now.hour,
+        minute: now.minute,
+        second: now.second,
+        timezoneOffset: now.offset
       }
     }
   };

+ 1 - 1
bin/bahai-date-api.js

@@ -24,7 +24,7 @@ if (date) {
   const dte = new Date(date);
   dateObj = getDate({
     year: dte.getFullYear(),
-    month: dte.getMonth(),
+    month: dte.getMonth() + 1,
     day: dte.getDate(),
     hour: dte.getHours(),
     minute: dte.getMinutes(),

+ 21 - 21
package.json

@@ -12,7 +12,7 @@
     "today": "./bin/bahai-date-api.js",
     "date": "./bin/bahai-date-api.js --date 1999-01-01",
     "today-json": "./bin/bahai-date-api.js --verbose",
-    "date-json": "./bin/bahai-date-api.js  --verbose --date 1999-01-01",
+    "date-json": "./bin/bahai-date-api.js --verbose --date 1999-01-01",
     "cli-publish": "clp --config=\"bin/optionDefinitions.js\" --target images/cli.svg",
     "start": "nodemon server.js",
     "test": "echo \"Error: No test specified\" && exit 1"
@@ -41,31 +41,31 @@
   "devDependencies": {
     "@brettz9/eslint-plugin": "^1.0.4",
     "command-line-publish": "^1.1.0",
-    "eslint": "^8.8.0",
-    "eslint-config-ash-nazg": "^32.3.0",
-    "eslint-config-standard": "^16.0.3",
-    "eslint-plugin-array-func": "^3.1.7",
-    "eslint-plugin-compat": "^4.0.2",
+    "eslint": "^8.36.0",
+    "eslint-config-ash-nazg": "^34.8.0",
+    "eslint-config-standard": "^17.0.0",
+    "eslint-plugin-array-func": "^3.1.8",
+    "eslint-plugin-compat": "^4.1.2",
     "eslint-plugin-eslint-comments": "^3.2.0",
-    "eslint-plugin-html": "^6.2.0",
-    "eslint-plugin-import": "^2.25.4",
-    "eslint-plugin-jsdoc": "^37.7.1",
-    "eslint-plugin-markdown": "^2.2.1",
-    "eslint-plugin-no-unsanitized": "^4.0.1",
+    "eslint-plugin-html": "^7.1.0",
+    "eslint-plugin-import": "^2.27.5",
+    "eslint-plugin-jsdoc": "^40.0.2",
+    "eslint-plugin-markdown": "^3.0.0",
+    "eslint-plugin-n": "^15.6.1",
+    "eslint-plugin-no-unsanitized": "^4.0.2",
     "eslint-plugin-no-use-extend-native": "^0.5.0",
-    "eslint-plugin-node": "^11.1.0",
-    "eslint-plugin-promise": "^6.0.0",
-    "eslint-plugin-sonarjs": "^0.11.0",
+    "eslint-plugin-promise": "^6.1.1",
+    "eslint-plugin-sonarjs": "^0.18.0",
     "eslint-plugin-standard": "^4.1.0",
-    "eslint-plugin-unicorn": "^40.1.0",
-    "nodemon": "^2.0.15"
+    "eslint-plugin-unicorn": "^46.0.0",
+    "nodemon": "^2.0.21"
   },
   "dependencies": {
     "badidate": "^3.0.2",
-    "body-parser": "^1.19.1",
-    "command-line-basics": "^1.0.2",
-    "express": "^4.17.2",
-    "express-rate-limit": "^6.2.0",
-    "luxon": "^2.3.0"
+    "body-parser": "^1.20.2",
+    "command-line-basics": "^1.1.0",
+    "express": "^4.18.2",
+    "express-rate-limit": "^6.7.0",
+    "luxon": "^3.3.0"
   }
 }

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 414 - 247
pnpm-lock.yaml


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä