Nodeschool learnyounode node.js module FILTER LS exercise

The following is exercise 5 of the student learning module for students

Create a program that prints a list of files in a given directory, filtered by file extension. You will be given the directory name as the first application to your program (for example /path/to/dir/) and the file extension for filtering by the second argument.

For example, if you get "txt" as the second argument, you will need to filter the list only for files ending in .txt.

The list of files must be printed to the console, one file per line and must use asynchronous I / O.

var fs = require('fs');
var path = require('path');
var mydir = process.argv[2];
var ext1 = process.argv[3]
fs.readdir(mydir, function(err, files){
  if(err){
    throw err
  }
  //console.log(files);
  files.forEach(function(filename){
    var ext = path.extname(filename);
    if(ext == ext1){
      console.log(filename);
    }
  });
});

, , , learnyounode,

, . - plz???

+4
7

- . :

    if(ext == ext){ // you're comparing the same variable
      console.log(filename);
    }

:

    if(ext === ext1){ // try to use '==='
      console.log(filename);
    }

: . of .txt , ext1, .extname(file) .:

var ext1 = '.' + process.argv[3];
+2

:

var fs = require('fs')
var path = require('path')

fs.readdir(process.argv[2], function (err, list) {
  list.forEach(function (file) {
    if (path.extname(file) === '.' + process.argv[3])
      console.log(file)
  })
})
+2

, :

var fs = require('fs');
    function endsWith(str, suffix) {
        var s = str.slice(str.length - suffix.length - 1);
        if (s == "." + suffix)
            return true;
        else
            return false;
};


fs.readdir(process.argv[2], function (err, list) {
    if (process.argv[3]) {
        for (var i = 0; i < list.length; i++) {
            if (endsWith(list[i], process.argv[3]))
                console.log(list[i]);
        }
    }
});
0

:

var fs = require('fs');
var filePath = process.argv[2];
var fileType = '.' + process.argv[3];

fs.readdir(filePath, function(err, list) {
for(var i=0; i<list.length; i++){
    if (list[i].match(fileType)) {
        console.log(list[i]);
    }
}
});
0

, :

var fs = require('fs');
var path = process.argv[2]; //first argument
var extension = process.argv[3]; //second argument
var re = new RegExp("."+extension, "g"); //a regexp that matches every string that begins with a dot and is followed by the extension, i.e. .txt

fs.readdir(path, function callback(err, list){ //read the directory
  if (!err) { //if no errors occur run next funtion
    list.forEach(function(val) { //take the list and check every value with the statement below
      if(re.test(val)) { //if the .test() rexexp-function does not match it will return a false, if it does it will return true
        console.log(val); //if it matches console log the value
      }
    });
  }
});
0

, , '.' .

var extension = '.' + process.argv [3];

Then you can do the comparison and printing.

0
source

that's how i solved it

var fs = require('fs');
const path = require("path")

var dir = process.argv[2],
    ext = "."+process.argv[3];
function borer(callback){
    fs.readdir(dir,function(err,list){
        if(err){
            console.log(err) 
        }else{
            var row = list.filter((a)=>{
                var regexp = new RegExp(ext+"$","ig") 
                if( a.search(regexp) > -1 ){
                    callback(a)
                }
            })

        }
    })
}
function print(f){
    console.log(f)
}

borer(print)
0
source

Source: https://habr.com/ru/post/1543239/


All Articles