salat 2.0.2 → 3.0.0

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/README.md CHANGED
@@ -25,6 +25,12 @@ $ salat
25
25
  $ salat [cityName]
26
26
  ```
27
27
 
28
+ Alternatively, if you don't want to install it globally, you can just use:
29
+
30
+ ```bash
31
+ npx salat
32
+ ```
33
+
28
34
  `City name should be provided the same way it's written in the cities.json`
29
35
 
30
36
  ## Output
package/app.js CHANGED
@@ -8,12 +8,12 @@ const {
8
8
  getCityName,
9
9
  displayResult,
10
10
  getData,
11
- parsePrayerTimesFromResponse
11
+ parsePrayerTimesFromResponse,
12
12
  } = require("./utils.js");
13
- process.env.NODE_TLS_REJECT_UNAUTHORIZED="0"
13
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
14
14
 
15
15
  // Logging functioin
16
- const green = msg => console.log(chalk.green(msg));
16
+ const green = (msg) => console.log(chalk.green(msg));
17
17
 
18
18
  // Project's data
19
19
  const { BANNER, LOCAL_STORAGE_PATH } = require("./constants");
@@ -31,7 +31,13 @@ const main = async () => {
31
31
  green(BANNER);
32
32
 
33
33
  const storageKey = `${cityName.toLowerCase()}_${new Date().toLocaleDateString()}`;
34
- const item = localStorage.getItem(storageKey);
34
+ let item = localStorage.getItem(storageKey);
35
+
36
+ // Disable localStorage for local development
37
+ if (process.env.NODE_ENV === "development") {
38
+ console.log("development mode: localStorage is disabled");
39
+ item = null;
40
+ }
35
41
  let prayers;
36
42
 
37
43
  if (item) {
@@ -43,12 +49,13 @@ const main = async () => {
43
49
  } catch (ex) {
44
50
  //TODO: Use a more descriptif error message
45
51
  console.error("Something went wrong!");
46
- console.log(ex)
52
+ console.log(ex);
47
53
  // console.log(ex);
48
54
  return;
49
55
  }
50
56
  }
51
57
 
58
+ console.clear();
52
59
  displayResult(prayers, cityName);
53
60
  };
54
61
 
package/constants.js CHANGED
@@ -1,5 +1,4 @@
1
- const API_URL =
2
- "http://www.habous.gov.ma/prieres/horaire-api.php";
1
+ const API_URL = "http://www.habous.gov.ma/prieres/horaire-api.php";
3
2
 
4
3
  const BANNER = ``;
5
4
 
@@ -15,5 +14,5 @@ module.exports = {
15
14
  BANNER,
16
15
  NOT_FOUND_ERROR,
17
16
  DEFAULT_CITY: "Marrakech",
18
- LOCAL_STORAGE_PATH: "./storage"
17
+ LOCAL_STORAGE_PATH: "./storage",
19
18
  };
package/data/prayers.json CHANGED
@@ -1,20 +1,20 @@
1
1
  [
2
- {
3
- "Prayer": "Fajr"
4
- },
5
- {
6
- "Prayer": "Chorouq"
7
- },
8
- {
9
- "Prayer": "Dhuhr"
10
- },
11
- {
12
- "Prayer": "Asr"
13
- },
14
- {
15
- "Prayer": "Maghrib"
16
- },
17
- {
18
- "Prayer": "Ishae"
19
- }
2
+ {
3
+ "prayer": "Fajr"
4
+ },
5
+ {
6
+ "prayer": "Chorouq"
7
+ },
8
+ {
9
+ "prayer": "Dhuhr"
10
+ },
11
+ {
12
+ "prayer": "Asr"
13
+ },
14
+ {
15
+ "prayer": "Maghrib"
16
+ },
17
+ {
18
+ "prayer": "Ishae"
19
+ }
20
20
  ]
package/package.json CHANGED
@@ -1,23 +1,20 @@
1
1
  {
2
2
  "name": "salat",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "description": "Daily Moroccan prayers time, right in your console, at the tip of your fingers",
5
- "homepage": "https://github.com/Kafiil/salat",
5
+ "homepage": "https://github.com/kafiln/salat-cli",
6
6
  "main": "app.js",
7
7
  "bin": {
8
8
  "salat": "app.js"
9
9
  },
10
10
  "dependencies": {
11
- "axios": ">=0.19.0",
12
11
  "chalk": "^2.4.2",
13
- "jsdom": "^14.0.0",
12
+ "jsdom": "^24.0.0",
13
+ "node-fetch": "^2.7.0",
14
14
  "node-localstorage": "^1.3.1"
15
15
  },
16
- "devDependencies": {
17
- "nodemon": "^2.0.2"
18
- },
19
16
  "scripts": {
20
- "dev": "nodemon app.js",
17
+ "dev": "env NODE_ENV=development nodemon app.js",
21
18
  "start": "node app"
22
19
  },
23
20
  "keywords": [
@@ -30,5 +27,8 @@
30
27
  "name": "Kafil NASDAMI",
31
28
  "email": "kafil.nasdami@gmail.com"
32
29
  },
33
- "license": "MIT"
34
- }
30
+ "license": "MIT",
31
+ "devDependencies": {
32
+ "nodemon": "^3.1.0"
33
+ }
34
+ }
package/utils.js CHANGED
@@ -1,12 +1,13 @@
1
- const axios = require("axios");
2
1
  const { JSDOM } = require("jsdom");
