I am currently creating a small app that automatically tweets portions of a book every day.
My book is inside a text file and I thus want to split the content of this text file into an array of 140-character-long strings.
I wanted to use a function such as split() but I am not getting good results as of now.
By the way, I have no specific separator between the strings I want to create.
I thought of counting the number of characters inside the text file and then defining the limit (ie. the number of splits) to have 140 character strings but I guess there must be a more intuitive function.
Any idea ?
Here is my actual code, test.txt linking to the book in text format.
var fs = require('fs');
var array = fs.readFileSync('./test.txt').toString().match("{1,140}");
for(i in array) {
console.log(array[i]);
}
Here is my new code thanks to your answer. The console is returning me strange numbers.
var fs = require('fs');
var book = fs.readFileSync('./test.txt');
var lastSplit; // position of the last split that you will cache
var limit = book.length > 140 ? 140 : book.length - lastSplit;
var urlsToAdd = book.slice(lastSplit, lastSplit + limit);
for(i in book) {
console.log(book[i]);
}
Thank you