-
Notifications
You must be signed in to change notification settings - Fork 2
/
words.js
62 lines (48 loc) · 1.35 KB
/
words.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
var hash = require('./hash')
var hash3 = hash.hash3
var conj = require('./utils/conj')
var fit = require('./utils/fit')
var defaults = require('./utils/defaults')
var word = require('./word')
var DEFAULT_MIN_WORDS = 2
var DEFAULT_MAX_WORDS = 3
var DEFAULT_CAPITALIZE = 'first'
function words(input, opts) {
opts = opts || 0
var capitalize = defaults(opts.capitalize, DEFAULT_CAPITALIZE)
var shouldCapitalizeAll = capitalize === true || capitalize === 'all'
var shouldCapitalizeFirst = shouldCapitalizeAll || capitalize === 'first'
var min = defaults(opts.min, DEFAULT_MIN_WORDS)
var max = defaults(opts.max, DEFAULT_MAX_WORDS)
var id = hash(input)
var n = fit(id, min, max)
var i = 0
id = hash3(id, 'words', i)
var firstOpts = conj(opts, {
capitalize: shouldCapitalizeFirst
})
var restOpts = conj(opts, {
capitalize: shouldCapitalizeAll
})
var result = word(id, firstOpts)
var next = result
var prev
while (++i < n) {
prev = next
do {
id = hash3(id, 'words', i)
next = word(id, restOpts)
} while (next.length === prev.length)
result += ' ' + next
}
return result
}
words.options = function wordsOptions(opts) {
var base = this
wordsFn.options = words.options
return wordsFn
function wordsFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}
module.exports = words