3
2
 
4
3
  const prayers = require("./data/prayers.json");
5
4
  const chalk = require("chalk");
6
5
  const { API_URL, DEFAULT_CITY } = require("./constants");
7
6
  const { NOT_FOUND_ERROR } = require("./constants");
7
+ const fetch = require("node-fetch");
8
+ const cheerio = require("cheerio");
8
9
 
9
- const error = msg => console.log(chalk.red(msg));
10
+ const error = (msg) => console.log(chalk.red(msg));
10
11
 
11
12
  const getCityId = (arg, cities) => {
12
13
  const parsed = parseInt(arg);
@@ -23,57 +24,58 @@ module.exports.getCityName = (arg, cities) => {
23
24
  const index = getCityIndex(arg, cities);
24
25
  if (index == -1) {
25
26
  error(NOT_FOUND_ERROR);
27
+ return DEFAULT_CITY;
26
28
  }
27
- return index == -1 ? DEFAULT_CITY : arg;
29
+ return arg;
28
30
  };
29
31
 
30
32
  const getCityIndex = (city, cities) =>
31
- cities.map(e => e.name.toLowerCase()).indexOf(city.toLowerCase());
33
+ cities.map((e) => e.name.toLowerCase()).indexOf(city.toLowerCase());
32
34
 
33
- module.exports.getData = async cityId =>
34
- await axios.get(`${API_URL}?ville=${cityId}`);
35
+ module.exports.getData = async (cityId) => {
36
+ const response = await fetch(`${API_URL}?ville=${cityId}`);
37
+ return await response.text();
38
+ };
35
39
 
36
- module.exports.parsePrayerTimesFromResponse = response => {
37
- const dom = new JSDOM(`${response.data}`);
40
+ module.exports.parsePrayerTimesFromResponse = (response) => {
41
+ const dom = new JSDOM(response);
38
42
  const tds = dom.window.document.getElementsByTagName("td");
39
43
 
40
44
  let j = 0;
41
- for (let i = 0; i < tds.length; i++) {
42
- if (i % 2) {
43
- prayers[j].Time = tds[i].textContent.trim();
44
- j++;
45
- }
45
+ for (let i = 1; i < tds.length; i += 2) {
46
+ prayers[j].time = tds[i].textContent.trim();
47
+ j++;
46
48
  }
49
+
47
50
  // Transorm array to object and return it
48
- return prayers.reduce((acc, { Prayer, Time }) => {
49
- acc[Prayer] = Time;
51
+ return prayers.reduce((acc, { prayer, time }) => {
52
+ acc[prayer] = time;
50
53
  return acc;
51
54
  }, {});
52
-
53
55
  };
54
56
 
55
57
  function tConv24(time24) {
56
- var ts = time24;
57
- var H = +ts.substr(0, 2);
58
- var h = (H % 12) || 12;
59
- h = (h < 10)?("0"+h):h; // leading 0 at the left for 1 digit hours
60
- var ampm = H < 12 ? ` ${chalk.hex('#a6c9de').visible('AM')}` : ` ${chalk.hex('#debfa6').visible('PM')}`;
61
- ts = h + ts.substr(2, 3) + ampm;
62
- return ts;
63
- };
58
+ const [hours, minutes] = time24.split(":");
59
+ const hour = Number(hours);
60
+ const formattedHour = hour % 12 || 12;
61
+ const formattedHourWithZero = (formattedHour + "").padStart(2, "0");
62
+ const formattedMinutes = minutes.padStart(2, "0");
63
+ const formattedTime = `${formattedHourWithZero}:${formattedMinutes}`;
64
+ const ampm = hour < 12 ? "AM" : "PM";
65
+ return `${formattedTime} ${ampm}`;
66
+ }
64
67
 
65
68
  module.exports.displayResult = (prayers, city) => {
69
+ if (!prayers) return;
66
70
  if (prayers) {
67
- console.log(
68
- ` 🧭 ${city}, Morocco\n\n 📆 ${new Date().toDateString()}`
69
- );
70
- console.log(`
71
- ◽ ${chalk.cyan('Fajr')} --> ${chalk.green(tConv24(prayers.Fajr))}
72
- ◽ ${chalk.cyan('Chorouq')} --> ${chalk.green(tConv24(prayers.Chorouq))}
73
- ◽ ${chalk.cyan('Dhuhr')} --> ${chalk.green(tConv24(prayers.Dhuhr))}
74
- ◽ ${chalk.cyan('Asr')} --> ${chalk.green(tConv24(prayers.Asr))}
75
- ◽ ${chalk.cyan('Maghrib')} --> ${chalk.green(tConv24(prayers.Maghrib))}
76
- ◽ ${chalk.cyan('Ishae')} --> ${chalk.green(tConv24(prayers.Ishae))}`
77
- );
71
+ console.log(` 🧭 ${city}, Morocco\n\n 📆 ${new Date().toDateString()}\n`);
72
+ Object.keys(prayers).forEach((key) => {
73
+ console.log(
74
+ ` ${chalk.cyan(key.padEnd(7, " "))} --> ${chalk.green(
75
+ tConv24(prayers[key])
76
+ )}`
77
+ );
78
+ });
79
+ console.log("\n");
78
80
  }
79
81
  };