Created
July 26, 2013 21:03
-
-
Save gergob/6092217 to your computer and use it in GitHub Desktop.
Contains the implementation of the wc linux command using node.js
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
#!/usr/bin/env node | |
/* | |
This small node.js app should do exactly what the wc linux command does. | |
Quote from man wc: | |
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard | |
input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, | |
always in the following order: newline, word, character, byte, maximum line length. | |
In this version I will use the Commander node js extension, please see the https://github.com/visionmedia/commander.js github page. | |
Please use the npm install commander command to install the library in your node app folder. | |
*/ | |
var fs = require('fs'); | |
var commander = require('commander'); | |
var NEW_LINE_DELIMITER = '\n'; | |
var SPACE = ' '; | |
var TAB = '\t'; | |
var cloneFunction = function(fn) { | |
// Workaround for commander.js issue. | |
// http://stackoverflow.com/a/6772648 | |
return fn.bind({}); | |
}; | |
var checkIfFileExists = function(infile) { | |
var infilePath = infile.toString(); | |
var stats = fs.statSync(infilePath); | |
if(!stats && !stats.isFile()) { | |
process.stdout.write('%s does not exist.', infilePath); | |
process.exit(1); | |
} | |
return infilePath; | |
}; | |
var countLines = function(infile) { | |
var fileContent = fs.readFileSync(infile); | |
if(fileContent) { | |
var lineCount = fileContent.toString().split(NEW_LINE_DELIMITER); | |
if(lineCount && lineCount.length > 0) { | |
var numberOfLines = lineCount.length - 1; | |
process.stdout.write(numberOfLines + ' ' + infile + NEW_LINE_DELIMITER); | |
} | |
else { | |
process.stdout.write('The file is empty.'); | |
} | |
} | |
else { | |
process.stdout.write('The file is empty.'); | |
} | |
}; | |
var countWords = function(infile) { | |
var fileContent = fs.readFileSync(infile); | |
if(fileContent) { | |
var sumOfWords = 0; | |
var lines = fileContent.toString().split(NEW_LINE_DELIMITER); | |
for(var i=0; i<lines.length ; ++i) { | |
if(lines[i] != NEW_LINE_DELIMITER && lines[i] != TAB && lines[i].length > 0) { | |
sumOfWords += (lines[i].split(SPACE)).length; | |
} | |
} | |
process.stdout.write(sumOfWords + ' ' + infile + NEW_LINE_DELIMITER); | |
} | |
else { | |
process.stdout.write('The file is empty.'); | |
} | |
}; | |
var countCharacters = function(infile) { | |
var fileContent = fs.readFileSync(infile); | |
if(fileContent) { | |
var textInFile = fileContent.toString(); | |
process.stdout.write(textInFile.length + ' ' + infile + NEW_LINE_DELIMITER); | |
} | |
else { | |
process.stdout.write('The file is empty.'); | |
} | |
}; | |
commander.option('-l, --lines <file>', 'Path to file to analyze', cloneFunction(checkIfFileExists)) | |
.option('-w, --words <file>', 'Path to file to analyze', cloneFunction(checkIfFileExists)) | |
.option('-c, --characters <file>', 'Path to file to analyze', cloneFunction(checkIfFileExists)) | |
.parse(process.argv); | |
if(commander.lines) { | |
countLines(commander.lines); | |
} | |
if(commander.words) { | |
countWords(commander.words); | |
} | |
if(commander.characters) { | |
countCharacters(commander.characters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment