Learnyounode: Exercise 6
Make it modular (refactoring Exercise 5)
Challenge

The result of this exercise is the same as the previous, but you must import a module from a separate file, which holds a function to produce the desired result.
The callback function must be called. Notice what the challenge description says about the first argument being passed as null in the event that there are no errors, and the second argument passed being your data.
One other condition to take note of is that you are required to print to the console from the original program file, not the module.
Solution
This solution requires you to create two files:
Line-by-line Analysis
Load the module you are about to build into a variable called myModule. Require it from a file called module.js which will have to be created.
/modular.js
var myModule = require(‘./module.js’)
Load the directory path and file extension filter string from the command line arguments into two variables called dir and filterString.
var dir = process.argv[2]
var filterString = “.” + process.arg[3]
Call the myModule function using the description of conditions from the challenge description. “The module must take three arguments: the directory name, the filename extension string, and a callback function.”
myModule(dir, filterString, function(err, list) {
Now is the time to handle one of the four conditions the module must follow: “Handle all the errors that may occur and pass them to the callback.”
if (err)
return console.error(‘Error: ‘, err)
If there is an error, the function returns console.error command with the error.
If there is no error, then the list returned from the callback will be a list of files that myModule returns. Iterate through this array with the .forEach function and use console.log to output the file names to the console, fulfulling the condition to print the file names from the original program file, not the module.
list.forEach(function (file) {
console.log(file)
})
})
/module.js
This module will return the filenames in the directory and contain much of the same logic as Exercise 5, with a few differences.
Start by requiring the fs and path modules as before:
var fs = require(‘fs’)
var path = require(‘path’)
Assign a single function export to the module.exports object. Remember the three arguments that this function uses to generate it’s result.
module.exports = function(dir, fileString, callback) {
Within this function, use fs.readdir and pass it a callback checking for errors as the third argument. This is an early return within a callback function.
fs.readdir(dir, function (err, list) {
Detect if there is an error before doing anything else. If an error is found, return it to the callback function which is handled from the original program file by outputting the returned error using console.log.
if (err)
return callback(err)
Otherwise, let list equal the result of calling .filter on list to filter through the files and return the ones matching the extension defined by fileString. This list will be returned to the callback in order to print them out to the console from the original program file. In the previous exercise, we just used console.log while iterating and filtering through the file names.
list = list.filter(function (file) {
return path.extname(file) === fileString
})
In the last part of this function, call the callback function, passing null as the first argument, and the newly filtered list as the second argument.
callback(null, list)
})
}

