-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_mimodule.js
More file actions
42 lines (30 loc) · 955 Bytes
/
Copy path6_mimodule.js
File metadata and controls
42 lines (30 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var fs = require('fs');
var path = require('path');
// exports one function that takes 3 args:
// dirname, filext, callback
// note: filext must = process.argv[3], can't be altered in mim.js
function filterContent(dirname, filext, callback) {
fs.readdir(dirname,
// readdir passes 2 args to this function, err & array of filenames
function(err, data) {
if (err) {
return callback(err);
}
// filter stuff out by filext
// save it in filteredArray
var filextFull = '.' + filext;
var filteredArray = [];
data.forEach(function(filename) {
if (path.extname(filename) === filextFull) {
filteredArray.push(filename);
}
});
// invoke the callback, pass in already filtered array
callback(null, filteredArray);
}
)
};
// must invoke it using callback(null, data)
// data will be your filtered list of files/array
// don't print directly to console from module file
module.exports = filterContent;