Skip to content

Commit 0ec2af3

Browse files
authored
Merge pull request #74 from mskian/main
2 parents 3847f7e + 1e2da22 commit 0ec2af3

File tree

10 files changed

+110
-100
lines changed

10 files changed

+110
-100
lines changed

.github/workflows/buildtest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/[email protected]
1414
- uses: actions/[email protected]
1515
with:
16-
node-version: '14.x'
16+
node-version: '16.x'
1717
- name: use node
1818
run: |
1919
npm install

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
const express = require('express');
1+
import express from "express";
22
const app = express();
33

4-
// Importing the routes
5-
const home = require('./routes/home');
6-
const en = require('./routes/en');
7-
const dutch = require('./routes/dutch');
4+
import home from './routes/home.js';
5+
import en from './routes/en.js';
6+
import dutch from './routes/dutch.js';
87

98
app.use('/', home);
10-
app.use('/word', en);
119
app.use('/word/dutch', dutch);
10+
app.use('/word', en);
1211

13-
// start app on localhost port 3000
1412
var port = process.env.PORT || 3000;
1513
app.listen(port, function() {
1614
console.log('listening on port ' + port);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
"axios": "^0.27.2",
99
"cheerio": "^1.0.0-rc.12",
1010
"express": "^4.18.1",
11-
"node-pronounce": "^0.0.3",
11+
"node-pronounce": "^0.0.4",
1212
"random-useragent": "^0.5.0"
1313
},
1414
"scripts": {
1515
"test": "echo \"Error: no test specified\" && exit 1",
1616
"start": "node index.js",
1717
"action": "node test.js",
18-
"dev": "nodemon index.js"
18+
"dev": "nodemon index.js --ext '*'"
1919
},
20+
"type": "module",
2021
"keywords": [
2122
"Word",
2223
"of",

routes/dutch.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
const express = require("express");
1+
import express from "express";
22
const router = express.Router();
3-
const { pronounce } = require("node-pronounce");
4-
const dutchWordsData = require("../data/dutch/words.json"); //Import Dutchs
5-
const { randomArrayItem } = require("../utils/randomArrayItem");
3+
import { pronounce } from "node-pronounce";
4+
import { readFileSync } from "fs";
5+
import { randomArrayItem } from "../utils/randomArrayItem.js";
66
var dutchRandomWord = [];
7+
const dutchWordsData = JSON.parse(readFileSync(new URL("../data/dutch/words.json", import.meta.url)));
78

89
router.get("/", function (req, res) {
910
res.header("Access-Control-Allow-Origin", "*");
10-
res.header(
11-
"Access-Control-Allow-Headers",
12-
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
13-
);
11+
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
1412
res.header("Access-Control-Allow-Methods", "GET");
1513
res.header("X-Frame-Options", "DENY");
1614
res.header("X-XSS-Protection", "1; mode=block");
@@ -40,4 +38,4 @@ router.get("/", function (req, res) {
4038
res.send(JSON.stringify(dutchRandomWord, null, 2));
4139
});
4240

43-
module.exports = router;
41+
export default router;

routes/en.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
const express = require("express");
1+
import express from "express";
22
const router = express.Router();
3-
const axios = require("axios");
4-
const cheerio = require("cheerio");
5-
const { pronounce } = require("node-pronounce");
6-
const randomUseragent = require("random-useragent");
3+
import axios from "axios";
4+
import * as cheerio from "cheerio";
5+
import { pronounce } from "node-pronounce";
6+
import randomUseragent from "random-useragent";
77
const rua = randomUseragent.getRandom();
88
var wordOfDay = [];
99
const baseUrl = 'https://randomword.com';
1010

1111
router.get("/", function (req, res) {
1212
res.header("Access-Control-Allow-Origin", "*");
13-
res.header(
14-
"Access-Control-Allow-Headers",
15-
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
16-
);
13+
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
1714
res.header("Access-Control-Allow-Methods", "GET");
1815
res.header("X-Frame-Options", "DENY");
1916
res.header("X-XSS-Protection", "1; mode=block");
@@ -29,7 +26,7 @@ router.get("/", function (req, res) {
2926
},
3027
})
3128
.then(function (response) {
32-
$ = cheerio.load(response.data);
29+
const $ = cheerio.load(response.data);
3330
if (wordOfDay.length > 0) {
3431
wordOfDay = [];
3532
}
@@ -74,10 +71,7 @@ router.get("/", function (req, res) {
7471

7572
router.get("/:pos", function (req, res) {
7673
res.header("Access-Control-Allow-Origin", "*");
77-
res.header(
78-
"Access-Control-Allow-Headers",
79-
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
80-
);
74+
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
8175
res.header("Access-Control-Allow-Methods", "GET");
8276
res.header("X-Frame-Options", "DENY");
8377
res.header("X-XSS-Protection", "1; mode=block");
@@ -95,7 +89,7 @@ router.get("/:pos", function (req, res) {
9589
},
9690
})
9791
.then(function (response) {
98-
$ = cheerio.load(response.data);
92+
const $ = cheerio.load(response.data);
9993
if (wordOfDay.length > 0) {
10094
wordOfDay = [];
10195
}
@@ -138,4 +132,4 @@ router.get("/:pos", function (req, res) {
138132
});
139133
});
140134

141-
module.exports = router;
135+
export default router;

routes/home.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
const express = require("express");
1+
import express from "express";
22
const router = express.Router();
33

44
router.get("/", function (req, res) {
55
res.header("Access-Control-Allow-Origin", "*");
6-
res.header(
7-
"Access-Control-Allow-Headers",
8-
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
9-
);
6+
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
107
res.header("Access-Control-Allow-Methods", "GET");
118
res.header("X-Frame-Options", "DENY");
129
res.header("X-XSS-Protection", "1; mode=block");
1310
res.header("X-Content-Type-Options", "nosniff");
1411
res.header("Strict-Transport-Security", "max-age=63072000");
15-
1612
res.json("Random Words API");
1713
});
1814

19-
module.exports = router;
15+
export default router;

test.js

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
1-
const cheerio = require('cheerio');
2-
const nlp = require('compromise');
3-
const nlpPronounce = require('compromise-pronounce');
4-
nlp.extend(nlpPronounce);
5-
const randomUseragent = require('random-useragent');
6-
const axios = require('axios');
1+
import axios from "axios";
2+
import * as cheerio from "cheerio";
3+
import { pronounce } from "node-pronounce";
4+
import randomUseragent from "random-useragent";
75
const rua = randomUseragent.getRandom();
86
var wordOfDay = [];
7+
const baseUrl = 'https://randomword.com';
98

10-
axios({
11-
method: 'GET',
12-
url: 'https://randomword.com/',
9+
axios({
10+
method: "GET",
11+
url: baseUrl,
1312
headers: {
14-
'User-Agent': rua
15-
}
16-
}).then(function(response) {
17-
18-
$ = cheerio.load(response.data);
19-
20-
if (wordOfDay.length > 0) {
13+
"User-Agent": rua,
14+
},
15+
})
16+
.then(function (response) {
17+
const $ = cheerio.load(response.data);
18+
if (wordOfDay.length > 0) {
2119
wordOfDay = [];
22-
}
20+
}
2321

24-
var post = $('.section #shared_section');
25-
var word = post.find('#random_word').eq(0).text().replace('\r\n\t\t\t\t\t', '').replace('\r\n\t\t\t\t', '').replace('\n\t\t\t\t\t', '').replace('\n\t\t\t\t', '');
26-
var definition = post.find('#random_word_definition').eq(0).text().replace('\n', '');
27-
var pronounceword = word;
28-
var doc = nlp(pronounceword);
29-
var pronounces = doc.terms().pronounce().map(o => o.pronounce).toString();
30-
var pronounce = pronounces.replace(",", "");
31-
wordOfDay.push({
22+
var post = $(".section #shared_section");
23+
var word = post
24+
.find("#random_word")
25+
.eq(0)
26+
.text()
27+
.replace("\r\n\t\t\t\t\t", "")
28+
.replace("\r\n\t\t\t\t", "")
29+
.replace("\n\t\t\t\t\t", "")
30+
.replace("\n\t\t\t\t", "");
31+
var definition = post
32+
.find("#random_word_definition")
33+
.eq(0)
34+
.text()
35+
.replace("\n", "");
36+
var pronounceword = pronounce(word).replace(",", "");
37+
38+
wordOfDay.push({
3239
word: decodeURI(word.charAt(0).toUpperCase() + word.slice(1)),
33-
definition: decodeURI(definition.charAt(0).toUpperCase() + definition.slice(1)),
34-
pronunciation: decodeURI(pronounce.charAt(0).toUpperCase() + pronounce.slice(1))
40+
definition: decodeURI(
41+
definition.charAt(0).toUpperCase() + definition.slice(1)
42+
),
43+
pronunciation: decodeURI(
44+
pronounceword.charAt(0).toUpperCase() + pronounceword.slice(1)
45+
),
46+
});
47+
console.log(JSON.stringify(wordOfDay, null, 2));
3548
})
36-
console.log("User-Agent:", rua);
37-
console.log(wordOfDay);
38-
39-
}).catch(function(error) {
40-
if (!error.response) {
41-
console.log('API URL is Missing');
42-
} else {
43-
console.log('Something Went Wrong - Enter the Correct API URL');;
44-
}
45-
});
49+
.catch(function (error) {
50+
if (!error.response) {
51+
console.log("API URL is Missing");
52+
} else {
53+
console.log("Something Went Wrong - Enter the Correct API URL");
54+
}
55+
});

utils/randomArrayItem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ function randomArrayItem(items) {
22
return items[Math.floor(Math.random() * items.length)];
33
}
44

5-
module.exports.randomArrayItem = randomArrayItem;
5+
const _randomArrayItem = randomArrayItem;
6+
export { _randomArrayItem as randomArrayItem };

utils/randomNum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ function getRandomIntInclusive(min, max) {
44
return Math.floor(Math.random() * (max - min + 1) + min);
55
}
66

7-
module.exports.randNum = getRandomIntInclusive;
7+
export const randNum = getRandomIntInclusive;

yarn.lock

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,19 @@ combined-stream@^1.0.8:
249249
dependencies:
250250
delayed-stream "~1.0.0"
251251

252-
compromise-pronounce@^0.0.5:
253-
version "0.0.5"
254-
resolved "https://registry.yarnpkg.com/compromise-pronounce/-/compromise-pronounce-0.0.5.tgz#4200596e1a657ccd87f616bfc917338003858dc0"
255-
integrity sha512-cjLx6JvyjI7u3OGmXhakmr8WwPhR55uukyMgeIbj2tgz3WjZoGT55DygKHNSjx32MZN6sJJxtT1zSjgbWMDmtw==
252+
compromise-speech@^0.1.0:
253+
version "0.1.0"
254+
resolved "https://registry.yarnpkg.com/compromise-speech/-/compromise-speech-0.1.0.tgz#4ca319787ffec6c1372f3f8aa7f9956cffb0d9c8"
255+
integrity sha512-+2KS/8T3SH5Hcd6tsCBVfxrkdi7sbJfKAeXGYimQdKGgweDMt0FT03wVCDZ6EgLYxReftkRKp1pxLx2pQLj4Kg==
256256

257-
compromise@^13.11.4:
258-
version "13.11.4"
259-
resolved "https://registry.yarnpkg.com/compromise/-/compromise-13.11.4.tgz#3136e6d2c2cca822ba7cebd140c9f27f405b67f1"
260-
integrity sha512-nBITcNdqIHSVDDluaG6guyFFCSNXN+Hu87fU8VlhkE5Z0PwTZN1nro2O7a8JcUH88nB5EOzrxd9zKfXLSNFqcg==
257+
compromise@^14.4.0:
258+
version "14.4.0"
259+
resolved "https://registry.yarnpkg.com/compromise/-/compromise-14.4.0.tgz#779d07c90b41606bb4624942ec841ae8a8806e24"
260+
integrity sha512-09bovqZT1NX84o/ntjftaY2XMdnm9pLJxB8N7IgAPFwytRNSEevB0aBnVFSR85VyAr+Spcjy6bDk5BYXcKDWkg==
261261
dependencies:
262-
efrt-unpack "2.2.0"
262+
efrt "2.6.0"
263+
grad-school "0.0.5"
264+
suffix-thumb "4.0.2"
263265

264266
265267
version "0.0.1"
@@ -414,10 +416,10 @@ [email protected]:
414416
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
415417
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
416418

417-
efrt-unpack@2.2.0:
418-
version "2.2.0"
419-
resolved "https://registry.yarnpkg.com/efrt-unpack/-/efrt-unpack-2.2.0.tgz#b05dbec0fb8cb346a27840e00c969df9c72fee52"
420-
integrity sha512-9xUSSj7qcUxz+0r4X3+bwUNttEfGfK5AH+LVa1aTpqdAfrN5VhROYCfcF+up4hp5OL7IUKcZJJrzAGipQRDoiQ==
419+
efrt@2.6.0:
420+
version "2.6.0"
421+
resolved "https://registry.yarnpkg.com/efrt/-/efrt-2.6.0.tgz#27854df4b0f596bf7b4444b594a1a58cfad421ca"
422+
integrity sha512-uVeV82e9c/n/8/QyWVbbt4jvvbVOvgYwnKE59KpwJ1DD7AGXWytU9v32v5SuvxKNl9V9JaudsMPtztJNY+Batg==
421423

422424
emoji-regex@^8.0.0:
423425
version "8.0.0"
@@ -606,6 +608,11 @@ graceful-fs@^4.1.2:
606608
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.9.tgz#041b05df45755e587a24942279b9d113146e1c96"
607609
integrity sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==
608610

611+
612+
version "0.0.5"
613+
resolved "https://registry.yarnpkg.com/grad-school/-/grad-school-0.0.5.tgz#9e7b2084f3e137965c19b63ef0dfe49867194f43"
614+
integrity sha512-rXunEHF9M9EkMydTBux7+IryYXEZinRk6g8OBOGDBzo/qWJjhTxy86i5q7lQYpCLHN8Sqv1XX3OIOc7ka2gtvQ==
615+
609616
has-flag@^3.0.0:
610617
version "3.0.0"
611618
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
@@ -906,13 +913,13 @@ [email protected]:
906913
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
907914
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==
908915

909-
node-pronounce@^0.0.3:
910-
version "0.0.3"
911-
resolved "https://registry.yarnpkg.com/node-pronounce/-/node-pronounce-0.0.3.tgz#9555d1740966bad0436e212dee8d97a09c5e0faa"
912-
integrity sha512-nFKwpp8f5DjNe/jjfUGaY6bWCuRAh3hEfpVYS7ZfMA1GHJQuP1UKlu6CvSfPW1RmTs/8xgqPE8BmI2kdb4q9Ag==
916+
node-pronounce@^0.0.4:
917+
version "0.0.4"
918+
resolved "https://registry.yarnpkg.com/node-pronounce/-/node-pronounce-0.0.4.tgz#48134a579d32451a907692f9193624e60ad754c2"
919+
integrity sha512-rX8YDCHU2uXMVklgaZ2OsuEzpp9Vg4ms1qnn6qTTA1wrU596N5rJv8Q+62xvLPj70jPJ8S+K24XiTTprecj0rQ==
913920
dependencies:
914-
compromise "^13.11.4"
915-
compromise-pronounce "^0.0.5"
921+
compromise "^14.4.0"
922+
compromise-speech "^0.1.0"
916923

917924
nodemon@^2.0.18:
918925
version "2.0.18"
@@ -1233,6 +1240,11 @@ strip-json-comments@~2.0.1:
12331240
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
12341241
integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo=
12351242

1243+
1244+
version "4.0.2"
1245+
resolved "https://registry.yarnpkg.com/suffix-thumb/-/suffix-thumb-4.0.2.tgz#9303c1be8252bf79d9c57e5737d7d849d1f6abc9"
1246+
integrity sha512-CCvCAr7JyeQoO+/lyq3P2foZI/xJz5kpG3L6kg2CaOf9K2/gaM9ixy/JTuRwjEK38l306zE7vl3JoOZYmLQLlA==
1247+
12361248
supports-color@^5.5.0:
12371249
version "5.5.0"
12381250
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"

0 commit comments

Comments
 (0)