all files / dir-compare/ common.js

100% Statements 36/36
100% Branches 44/44
100% Functions 5/5
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87           2362× 2224× 996×   1228×     138×             386× 386× 502× 502× 132×     254×             1212×     1210× 108× 24×   84×     1102× 278× 108×   170×     824×           1272× 104× 1168× 168×     1000× 1000×             72× 66× 16×     50× 50×            
var minimatch = require('minimatch');
 
module.exports = {
	/**
	 * One of 'missing','file','directory'
	 */
	getType : function(fileStat) {
		if (fileStat) {
			if (fileStat.isDirectory()) {
				return 'directory';
			} else {
				return 'file';
			}
		} else {
			return 'missing';
		}
	},
	/**
	 * Matches fileName with pattern.
	 */
	match : function(fileName, pattern){
	    var patternArray = pattern.split(',');
	    for(var i = 0; i<patternArray.length; i++){
	        var pat = patternArray[i];
	        if(minimatch(fileName, pat, { dot: true })){ //nocase
	            return true;
	        }
	    }
	    return false;
	},
 
	/**
	 * Filter entries by file name. Returns true if the file is to be processed.
	 */
	filterEntry : function(entry, options){
	    if(entry.symlink && options.skipSymlinks){
	        return false;
	    }
	    
	    if(entry.stat.isFile() && options.includeFilter){
	        if(this.match(entry.name, options.includeFilter)){
	            return true;
	        } else{
	            return false;
	        }
	    }
	    if(options.excludeFilter){
	        if(this.match(entry.name, options.excludeFilter)){
	            return false;
	        } else{
	            return true;
	        }
	    }
	    return true;
	},
	/**
	 * Comparator for directory entries sorting.
	 */
	compareEntryCaseSensitive : function (a, b, ignoreCase) {
	    if (a.stat.isDirectory() && b.stat.isFile()) {
	        return -1;
	    } else if (a.stat.isFile() && b.stat.isDirectory()) {
	        return 1;
	    } else {
	    	// http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp
	    	var str1 = a.name, str2 = b.name;
	    	return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
	    }
	},
	/**
	 * Comparator for directory entries sorting.
	 */
	compareEntryIgnoreCase : function (a, b, ignoreCase) {
	    if (a.stat.isDirectory() && b.stat.isFile()) {
	        return -1;
	    } else if (a.stat.isFile() && b.stat.isDirectory()) {
	        return 1;
	    } else {
	    	// http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp
	    	var str1 = a.name.toLowerCase(), str2 = b.name.toLowerCase();
	    	return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
	    }
	}
 
 
}