Skip to content

Instantly share code, notes, and snippets.

@zonak
Created April 11, 2012 15:41
Show Gist options
  • Save zonak/2360109 to your computer and use it in GitHub Desktop.
Save zonak/2360109 to your computer and use it in GitHub Desktop.
grunt helper for finding the longest common path
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