filmplane filmplane - 8 months ago 26
Node.js Question

How do I trim down a composed Tweet when it exceeds 140 charcters?

I have a tweet function that removes @-mentions and will not tweet given the following conditions:
1) if question isn't same as the answer,
2) if composed tweet is greater than 140 characters and
3) if tweet is possibly sensitive.

Works fine but I would rather trim down the composed

if its length exceeds 140. Ideally, I want to chop off all but the first 137 characters and add "..." I am unsure of what best approach would be.

How would trimming down this tweet be achieved?

Here is the current code:

function(tweet) {

var question = tweet.txt;
var answer = tweet.txt + "some text";

if(question !== answer && answer.length < 140 && !tweet.possibly_sensitive) {
answer = answer.replace(/@/g, "."); //removes @-mentions.
return { id_str: tweet.id_str, text: answer };


You can remove the end of the answer string using slice() which is provided by the String Object in JavaScript.

// returns a new answer string containing the first 137 characters
// with '...' tacked onto the end
answer = answer.slice(0,137) + '...'; `