Skip to content

Commit

Permalink
jshint: double quotes to single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
swang committed Jul 1, 2015
1 parent 3f66d1a commit f87e8c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kindaFile = function(file) {
MarkovChain = function(args) {
if (!args) { args = {} }
this.wordBank = {}
this.sentence = ""
this.sentence = ''
this.files = []
if (args.files) {
return this.use(args.files)
Expand All @@ -47,14 +47,14 @@ MarkovChain = function(args) {
MarkovChain.prototype.VERSION = require('./package').version

MarkovChain.prototype.use = function(files) {
if (isType(files) === "array") {
if (isType(files) === 'array') {
this.files = files
}
else if (isType(files) === "string") {
else if (isType(files) === 'string') {
this.files = [files]
}
else {
throw new Error("Need to pass a string or array for use()")
throw new Error('Need to pass a string or array for use()')
}
return this
}
Expand Down Expand Up @@ -98,15 +98,16 @@ MarkovChain.prototype.process = function(callback) {
async.parallel(readFiles, function(err, retFiles) {
var words
, curWord

this.parseFile(retFiles.toString())

curWord = this.startFn(this.wordBank)

this.sentence = curWord

while (this.wordBank[curWord] && !this.endFn()) {
words = Object.keys(this.wordBank[curWord])
curWord = pickRandom(words)
this.sentence += " " + curWord
this.sentence += ' ' + curWord
}
callback(null, this.sentence.trim())

Expand All @@ -124,7 +125,7 @@ MarkovChain.prototype.parseFile = function(file) {
, nextWord
, words

words = lines.split(" ").filter(function(w) { return (w.trim() !== "") })
words = lines.split(' ').filter(function(w) { return (w.trim() !== '') })
for (i = 0; i < words.length - 1; i++) {
curWord = this.normalize(words[i])
nextWord = this.normalize(words[i + 1])
Expand All @@ -143,39 +144,39 @@ MarkovChain.prototype.parseFile = function(file) {

MarkovChain.prototype.start = function(fnStr) {
var startType = isType(fnStr)
if (startType === "string") {
if (startType === 'string') {
this.startFn = function() {
return fnStr
}
}
else if (startType === "function") {
else if (startType === 'function') {
this.startFn = function(wordList) {
return fnStr(wordList)
}
}
else {
throw new Error("Must pass a function, or string into start()")
throw new Error('Must pass a function, or string into start()')
}
return this
}

MarkovChain.prototype.end = function(fnStrOrNum) {
var endType = isType(fnStrOrNum)

if (endType === "function") {
if (endType === 'function') {
this.endFn = function() { return fnStrOrNum(this.sentence) }
}
else if (endType === "string") {
else if (endType === 'string') {
this.endFn = function() {
return this.sentence.split(" ").slice(-1)[0] !== fnStrOrNum
return this.sentence.split(' ').slice(-1)[0] !== fnStrOrNum
}
}
else if (endType === "number" || fnStrOrNum === undefined) {
else if (endType === 'number' || fnStrOrNum === undefined) {
fnStrOrNum = fnStrOrNum || Infinity
this.endFn = function() { return this.sentence.split(" ").length > fnStrOrNum }
this.endFn = function() { return this.sentence.split(' ').length > fnStrOrNum }
}
else {
throw new Error("Must pass a function, string or number into end()")
throw new Error('Must pass a function, string or number into end()')
}
return this
}
Expand Down
42 changes: 21 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@ describe('MarkovChain', function() {

describe('readFile', function() {
it('return the contents of file a.txt', function(done) {
testMarkov.use(["./test/fixtures/a.txt"]).start("this").end(5)
testMarkov.use(['./test/fixtures/a.txt']).start('this').end(5)
testMarkov.readFile(testMarkov.files[0])(function(err, resp) {
expect(err).to.not.exist
expect(resp).to.be.a('string')
expect(resp).to.equal("this is file: a.txt\nthis is not file: b.txt\n")
expect(resp).to.equal('this is file: a.txt\nthis is not file: b.txt\n')
done()
})
})
})

describe('process', function() {
it('should process files into word banks', function(done) {
testMarkov.use(["./test/fixtures/a.txt"]).start("not").end(5).process(function(err, resp) {
testMarkov.use(['./test/fixtures/a.txt']).start('not').end(5).process(function(err, resp) {
expect(err).to.not.exist
expect(testMarkov.countTotal('this')).to.equal(2)
expect(testMarkov.countTotal('is')).to.equal(2)
expect(testMarkov.countTotal('file:')).to.equal(2)
expect(testMarkov.countTotal('not')).to.equal(1)
expect(resp.substring(0, 4)).to.equal("not ")
expect(resp.substring(0, 4)).to.equal('not ')
done()
})
})
})

describe('start', function() {
it('should set the start property with value, "spot"', function(done) {
it('should set the start property with value "spot"', function(done) {
testMarkov.start('spot')
expect(testMarkov.startFn()).to.equal("spot")
expect(testMarkov.startFn()).to.equal('spot')
done()
})
it('should set the start property with a function, "onlyIfWordHasLetterI"', function(done) {
it('should set the start property with a function "onlyIfWordHasLetterI"', function(done) {
var onlyIfWordHasLetterI = function(wordList) {
var words = Object.keys(wordList);
var tmpList = words.filter(function(word) { return word.indexOf('i') > -1 })
Expand All @@ -93,35 +93,35 @@ describe('MarkovChain', function() {
})

describe('end', function() {
it('should set the end property with value, "tops"', function(done) {
testMarkov.sentence = "this sentence ends with tops"
testMarkov.end("tops")
it('should set the end property with value "tops"', function(done) {
testMarkov.sentence = 'this sentence ends with tops'
testMarkov.end('tops')
expect(testMarkov.endFn()).to.equal(false)
testMarkov.end("nottops")
testMarkov.end('nottops')
expect(testMarkov.endFn()).to.equal(true)
done()
})
it('should set the end property with value, 5', function(done) {
it('should set the end property with value "5"', function(done) {
testMarkov.end(5)
testMarkov.sentence = "this is a test of a test"
testMarkov.sentence = 'this is a test of a test'
expect(testMarkov.endFn()).to.equal(true)
testMarkov.sentence = "this is a test of"
testMarkov.sentence = 'this is a test of'
expect(testMarkov.endFn()).to.equal(false)
testMarkov.sentence = "this is a"
testMarkov.sentence = 'this is a'
expect(testMarkov.endFn()).to.equal(false)
done()
})
it('should set the end property with a function', function(done) {
testMarkov.end(function() { return this.sentence.split(" ").length > 3 || this.sentence.split(" ").slice(-1)[0] === "test" }.bind(testMarkov))
testMarkov.sentence = "test"
testMarkov.end(function() { return this.sentence.split(' ').length > 3 || this.sentence.split(' ').slice(-1)[0] === 'test' }.bind(testMarkov))
testMarkov.sentence = 'test'
expect(testMarkov.endFn()).to.equal(true)
testMarkov.sentence = "this is a test of a test"
testMarkov.sentence = 'this is a test of a test'
expect(testMarkov.endFn()).to.equal(true)
testMarkov.sentence = "this is a test of"
testMarkov.sentence = 'this is a test of'
expect(testMarkov.endFn()).to.equal(true)
testMarkov.sentence = "this is a test of the tester"
testMarkov.sentence = 'this is a test of the tester'
expect(testMarkov.endFn()).to.equal(true)
testMarkov.sentence = "this is a"
testMarkov.sentence = 'this is a'
expect(testMarkov.endFn()).to.equal(false)
done()
})
Expand Down

0 comments on commit f87e8c1

Please sign in to comment.