Last active
May 30, 2016 15:54
-
-
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"
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
/* 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