Created
November 16, 2016 09:29
-
-
Save michel-zimmer/283f53263698f79c75bb6bb76a8641b4 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
#!/usr/bin/env node | |
const ignorecase = true; | |
const include = "abcdefghijklmnopqrstuvwxyz"; | |
function lpd(data) { | |
// prepare | |
let distribution = {}; | |
let sum = 0; | |
include.split('').forEach(c => distribution[c] = 0); | |
// ignore case | |
if (ignorecase) { | |
data = data.toLowerCase(); | |
} | |
// count | |
data.split('').forEach(c => { | |
if (include.indexOf(c) >= 0) { | |
distribution[c]++; | |
sum++; | |
} | |
}); | |
// calculate and round percentage | |
include.split('').forEach(c => distribution[c] = Math.round(distribution[c] * 10000 / sum) / 100); | |
return distribution; | |
} | |
// use stdin and stout when called directly | |
if (require.main === module) { | |
let data = ""; | |
process.openStdin().on('data', (chunk) => data += chunk).on('end', () => console.log(JSON.stringify(lpd(data), null, 2))); | |
} else { | |
// otherwise export as module | |
exports = lpd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment