
var crypto = require('crypto');
var multimatch = require('multimatch');
var path = require('path');


module.exports = plugin;


function plugin(options) {
    
    // default options
    options = options || {};
    if (typeof options.keep !== "boolean") options.keep = false;
    options.algorithm = options.algorithm || 'sha256';
    options.pattern = options.pattern || ['**/*.{js,scss,css,map,png,jpg}'];
    options.rename = options.rename || function(filepath, digest) {
        
        // we split at the first period, instead of extname
        //  this is to handle .css.map
        var ext = filepath.indexOf('.');
        
        return [
            filepath.substring(0, ext),
            '.', digest.substr(0, 16),
            filepath.substring(ext),
        ].join('');
        
    };
    
    return function(files, ms, done) {
        
        var metadata = ms.metadata();
        metadata.hashes = metadata.hashes || {};
        
        Object.keys(files)
            .filter(function(filepath) {
                
                return multimatch(filepath, options.pattern).length > 0;
                
            })
            .forEach(function(filepath) {
                
                
                // this might error, that's Ok.
                var hash = crypto.createHash(options.algorithm);
                
                hash.update(files[filepath].contents);
                var digest = hash.digest('hex');
                
                var destination = options.rename(filepath, digest);
                
                files[destination] = files[filepath];
                if (!options.keep) delete files[filepath];
                
                metadata.hashes[filepath] = destination;
                
            });
        
        return process.nextTick(done);
        
    };
    
}

@@ -960,127 +960,57 @@
     
-Object.keys(files)%0A            .filter(function(filepath) %7B%0A                %0A                return multimatch
+var relevantFiles = multimatch(Object.keys
 (file
-path
+s)
 , op
@@ -1027,65 +1027,31 @@
 ern)
-.length %3E 0;%0A                %0A            %7D)%0A            
+;%0A        relevantFiles
 .for
@@ -1092,34 +1092,9 @@
     
-    
 %0A
-                %0A    
     
@@ -1133,20 +1133,16 @@
 t's Ok.%0A
-    
         
@@ -1202,33 +1202,25 @@
             
- 
-   %0A    
+%0A
             
@@ -1266,28 +1266,24 @@
             
-    
 var digest =
@@ -1307,38 +1307,33 @@
 ');%0A            
-    %0A 
+%0A
                v
@@ -1324,27 +1324,24 @@
             
-   
 var destinat
@@ -1384,38 +1384,33 @@
 t);%0A            
-    %0A 
+%0A
                f
@@ -1401,27 +1401,24 @@
             
-   
 files%5Bdestin
@@ -1435,36 +1435,32 @@
 iles%5Bfilepath%5D;%0A
-    
             if (
@@ -1506,33 +1506,25 @@
             
- 
-   %0A    
+%0A
             
@@ -1572,33 +1572,25 @@
             
-    %0A    
+%0A
         %7D);%0A

Refactor pattern match loop