Last active
June 1, 2016 13:58
-
-
Save daviddias/95a5781f950f5baa5d3e35919bc50dc0 to your computer and use it in GitHub Desktop.
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'); | |
// fs.readdir is an async func, it doesn't return any value | |
// see: https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback | |
// var lists = fs.readdir(process.argv[2], callback); | |
fs.readdir(process.argv[2], callback); | |
// it yields just a single list, not lists. That list is in fact an array | |
// function callback (err, lists) { | |
function callback (err, list) { | |
if (err) { | |
throw err; | |
} | |
// it is fine to redefine the variable, however, for clarity you should put it on a new var | |
var filtered = list.filter(function (item) { | |
// | |
return path.extname(item) === '.' + process.argv[3] | |
}) | |
// You don't want to call the callback inside the callback, it will just make the function recursive | |
// Just like your previous solution, you just need to print the result (filtered list) | |
// callback(null, list) | |
console.log(filtered) | |
} |
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 lists = fs.readdir (process.argv[2], callback); | |
function callback (err, lists) { | |
if (err) throw err; | |
lists = lists.filter(function (list) { | |
return path.extname(list) === '.' + process.argv[3] | |
}) | |
callback(null, list) | |
} |
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 dirPath = process.argv[2] | |
var ext = '.' + process.argv[3] | |
fs.readdir(dirPath, function (err, list) { | |
if (err) { throw err } | |
list.filter(function (item) { | |
return (path.extname(item) === ext) | |
}).forEach(function (item) { | |
console.log(item) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment