-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathrandomizer.js
49 lines (42 loc) · 1.24 KB
/
randomizer.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
"use strict"
var loremIpsum = require('lorem-ipsum')
function randomArrayOf(minCells, maxCells, callback) {
if ('function' === typeof maxCells && 'undefined' === typeof callback) {
callback = maxCells
maxCells = minCells
}
return Array.apply(null, { length: randomInt(minCells, maxCells) }).map(callback)
}
function randomArrayOfInts(maxCells, minInt, maxInt) {
return randomArrayOf(0, maxCells, function() {
return randomInt(minInt, maxInt)
})
}
function randomArrayOfLorems(minCells, maxCells) {
var loremOptions = Array.prototype.slice(arguments, 2)
return randomArrayOf(minCells, maxCells, function() {
return loremIpsum.apply(loremIpsum, loremOptions)
})
}
function randomInt(min, max) {
if (min === max) return max
return Math.floor((Math.random() * (max - min + 1)) + min)
}
function randomWords(count, options) {
options = options || {}
var result = loremIpsum().split(' ').slice(0, count)
if (options.capitalized) {
result = result.map(function(word) {
word[0] = word[0].toUpperCase()
return word
})
}
return result.join(' ')
}
module.exports = {
int: randomInt,
arrayOf: randomArrayOf,
arrayOfInts: randomArrayOfInts,
arrayOfLorems: randomArrayOfLorems,
words: randomWords
}