Skip to content

Instantly share code, notes, and snippets.

@dimsemenov
Created October 21, 2016 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimsemenov/c86cfb38051055a9f3a4e0535489bcb4 to your computer and use it in GitHub Desktop.
Save dimsemenov/c86cfb38051055a9f3a4e0535489bcb4 to your computer and use it in GitHub Desktop.
Counts words & sorts them by number of occurences
var fileToTest = grunt.file.read('somefile.js');
var vars = fileToTest.match(/(\w+)/g);
var i = vars.length;
var count;
var parsedVars = [];
var maxWordLen = 3;
while(i--) {
var word = vars[i];
if(word !== undefined) {
count = 0;
vars = vars.filter(function (a){
if(a === word) {
count++;
}
return a !== word;
});
// only words with length more than 3
if(word.length > maxWordLen) {
parsedVars.push({
word: word,
count: count
});
}
}
}
var sortedVars = parsedVars.sort(function (a, b) {
return b.count - a.count;
});
sortedVars.forEach(function(item) {
grunt.log.writeln(item.word + ' ' + item.count);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment