-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
57 lines (48 loc) · 2.05 KB
/
main.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
const enFruits = require('./langs/en.json')
const esFruits = require('./langs/es.json')
const jaFruits = require('./langs/ja.json')
const ptFruits = require('./langs/pt.json')
const nlFruits = require('./langs/nl.json')
const frFruits = require('./langs/fr.json')
const deFruits = require('./langs/de.json')
const csFruits = require('./langs/cs.json')
const bnFruits = require('./langs/bn.json')
const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max))
const filterMaxWords = (fruits, maxWords) => {
if (!Number.isInteger(maxWords))
throw new Error('The value of maxWords is incorrect.')
// If `maxWords` is 1, no space is allowed in the string.
const allowSpaceCount = maxWords - 1
const filteredFruits = fruits.filter((f) => {
return f.split(' ').length - 1 <= allowSpaceCount
})
return filteredFruits
}
/**
* Get Random Fruit Name
* @param {string} language - choose language of random fruit, default is english: 'en', function ue [ISO-639-1 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
* @returns {string} return a string with fruits name, example: 'Apple'
*/
const getRandomFruitsName = (language = 'en', option = {}) => {
if (
!['en', 'es', 'ja', 'pt', 'nl', 'fr', 'de', 'cs', 'bn'].includes(language)
) {
throw new Error('Selected language is invalid.')
}
const optionKeys = Object.keys(option)
if (!!optionKeys.length && !optionKeys.includes('maxWords')) {
throw new Error('Specified option is invalid.')
}
let fruits = enFruits.fruits
if (language === 'es') fruits = esFruits.fruits
if (language === 'ja') fruits = jaFruits.fruits
if (language === 'pt') fruits = ptFruits.fruits
if (language === 'nl') fruits = nlFruits.fruits
if (language === 'fr') fruits = frFruits.fruits
if (language === 'de') fruits = deFruits.fruits
if (language === 'cs') fruits = csFruits.fruits
if (language === 'bn') fruits = bnFruits.fruits
if ('maxWords' in option) fruits = filterMaxWords(fruits, option.maxWords)
return fruits[getRandomInt(fruits.length)]
}
module.exports = getRandomFruitsName