-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathword.js
More file actions
99 lines (78 loc) · 2.21 KB
/
word.js
File metadata and controls
99 lines (78 loc) · 2.21 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var hash = require('./hash')
var conj = require('./utils/conj')
var flip = require('./utils/flip')
var unicodify = require('./utils/unicodify')
var defaults = require('./utils/defaults')
var SYLLABLES = require('./data/lorem.json')
var INT_RANGE = 4294967296
var DEFAULT_MIN_SYLLABLES = 1
var DEFAULT_MAX_SYLLABLES = 4
var DEFAULT_CAPITALIZE = true
var DEFAULT_UNICODE = 0
function word(input, opts) {
opts = opts || 0
var shouldCapitalize = defaults(opts.capitalize, DEFAULT_CAPITALIZE)
var minSyllables = defaults(opts.minSyllables, DEFAULT_MIN_SYLLABLES)
var maxSyllables = defaults(opts.maxSyllables, DEFAULT_MAX_SYLLABLES)
var pUnicode = defaults(opts.unicode, DEFAULT_UNICODE)
var id = hash.hash2(input, 'word')
id = hash.sequenceNext(id)
var result
var prev
var next
var probability
var candidates
var candidate
var candidatesLen
var candidateSyllable
var candidateProbability
var nextProbabilityMark
var syllableCount
var candidateIndex
do {
result = ''
prev = ''
syllableCount = 0
do {
id = hash.sequenceNext(id)
probability = id / INT_RANGE
nextProbabilityMark = 0
candidates = SYLLABLES[prev]
candidatesLen = candidates.length
candidateIndex = -1
next = null
while (++candidateIndex < candidatesLen && next == null) {
candidate = candidates[candidateIndex]
candidateSyllable = candidate[0]
candidateProbability = candidate[1]
nextProbabilityMark += candidateProbability
if (probability <= nextProbabilityMark) {
next = candidateSyllable
}
}
syllableCount++
result += next
prev = next
} while (syllableCount < maxSyllables && next !== '')
} while (syllableCount < minSyllables)
id = hash.sequenceNext(id)
if (flip(id, pUnicode)) {
result = unicodify(id, result)
}
if (shouldCapitalize) {
result = capitalize(result)
}
return result
}
word.options = function wordOptions(opts) {
var base = this
wordFn.options = word.options
return wordFn
function wordFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}
module.exports = word
function capitalize(s) {
return s[0].toUpperCase() + s.slice(1)
}