Skip to content

Instantly share code, notes, and snippets.

@JuanCaicedo
Last active May 30, 2016 15:54
Show Gist options
  • Save JuanCaicedo/5d45403382d998508239ea6fb6837d13 to your computer and use it in GitHub Desktop.
Save JuanCaicedo/5d45403382d998508239ea6fb6837d13 to your computer and use it in GitHub Desktop.
This is an example solution to exercise 6 in learnyounode, "Make it Modular"
/* main.js */
var myModule = require('./my-module');
var directory = process.argv[2];
var filter = process.argv[3];
var callback = function(error, filtered) {
if (error) {
console.log('error');
console.log(error);
}
for (var index in filtered) {
console.log(filtered[index]);
}
};
myModule(directory, filter, callback);
/* my-module.js */
var fs = require('fs');
var path = require('path');
function filterFiles(list, filter) {
return list.filter(function(file) {
return path.extname(file) == '.' + filter;
});
};
module.exports = function(directory, filter, callback) {
fs.readdir(directory, function(error, list) {
if (error) {
return callback(error);
}
var filtered = filterFiles(list, filter);
return callback(null, filtered);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment