Created
October 21, 2016 08:48
-
-
Save dimsemenov/c86cfb38051055a9f3a4e0535489bcb4 to your computer and use it in GitHub Desktop.
Counts words & sorts them by number of occurences
This file contains hidden or 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
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