-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
156 lines (141 loc) · 3.95 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
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
'use strict'
const quotes = require('./quotes')
const quotesLen = quotes.length
/**
* An anime quote Object.
* @typedef {Object} AnimeQuotesObject
* @property {string} quote Quote.
* @property {string} anime Anime name.
* @property {number} id Quote ID.
* @property {string} name Character name.
* @property {boolean} success Object status.
*/
/**
* Returns empty object.
* @returns {AnimeQuotesObject}
*/
function _getEmptyObject () {
return {
quote: '',
anime: '',
id: '',
name: '',
success: false
}
}
/*
* Create a quote object from the input quote.
* @param {AnimeQuotesObject} quote
* @returns {AnimeQuotesObject}
*/
function _createQuoteObject (quote) {
let quoteObject = _getEmptyObject()
quoteObject = quote
quoteObject.success = true
return quoteObject
}
/**
* Get a random number between min and max. Min and max both inclusive.
* @param {number} min
* @param {number} max
* @returns {number} Random number.
*/
function _randomIntFromInterval (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
/**
* Search data using the input key and value.
* @param {('quote'|'anime'|'name')} inputKey
* @param {string} inputVal
* @param {boolean} substringSearch
* @returns {AnimeQuotesObject[]}
*/
function _searchData (inputKey, inputVal, substringSearch) {
const result = []
inputVal = inputVal.toLowerCase()
for (let i = 0; i < quotes.length; i++) {
const currentResult = quotes[i][inputKey].toLowerCase()
if ((substringSearch === false && currentResult === inputVal) ||
(substringSearch === true && currentResult.includes(inputVal))) {
result.push(_createQuoteObject(quotes[i]))
}
}
return result
}
/**
* Get a random quote from the given quotes array.
* @param {AnimeQuotesObject[]} quotesArray
* @returns {AnimeQuotesObject}
*/
function _getRandomQuote (quotesArray) {
let quote = _getEmptyObject()
const len = quotesArray.length
if (len > 0) {
quote = _createQuoteObject(quotesArray[_randomIntFromInterval(0, len - 1)])
}
return quote
}
/**
* Get a random quote.
* @returns {AnimeQuotesObject}
*/
function randomQuote () {
return _getRandomQuote(quotes)
}
/**
* Get a random quote from the input anime.
* @param {string} anime Anime name.
* @param {boolean} [substringSearch=false] Partial matching for output.
* @returns {AnimeQuotesObject}
*/
function getRandomQuoteByAnime (anime, substringSearch = false) {
const result = getQuotesByAnime(anime, substringSearch)
return _getRandomQuote(result)
}
/**
* Get a random quote from the input character.
* @param {string} character Anime character's name.
* @param {boolean} [substringSearch=false] Partial matching for output.
* @returns {AnimeQuotesObject}
*/
function getRandomQuoteByCharacter (character, substringSearch = false) {
const result = getQuotesByCharacter(character, substringSearch)
return _getRandomQuote(result)
}
/**
* Get Quote by ID.
* @param {number} id
*/
function getQuote (id) {
let quote = _getEmptyObject()
if (id > 0 && id <= quotesLen) {
quote = _createQuoteObject(quotes[quotesLen - id])
}
return quote
}
/**
* Get quotes by Anime.
* @param {string} anime Anime name.
* @param {boolean} [substringSearch=false] Partial matching for output.
* @returns {AnimeQuotesObject[]}
*/
function getQuotesByAnime (anime, substringSearch = false) {
return _searchData('anime', anime, substringSearch)
}
/**
* Get quotes by Character.
* @param {string} character Anime character's name.
* @param {boolean} [substringSearch=false] Partial matching for output.
* @returns {AnimeQuotesObject[]}
*/
function getQuotesByCharacter (character, substringSearch = false) {
return _searchData('name', character, substringSearch)
}
module.exports = {
randomQuote: randomQuote,
getQuote: getQuote,
getQuotesByAnime: getQuotesByAnime,
getQuotesByCharacter: getQuotesByCharacter,
getRandomQuoteByAnime: getRandomQuoteByAnime,
getRandomQuoteByCharacter: getRandomQuoteByCharacter
}