-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (36 loc) · 1.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const axios = require('axios');
const cheerio = require('cheerio');
const { MAL, URL } = require('./utils');
/**
* Fetches the meaning of a word from Olam dictionary.
*
* @param {string} query - The word to search.
* @returns {Array<Object>|boolean>} An array of word definitions or false if no definition is found.
*/
String.prototype.format = function(...args) {
return this.replace(/{}/g, () => args.shift());
};
module.exports = function (query) {
let lang = query.match(MAL) ? 'malayalam' : 'english';
let result = [];
return axios.get(URL.format(lang, encodeURIComponent(query)))
.then(({ data }) => {
let $ = cheerio.load(data);
let entries = $('.entry');
entries.each((index, entry) => {
let title = $(entry).find('.title').text().trim();
let definitions = $(entry).find('ol.defs');
definitions.each((i, def) => {
let partOfSpeech = $(def).find('.types').text().replace(/-/g, '').trim() || null;
$(def).find('.def').each((i, mean) => {
let meaning = $(mean).text().trim();
result.push({ title, partOfSpeech, meaning: meaning });
});
});
});
return result.length === 0 ? false : result;
})
.catch((error) => {
throw new Error(error);
});
};