File tree Expand file tree Collapse file tree 2 files changed +38
-3
lines changed 
chapter_03-using-stubs-to-break-dependencies/LogAn Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 1+ {
2+     "extensions" : [" slf" " sql" 
3+ }
Original file line number Diff line number Diff line change 1+ const  fs  =  require ( 'fs' ) ; 
2+ const  util  =  require ( 'util' ) ; 
13const  ArgumentError  =  require ( './ArgumentError' ) ; 
24
5+ const  readFilePromisied  =  util . promisify ( fs . readFile ) ; 
6+ 
37function  logAnalyzer ( )  { 
48    /** 
59     * @type  {boolean } 
@@ -15,23 +19,51 @@ function logAnalyzer() {
1519
1620    /** 
1721     * @param  {string } fileName 
18-      * @return  {boolean } 
22+      * @return  {Promise } 
1923     */ 
20-     function  isValidLogFileName ( fileName )  { 
24+     async   function  isValidLogFileName ( fileName )  { 
2125        wasLastFileNameValid  =  false ; 
2226
2327        if  ( fileName  ===  '' )  { 
2428            throw  new  ArgumentError ( 'filename has to be provided' ) ; 
2529        } 
2630
27-         if  ( ! fileName . toUpperCase ( ) . endsWith ( '.SLF' ) )  { 
31+         const  result  =  await  isValid ( fileName ) ; 
32+ 
33+         if  ( ! result )  { 
2834            return  false ; 
2935        } 
3036
3137        wasLastFileNameValid  =  true ; 
3238        return  true ; 
3339    } 
3440
41+     /** 
42+      * @param  {string } fileName 
43+      */ 
44+     async  function  isValid ( fileName )  { 
45+         const  fileNameExtensions  =  await  readFilePromisied ( 
46+             `${ __dirname }  , 
47+             'utf8' 
48+         ) . then ( fileContent  =>  JSON . parse ( fileContent ) . extensions ) ; 
49+ 
50+         const  isValidExtension  =  fileNameExtensions . some ( 
51+             function  checkFileNameExtension ( extension )  { 
52+                 if  ( 
53+                     fileName 
54+                         . toUpperCase ( ) 
55+                         . endsWith ( `.${ extension . toUpperCase ( ) }  ) 
56+                 )  { 
57+                     return  true ; 
58+                 } 
59+ 
60+                 return  false ; 
61+             } 
62+         ) ; 
63+ 
64+         return  isValidExtension ; 
65+     } 
66+ 
3567    return  { 
3668        getWasLastFileNameValid, 
3769        isValidLogFileName, 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments