-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
92 lines (87 loc) · 2.71 KB
/
utils.js
File metadata and controls
92 lines (87 loc) · 2.71 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const utils = {
capitalizeFirstLetter: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
parseDefinitions: function (result) {
try {
let ret = '';
result.forEach(function (item, index) {
ret += (index + 1) + `. ${item.text}\n`
});
return ret;
}catch (e) {
return "Sorry! Definition not found."
}
},
parseExamples: function (result) {
try {
let ret = '';
result.examples.forEach(function (item, index) {
ret += (index + 1) + `. ${item.text}\n`
});
return ret;
}catch (e) {
return "Sorry! Example not found."
}
},
parseSynonym: function (result) {
let ret = '';
try {
result.forEach(function (item) {
if (item.relationshipType == 'synonym') {
item.words.forEach(function (word, index) {
ret += (index + 1) + `. ${utils.capitalizeFirstLetter(word)}\n`
})
}
});
if (ret == '') {
ret = "Sorry! Synonym not found\n"
}
} catch (e) {
ret = "Sorry! Synonym not found\n"
}
return ret;
},
parseAntonym: function (result) {
let ret = '';
try {
result.forEach(function (item) {
if (item.relationshipType == 'antonym') {
item.words.forEach(function (word, index) {
ret += (index + 1) + `. ${utils.capitalizeFirstLetter(word)}\n`
})
}
});
if (ret == '') {
ret = "Sorry! Antonym not found\n"
}
} catch (e) {
ret = "Sorry! Antonym not found\n"
}
return ret;
},
shuffle: function (array) {
if(array.length <2){
return array;
}
const originalArray = [...array];
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
if(originalArray == array){
return utils.shuffle(array);
}
else {
return array;
}
}
}
module.exports = utils;