Created
November 8, 2016 11:21
-
-
Save aleksxor/820698620a7603020855f5bd9d41bb82 to your computer and use it in GitHub Desktop.
pipe demo
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 | |
let data = ''; | |
const args = process.argv.slice(2); | |
if (args.length) { | |
console.log(sum(args)); | |
process.exit(0); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
process.stdin.on('data', (chunk) => data = data + chunk); | |
process.stdin.on('end', () => { | |
console.log(sum(data.split(' '))); | |
process.exit(0); | |
}) | |
function sum(arr) { | |
return arr | |
.map(n => parseInt(n, 10)) | |
.reduce((acc, n) => acc + n, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment