Skip to content

Instantly share code, notes, and snippets.

@joshwnj joshwnj/count-original.js Secret
Last active Aug 29, 2015

Embed
What would you like to do?
untangling-callbacks
var lines = res.split('\n');
var count = 0;
for (var i = 0; i < lines.length; i += 1) {
// only count the non-empty lines
if (lines[i].length > 0) { count += 1; }
}
function countNonEmptyLines (content) {
return content
.split('\n')
.filter(Boolean)
.length;
}
function countNonEmptyLines (string) { ...
function findMaxFileInDirectory (directory, callback) {
// get a list of filenames
// ...
findMaxFile(filenames, callback);
}
function findMaxFile (filenames, callback) {
// find the file containing the most non-empty lines
// ...
}
filenames.forEach(function (filename) {
fs.readFile(filename, function (err, contents) {
var count = countNonEmptyLines(contents);
if (count > maxLines) {
maxFilename = filename;
maxLines = count;
}
});
})
// load all files
loadFiles(filenames, function (err, results) {
// get an array of line-counts
var lineCounts = results.map(countNonEmptyLines);
// get the index of the highest number
var i = indexOfMax(lineCounts);
// now we know which file has the most non-empty lines
var maxFile = filenames[i];
...
filenames.forEach(function (filename) {
var pathToFile = path.join(directory, filename);
fs.readFile(pathToFile, 'utf8', function (err, contents) {
...
var fs = require('fs');
var path = require('path');
function findMaxFileInDirectory (directory, callback) {
// find all files in the directory
fs.readdir(directory, function (err, filenames) {
if (err) { return callback(err); }
// keep track of the maximum as we go
var maxLines = 0;
var maxFilename = null;
// keep track of how many files we have left to process
var remaining = filenames.length;
filenames.forEach(function (filename) {
// prepend the directory to get the full file path
filename = path.join(dir, filename);
// load the file
fs.readFile(filename, 'utf8', function (err, res) {
if (err) { return callback(err); }
var lines = res.split('\n');
var count = 0;
for (var i = 0; i < lines.length; i += 1) {
// only count the non-empty lines
if (lines[i].length > 0) { count += 1; }
}
// have we got a new maximum?
if (count > maxLines) {
maxFilename = filename;
maxLines = count;
}
// when all files have been checked, send back the result
remaining -= 1;
if (remaining === 0) { callback(null, maxFilename); }
});
});
})
}
fs.readdir(directory, function (err, filenames) { ...
var fs = require('fs');
var path = require('path');
var loadFiles = require('./lib/load-files');
var countNonEmptyLines = require('./lib/count-non-empty-lines');
var indexOfMax = require('./lib/index-of-max');
function findMaxFileInDirectory (dir, callback) {
fs.readdir(dir, function (err, filenames) {
if (err) { return callback(err); }
// create a list of file paths
var prependDir = function (filename) { return path.join(dir, filename); };
findMaxFile(filenames.map(prependDir), callback);
});
}
function findMaxFile (filenames, callback) {
// load all files
loadFiles(filenames, function (err, results) {
if (err) { return callback(err); }
// count non-empty lines
var lineCounts = results.map(countNonEmptyLines);
var i = indexOfMax(lineCounts);
// send back the result
callback(null, filenames[i]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.