如何使用javascript小解释调用函数中的函数

时间:2014-07-07 07:10:53

标签: javascript node.js

我是java脚本的初学者,尝试一个解析文本文件数据的小程序。为了在特定日期创建过滤器,我已经创建了一个函数来获取用户输入的日期。如果它在范围内,则必须比较的日期是在文本文件中,这个日期也得到了它。但现在我不想重写函数" getAllFilesFromFolder"在下面的代码中找到,此函数必须在所有情况下执行,但如果我按日期点击按钮过滤器,它必须读取用户给出的日期范围内的文件。有人可以给我一个如何做的解释。我试过下面的代码。

function getdate(){
      var dateStart = new Date($('#dateStart').val()).getTime();
      var dateEnd = new Date($('#dateEnd').val()).getTime();
      //if(!testDate){var testDate = new Date(2014, 05, 02).getTime();}
      var testDate = new Date(2014, 05, 02).getTime();
      if (dateStart <= testDate && testDate <= dateEnd) {
          alert('IN');
          //Here filter the files with the date 
      }else{
         alert('OUT');
         //Here no new to read and parse the file beacause it is out of range 
      }

    }

//Parse folder and file to get the required files
function getAllFilesFromFolder(folder){
  //Parsing the given folder the result which is return is kept in an array
  var test = fse.readdirSync(folder);
  //Going through the array 
  for(var n=0; test[n]; n++){
    var stats = fs.lstatSync(folder+"\\"+test[n]);
    if(stats && stats.isDirectory()){
      getAllFilesFromFolder(folder+"\\"+test[n]);
      var path = folder+"\\"+test[n]+"<br />";
      }else{  
        var path = folder+"\\"+test[n];
        //Regex on the file to be taken
        var pattern = /^(PM)[0-9]{5}[_](xam)[_](pmbok5th)[_](pmp)[_](v92)(.txt)$/;
        var parent = folder+"\\";
        var file = test[n];
        var load = pattern.test(file); 
        //Split on file to get the "id user"
        identifiant = file.split('_');
        //Test the regex on file name "PM*_xam_pmbok5th_pmp_v92.txt" 
        if(load == true){
          var read = fse.readFileSync(path, 'utf8');
          var suspendDate = read.lastIndexOf('xam/');
          var wantedDate = read.slice(suspendDate);
          info = wantedDate.split('/');
          var suspendData = read.lastIndexOf('/DB');
          var suspendData = wantedDate.lastIndexOf('/DB');
          var wantedData = wantedDate.slice(suspendData);
          var db_response = wantedData.split(".");
          var all_response = db_response[0].split(":");  


          if(typeof(info[2]) != "undefined" && all_response != "undefined"){
            var response = all_response[1].split("|");
            //Parsing the array response to find the "id" here id of the question and the "ans" here answer of the question "R" or "W"
            for(var p = 0; p < response.length; p++){
              //Test if result exist we increment if not it is generate with the function initResp
              if(typeof(response[p]) != "undefined"){
                var id = response[p].slice(0,6);
                var ans = response[p].slice(-1);
                if (question[id]) {
                  question[id][ans] += 1;
                  } else {
                    var results = initResp(ans);
                    question[id] = results;
                    }; 

                } else {
                  } 
            }
          } else {
            //$("#results1").append("<strong>La session est vide</strong><br>");
            }
          i++;
      } 
    }
  }
}; 

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。有关详细信息,请参阅以下代码

function rangeDate(testDate){
  var dateStart = new Date($('#dateStart').val()).getTime();
  var dateEnd = new Date($('#dateEnd').val()).getTime();
  if (dateStart <= testDate && testDate <= dateEnd) {
      date = true;
      return date;
  }else{
      date = false;
      return date;
  }

}
function getAllFilesFromFolder(folder)/*, useDate*/{
  //Parsing the given folder the result which is return is kept in an array
  var test = fse.readdirSync(folder);
  //Going through the array 
  for(var n=0; test[n]; n++){
    var stats = fs.lstatSync(folder+"\\"+test[n]);
    if(stats && stats.isDirectory()){
      getAllFilesFromFolder(folder+"\\"+test[n]);
      var path = folder+"\\"+test[n]+"<br />";
      }else{  
        var path = folder+"\\"+test[n];
        //Regex on the file to be taken
        var pattern = /^(PM)[0-9]{5}[_](xam)[_](pmbok5th)[_](pmp)[_](v92)(.txt)$/;
        var parent = folder+"\\";
        var file = test[n];
        var load = pattern.test(file); 
        //Split on file to get the "id user"
        identifiant = file.split('_');
        //Test the regex on file name "PM*_xam_pmbok5th_pmp_v92.txt" 
        if(load == true){
          var read = fse.readFileSync(path, 'utf8');
          var suspendDate = read.lastIndexOf('xam/');
          var wantedDate = read.slice(suspendDate);
          var info = wantedDate.split('/');
          testDate = new Date(info[4]).getTime();
          rangeDate(testDate);
          if(date == true){
            var suspendData = read.lastIndexOf('/DB');
            var suspendData = wantedDate.lastIndexOf('/DB');
            var wantedData = wantedDate.slice(suspendData);
            var db_response = wantedData.split(".");
            var all_response = db_response[0].split(":");  
            if(typeof(info[2]) != "undefined" && all_response != "undefined"){
              var response = all_response[1].split("|");
              //Parsing the array response to find the "id" here id of the question and the "ans" here answer of the question "R" or "W"
              for(var p = 0; p < response.length; p++){
                //Test if result exist we increment if not it is generate with the function initResp
                if(typeof(response[p]) != "undefined"){
                  var id = response[p].slice(0,6);
                  var ans = response[p].slice(-1);
                  if (question[id]) {
                    question[id][ans] += 1;
                    } else {
                      var results = initResp(ans);
                      question[id] = results;
                      }; 

                  } else {
                    } 
              }
            } else {
              //$("#results1").append("<strong>La session est vide</strong><br>");
              }
            i++;
          }else{

          }

      } 
    }
  }


};