-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (40 loc) · 1.11 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
const animals = require('./words/animals.js')
const colours = require('./words/colours.js')
const adjectives = require('./words/adjectives.js')
const animalLen = animals.length
const colourLen = colours.length
const adjectiveLen = adjectives.length
const defaults = {
separator : '-',
format : 'lower',
}
function name(opts) {
opts = opts || defaults
opts = {
separator : opts.separator || defaults.separator,
format : opts.format || defaults.format,
}
let parts = []
parts.push(adjectives[Math.floor(Math.random() * adjectiveLen)])
parts.push(colours[Math.floor(Math.random() * colourLen)])
parts.push(animals[Math.floor(Math.random() * animalLen)])
let fn = function(){}
if ( opts.format === 'lower' ) {
fn = function(s) {
return s.toLowerCase()
}
}
if ( opts.format === 'upper' ) {
fn = function(s) {
return s.toUpperCase()
}
}
if ( opts.format === 'title' ) {
fn = function(s) {
return s[0].toUpperCase() + s.slice(1).toLowerCase()
}
}
parts = parts.map(fn)
return parts.join(opts.separator)
}
module.exports = name