Comparing version 1.0.8 to 1.1.0
76
index.js
@@ -16,10 +16,10 @@ 'use strict' | ||
} else if (typeof options !== 'object') { | ||
throw new TypeError( | ||
'Punc: expected options to be either an object or a \ | ||
string, ' + 'but got ' + typeof options + ' instead' | ||
throw new TypeError(`Punc: expected options to be either | ||
an object or a string, but got ${typeof options} instead` | ||
) | ||
} | ||
let punctuationsOnly = [] | ||
let punctuationsSeen = { ';': 0 | ||
let punctuationStore = [] | ||
let wordsPerSent = 0 | ||
let dict = { ';': 0 | ||
, ':': 0 | ||
@@ -35,19 +35,18 @@ , "'": 0 | ||
, '-': 0 | ||
} | ||
} | ||
return new Promise((resolve, reject) => { | ||
ReadFile(filePath, options.encoding) | ||
.pipe(Through2.obj(function(chunk, _, callback) { | ||
ForEach.call(chunk, each => { | ||
if ( each in punctuationsSeen ) { | ||
punctuationsSeen[ each ]++ | ||
punctuationsOnly.push(each) | ||
} | ||
.pipe(removeCarriageReturn()) | ||
.pipe(removeDoubleSpaces()) | ||
.pipe(findWordsPerSentence(count => wordsPerSent = count)) | ||
.pipe(findAndCount(dict, punctuationStore)) | ||
.on('data', _ => _) | ||
.on('end', _ => { | ||
console.log('end count:', wordsPerSent) | ||
return resolve({ body: punctuationStore.join('') | ||
, count: dict | ||
, wordsPerSentence: wordsPerSent | ||
}) | ||
callback() | ||
})) | ||
.on('data', _ => _) | ||
.on('end', _ => resolve({ body: punctuationsOnly.join('') | ||
, count: punctuationsSeen | ||
})) | ||
}) | ||
.on('error', error => reject(error)) | ||
@@ -57,3 +56,42 @@ }) | ||
function removeCarriageReturn () { | ||
return Through2.obj(function(chunk, _, callback) { | ||
chunk = chunk.replace(/\r/g, '') | ||
callback(null, chunk) | ||
}) | ||
} | ||
function removeDoubleSpaces () { | ||
return Through2.obj(function(chunk, _, callback) { | ||
chunk = chunk.replace(/\s\s+/g, ' ') | ||
callback(null, chunk) | ||
}) | ||
} | ||
function findWordsPerSentence (changeCount) { | ||
return Through2.obj(function(chunk, _, callback) { | ||
let periodCount = (chunk.match(/\.|\?|\!/g) || []).length | ||
let wordCount = chunk.split(' ').length | ||
changeCount(wordCount / periodCount) | ||
callback(null, chunk) | ||
}) | ||
} | ||
function findAndCount (map, dest) { | ||
return Through2.obj(function(chunk, _, callback) { | ||
ForEach.call(chunk, punctuation => { | ||
if ( punctuation in map ) { | ||
map[ punctuation ]++ | ||
dest.push(punctuation) | ||
} | ||
}) | ||
callback() | ||
}) | ||
} | ||
/* THE TRUTH IS OUT THERE */ | ||
module.exports = Punc | ||
module.exports = Punc | ||
{ | ||
"name": "punc", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Ever wonder what your favorite books look like without words?", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,11 @@ 'use strict' | ||
let aliceStub = new Buffer('-;alice,in.wonder\'land(-;-.,""') | ||
let word_count_stub = new Buffer(`one. | ||
two two! | ||
three three three? | ||
four four four four.`) | ||
Mock({ 'books': { 'alice.txt': aliceStub } }) | ||
Mock({ 'books': { 'alice.txt': aliceStub | ||
, 'word_count.txt': word_count_stub | ||
} | ||
}) | ||
@@ -57,4 +64,5 @@ /* TESTS */ | ||
if (aliceCheatSheet[key] !== book.count[key]) { | ||
return t.fail(`Value mismatch.\ | ||
Expected: ${aliceCheatSheet[key]}.\ | ||
return t.fail(`Value mismatch.\n\ | ||
Punctuation: ${key}\n\ | ||
Expected: ${aliceCheatSheet[key]}.\n\ | ||
Actual: ${book.count[key]}`) | ||
@@ -75,2 +83,12 @@ } | ||
.catch(err => t.fail(err)) | ||
}) | ||
Test('Punc: words per sentence', t => { | ||
t.plan(2) | ||
Punc('books/word_count.txt') | ||
.then(book => { | ||
t.ok(book.wordsPerSentence, 'wordCount property exists on returned object') | ||
t.equals(book.wordsPerSentence, 2.5) | ||
}) | ||
.catch(err => t.fail(err)) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7294
158