0

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

Marwann
  • 163
  • 3
  • 14
  • Possible duplicate of http://stackoverflow.com/questions/7033639/javascript-split-large-string-in-n-size-chunks – juvian Mar 12 '15 at 16:00
  • If a sentence is longer then 140 chars, what do you want to do with it? Do you mind tweeting half a sentence? – javinor Mar 12 '15 at 16:09
  • Juvian, thanks for linking. Interesting but not totally answering my question. Javinor, as of now I don't mind as I want something functional fast, but this is an issue I would like to correct afterwards. Thanks – Marwann Mar 12 '15 at 16:11

1 Answers1

0

what you can do instead of splitting the whole book at once in n splits with each consists of 140 characters is to keep in mind the last position of the last split which is also the total number of splits done before.

var lastSplit; // position of the last split that you will cache
var limit   = book.length - lastSplit > 140 ? 140 : book.length - lastSplit;
var urlsToAdd = book.slice(lastSplit, lastSplit + limit);
  • so the lastSplit will hold the position of the last split and also indicates how many character have been also tweeted.
  • Now you need to check how many characters you will tweet just to be sure you will not exceed the book characters length
  • You do the split from the last known position by the number of characters you wish which is either lastSplit + 140 or the books length if you near the end

Of course this logic is very dummy, meaning that you don't care if you stop the characters in the middle of the word or the sentence .. etc.

Update: The code i provided is a general thought process, it is not necessarily copy/paste thing. However, from the code you pasted after, you can try:

var fs = require('fs');
var book = fs.readFileSync('./test.txt');
var lastSplit = 0;

function tweet(book) {
var limit   = book.length - lastSplit > 140 ? 140 : book.length - lastSplit;
var tweet   = book.slice(lastSplit, lastSplit + limit);
lastSplit += limit;
return tweet;
}

while (lastSplit < book.length) {
 console.log(tweet(book));
}

Updated II: Working Code With dummy Book (to convince you that it works !)

function tweet(book) {
 var  lastSplit = 0;
 while (lastSplit < book.length) {
  var limit   = book.length - lastSplit > 140 ? 140 : book.length - lastSplit;
  var tweet   = book.slice(lastSplit, lastSplit + limit);
  lastSplit += limit;
  $('body').append('<p>' + tweet + '</p>');
 }
}

// this will generate a dummy string containing numbers from 0-800
tweet(Array.apply(null, {length: 800}).map(Number.call, Number).join(''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
  • Thanks. I updated my code in my post but the console is now giving me strange results, like random numbers and [Function]. – Marwann Mar 12 '15 at 16:18
  • @Marwann the code usually is not a direct copy/paste .. it should help you in the process. However, i updated my answer. If you provide a JsFiddle that will be better so that i can try things out – AhmadAssaf Mar 12 '15 at 16:37
  • Thanks. I'll do so. As of your code it doesn't seem to work put I'll provide a jsfiddle ! – Marwann Mar 12 '15 at 19:56
  • the code works fine, just tested it out in the console – AhmadAssaf Mar 13 '15 at 00:44