Last active
May 16, 2016 14:45
-
-
Save jbreckmckye/1a46f550ad1209ab00d2da0d0a363933 to your computer and use it in GitHub Desktop.
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
var myFileName = 'commercial.js'; | |
var fs = require('fs'); | |
var input = fs.createReadStream(myFileName); | |
var text = ''; | |
input.on('data', function (data) { | |
text += data; | |
}); | |
input.on('end', function () { | |
var defineBlock = /define\(["'][^"']*["']/g; | |
var keys = text.match(defineBlock); | |
var bodies = text.split(defineBlock); | |
bodies.shift(); | |
var blocks = keys.map(function (key, index) { | |
return { | |
key : key.split('define(')[1], | |
body : bodies[index], | |
length : bodies[index].length | |
}; | |
}); | |
var blocksBySize = blocks.sort(function (a, b) { | |
if (a.length > b.length) { | |
return 1; | |
} | |
if (a.length < b.length) { | |
return -1; | |
} | |
return 0; | |
}).reverse(); | |
console.log(blocksBySize.map(function (block) { | |
return block.length + ' chars in ' + block.key; | |
}).join('\n')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment