-
Notifications
You must be signed in to change notification settings - Fork 2
/
sentence.js
53 lines (42 loc) · 1.33 KB
/
sentence.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
var hash = require('./hash')
var conj = require('./utils/conj')
var fit = require('./utils/fit')
var defaults = require('./utils/defaults')
var words = require('./words')
var DEFAULT_MIN_CLAUSES = 1
var DEFAULT_MAX_CLAUSES = 2
var DEFAULT_MIN_WORDS = 5
var DEFAULT_MAX_WORDS = 8
var DEFAULT_UNICODE = 0
function sentence(input, opts) {
opts = opts || 0
var minWords = defaults(opts.minWords, DEFAULT_MIN_WORDS)
var maxWords = defaults(opts.maxWords, DEFAULT_MAX_WORDS)
var minClauses = defaults(opts.minClauses, DEFAULT_MIN_CLAUSES)
var maxClauses = defaults(opts.maxClauses, DEFAULT_MAX_CLAUSES)
var unicode = defaults(opts.unicode, DEFAULT_UNICODE)
var ids = hash.sequence2(input, 'sentence')
var n = fit(ids.next().value, minClauses, maxClauses)
var i = 0
var firstOpts = conj(opts, {
capitalize: 'first',
min: minWords,
max: maxWords,
unicode: unicode
})
var restOpts = conj(firstOpts, { capitalize: false })
var result = words(ids.next().value, firstOpts)
while (++i < n) {
result += ', ' + words(ids.next().value, restOpts)
}
return result + '.'
}
sentence.options = function sentenceOptions(opts) {
var base = this
sentenceFn.options = sentence.options
return sentenceFn
function sentenceFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}
module.exports = sentence