Last active
March 5, 2018 17:08
-
-
Save JogoShugh/df998faabdc13be58a01598fcbd84c26 to your computer and use it in GitHub Desktop.
Tally By File Extension CommitStream.js
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
function tally(root, prop) { | |
if (!root[prop]) root[prop] = 0; | |
root[prop]++; | |
} | |
function hasItems(array) { | |
return array && array.length > 0; | |
} | |
function tallyBy(state, key, length) { | |
state.summary[key] += length; | |
} | |
function tallyByFileExtensions(fileExtensions, array) { | |
array.forEach(function(item) { | |
var extension = getFileExtension(item); | |
if (extension) tally(fileExtensions, extension); | |
}); | |
} | |
function getFileExtension(item) { | |
var lastDot = item.lastIndexOf('.'); | |
if (lastDot < 0) return false; | |
var extension = item.substring(lastDot + 1); | |
return extension; | |
} | |
fromStream("$et-GitHubCommitReceived") | |
.when({ | |
$init: function() { | |
return { | |
touched: 0, | |
summary: { | |
modified: 0, | |
added: 0, | |
removed: 0 | |
}, | |
fileExtensions: {} | |
} | |
}, | |
GitHubCommitReceived: function(s, e) { | |
var original = e.data.originalMessage; | |
if (!original) return; | |
var options = { | |
modified : original.modified, | |
added : original.added, | |
removed : original.removed | |
}; | |
for(var key in options){ | |
var items = options[key]; | |
if (hasItems(items)) { | |
tallyBy(s, key, items.length); | |
tallyByFileExtensions(s.fileExtensions, items); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment