Word, char and line count

 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
function DocumentParser(Reader)
{
this.reader = Reader;
this.reset();
}
DocumentParser.prototype.reset = function()
{
this.wordCount = 0;
this.charCount = 0;
this.lineCount = 0;
};
DocumentParser.prototype.parse = function()
{
this.reset();
var prevChar = ' ';
var chunk = this.reader.getChunk();
while (chunk !== '') {
for (var i = 0; i < chunk.length; i++) {
if (chunk[i] === '\n') {
this.lineCount++;
prevChar = ' ';
} else {
this.charCount++;
if (this.lineCount === 0) this.lineCount = 1;
if (chunk[i] !== ' ' && prevChar === ' ') {
this.wordCount++;
}
prevChar = chunk[i];
}
}
chunk = this.reader.getChunk();
}
};