-
-
Save joshwnj/9e07e96e53f8724ac34b to your computer and use it in GitHub Desktop.
untangling-callbacks
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
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; } | |
} |
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
function countNonEmptyLines (content) { | |
return content | |
.split('\n') | |
.filter(Boolean) | |
.length; | |
} |
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
function countNonEmptyLines (string) { ... |
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
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 | |
// ... | |
} |
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
filenames.forEach(function (filename) { | |
fs.readFile(filename, function (err, contents) { | |
var count = countNonEmptyLines(contents); | |
if (count > maxLines) { | |
maxFilename = filename; | |
maxLines = count; | |
} | |
}); | |
}) |
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
// 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]; | |
... |
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
filenames.forEach(function (filename) { | |
var pathToFile = path.join(directory, filename); | |
fs.readFile(pathToFile, 'utf8', function (err, contents) { | |
... |
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
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); } | |
}); | |
}); | |
}) | |
} |
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
fs.readdir(directory, function (err, filenames) { ... |
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
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