Created
April 11, 2012 15:41
-
-
Save zonak/2360109 to your computer and use it in GitHub Desktop.
grunt helper for finding the longest common path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.registerHelper('commonPath', function(inFiles) { | |
var results = [], | |
minLen = 99, | |
isValid, | |
dirs; | |
dirs = inFiles.map(function(filepath) { | |
var tmpArr = path.dirname(filepath).split('/'); | |
if(tmpArr.length < minLen) { | |
minLen = tmpArr.length; | |
}; | |
return tmpArr; | |
}); | |
if (dirs.length) { | |
for (var _j = 0; _j < minLen; _j++) { | |
isValid = true; | |
for (var _i = 1, _len = dirs.length; _i < _len; _i++) { | |
if(dirs[0][_j] !== dirs[_i][_j]) { | |
isValid = false; | |
break; | |
} | |
} | |
if(isValid) { | |
results.push(dirs[0][_j]); | |
} else { | |
break; | |
} | |
} | |
}; | |
return path.join(results.join('/')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment