Last active
December 26, 2015 12:58
-
-
Save dariusk/7154705 to your computer and use it in GitHub Desktop.
Read a text file and log to console in 140 character chunks, maintaining whole words. In NodeJS; requires the "line-by-line" module.
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 fs = require('fs'); | |
var lbl = require('line-by-line'), | |
lr = new lbl('ulysses.txt'); | |
var buffer = ''; | |
function flatten(arr) { | |
var result = []; | |
result = result.concat.apply(result, arr); | |
return result; | |
} | |
lr.on('error', function (err) { | |
// 'err' contains error object | |
console.log('Error!', err); | |
}); | |
lr.on('line', function (line) { | |
// pause emitting of lines... | |
lr.pause(); | |
// ...do your asynchronous line processing.. | |
var tweet = ''; | |
buffer += ' ' + line; | |
if (buffer.length > 140) { | |
tweet = buffer.substr(0,140).replace(/\s\w+$/,'').trim(); | |
buffer = buffer.replace(tweet,''); | |
} | |
console.log(tweet); | |
setTimeout(function () { | |
// ...and continue emitting lines every 2 minutes | |
lr.resume(); | |
}, 1000 * 60 * 2); | |
}); | |
lr.on('end', function () { | |
// All lines are read, file is closed now. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment