-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(paragraph): Support paragraphs and sentences
- Loading branch information
Showing
8 changed files
with
271 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
module.exports = function fit(v, lo, hi) { | ||
lo = lo || 0 | ||
return hi == null ? v + lo : (v % (hi + 1 - lo)) + lo | ||
|
||
if (hi == null) { | ||
return v + lo | ||
} | ||
|
||
hi = Math.max(hi, lo) | ||
return (v % (hi + 1 - lo)) + lo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var hash = require('./hash') | ||
var conj = require('./internal/conj') | ||
var fit = require('./internal/fit') | ||
var defaults = require('./internal/defaults') | ||
var sentence = require('./sentence') | ||
|
||
var DEFAULT_MIN_SENTENCES = 4 | ||
var DEFAULT_MAX_SENTENCES = 5 | ||
|
||
function paragraph(input, opts) { | ||
opts = opts || 0 | ||
var minSentences = defaults(opts.minSentences, DEFAULT_MIN_SENTENCES) | ||
var maxSentences = defaults(opts.maxSentences, DEFAULT_MAX_SENTENCES) | ||
|
||
var id = hash(input) | ||
var n = fit(id, minSentences, maxSentences) | ||
var i = 0 | ||
id = hash([id, 'paragraph', i]) | ||
|
||
var result = sentence(id, opts) | ||
|
||
while (++i < n) { | ||
id = hash([id, 'paragraph', i]) | ||
result += ' ' + sentence(id, opts) | ||
} | ||
|
||
return result | ||
} | ||
|
||
paragraph.options = function paragraphOptions(opts) { | ||
var base = this | ||
paragraphFn.options = paragraph.options | ||
return paragraphFn | ||
|
||
function paragraphFn(input, overrides) { | ||
return base(input, conj(opts, overrides)) | ||
} | ||
} | ||
|
||
module.exports = paragraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var hash = require('./hash') | ||
var conj = require('./internal/conj') | ||
var fit = require('./internal/fit') | ||
var defaults = require('./internal/defaults') | ||
var words = require('./words') | ||
|
||
var DEFAULT_MIN_CLAUSES = 1 | ||
var DEFAULT_MAX_CLAUSES = 2 | ||
var DEFAULT_MIN_SYLLABLES = 1 | ||
var DEFAULT_MIN_WORDS = 5 | ||
var DEFAULT_MAX_WORDS = 8 | ||
|
||
function sentence(input, opts) { | ||
opts = opts || 0 | ||
var minSyllables = defaults(opts.minSyllables, DEFAULT_MIN_SYLLABLES) | ||
var minWords = defaults(opts.minWords, DEFAULT_MIN_WORDS) | ||
var maxWords = defaults(opts.minWords, DEFAULT_MAX_WORDS) | ||
var minClauses = defaults(opts.minClauses, DEFAULT_MIN_CLAUSES) | ||
var maxClauses = defaults(opts.maxClauses, DEFAULT_MAX_CLAUSES) | ||
|
||
var id = hash(input) | ||
var n = fit(id, minClauses, maxClauses) | ||
var i = 0 | ||
id = hash([id, 'sentence', i]) | ||
|
||
var firstOpts = conj(opts, { | ||
capitalize: true, | ||
minWords: minWords, | ||
maxWords: maxWords, | ||
minSyllables: minSyllables | ||
}) | ||
|
||
var restOpts = conj(firstOpts, { capitalize: false }) | ||
|
||
var result = words(id, firstOpts) | ||
|
||
while (++i < n) { | ||
id = hash([id, 'sentence', i]) | ||
result += ', ' + words(id, 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 |
Oops, something went wrong.