-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotesRepository.js
163 lines (118 loc) · 4.42 KB
/
quotesRepository.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
var datas = require('./datas.json');
try {
module.exports = {
getRandom: function getRandom(numberOfQuotes) {
if (numberOfQuotes > 1)
{
var quotesNb = datas.quotes.length;
var limit = numberOfQuotes > quotesNb ? quotesNb : numberOfQuotes;
var out = new Array(limit);
var quotesUsed = new Array(limit);
var quoteAlreadyUsed;
for (var i = 0; i < limit; i++)
{
var randomQuote = randomize(datas.quotes);
quoteAlreadyUsed = quotesUsed.indexOf(randomQuote) > -1;
while (quoteAlreadyUsed) {
randomQuote = randomize(datas.quotes);
quoteAlreadyUsed = quotesUsed.indexOf(randomQuote) > -1;
}
quotesUsed[i] = randomQuote;
out[i] = formatQuote(randomQuote);
}
}
else
{
var randomQuote = randomize(datas.quotes);
var out = formatQuote(randomQuote);
}
return out;
},
getByAuthor: function getByAuthor(name, numberOfQuotes) {
var out = null;
if (typeof datas.characters[name] === 'undefined')
return out;
var quotesList = new Array();
for (var i in datas.quotes) {
if (datas.quotes[i].character == name) {
quotesList.push(datas.quotes[i]);
}
}
if (quotesList.length > 0) {
if (numberOfQuotes > 1)
{
var quotesNb = quotesList.length;
var limit = numberOfQuotes > quotesNb ? quotesNb : numberOfQuotes;
var out = new Array(limit);
var quotesUsed = new Array(limit);
var quoteAlreadyUsed;
for (var i = 0; i < limit; i++)
{
var randomQuote = randomize(quotesList);
quoteAlreadyUsed = quotesUsed.indexOf(randomQuote) > -1;
while (quoteAlreadyUsed) {
randomQuote = randomize(quotesList);
quoteAlreadyUsed = quotesUsed.indexOf(randomQuote) > -1;
}
quotesUsed[i] = randomQuote;
out[i] = formatQuote(randomQuote);
}
}
else
{
var randomQuote = randomize(quotesList);
var out = formatQuote(randomQuote);
}
}
return out;
},
getCharacters: function getCharacters(name) {
var out = new Array();
var charactersList = datas.characters;
if (name !== null) {
if (typeof datas.characters[name] === 'undefined')
return out;
var charactersList = new Array();
charactersList[name] = datas.characters[name];
}
var quotesList = new Array();
for (var q in datas.quotes) {
if (!(datas.quotes[q].character in quotesList))
quotesList[datas.quotes[q].character] = new Array();
quotesList[datas.quotes[q].character].push(datas.quotes[q].sentence);
}
for (var i in charactersList) {
var character_quotes = null;
if (i in quotesList && quotesList[i].length > 0) {
character_quotes = quotesList[i];
}
var character = {
name: charactersList[i].name,
slug: i,
quotes: character_quotes
};
out.push(character);
}
return out;
}
};
} catch (e) {
console.log(e);
}
// Randomize quote
function randomize(quotes)
{
return quotes[Math.floor(Math.random() * quotes.length)];
}
// Format quote result
function formatQuote(randomQuote)
{
var quote = {};
quote.sentence = randomQuote.sentence;
quote.character = {
name: datas.characters[randomQuote.character].name,
slug: randomQuote.character
};
return quote;
}