Decreasing 64-bit Tweet ID in JavaScript

JavaScript is only able to handle integers up to 53-bit in size, here is a script to decrease tweet ID which is a 64-bit number in JavaScript without libraries or recursion, to use with max_id or since_id in Twitter API

As some of you might know, JavaScript is only able to handle integers up to 53-bit in size. This post, Working with large integers in JavaScript (which is a part of Numbers series) does a great job at explaining general concepts on dealing with large numbers in JS.

64-bit Tweet ID is "rounded" in JS
64-bit Tweet ID is “rounded” in JS

I had to do some research on the topic when I was re-writing some JavaScript code responsible for handling Twitter search in Storify editor: we had tweet duplicates in results! In this article, Working with Timelines, Twitter official documentation says:

Environments where a Tweet ID cannot be represented as an integer with 64 bits of precision (such as JavaScript) should skip this step.

So true, because id and id_str fields in a Twitter API response were different. Apparently, JavaScript engine just “rounds” inappropriately large numbers. :-( The task was complicated by the fact that I needed to subtract 1 from the last tweet’s ID to prevent its reappearance in a second search response. After the subtraction I could have easily passed the value to max_id parameter of Twitter API.

I’ve come across different solutions, but decided to write my own function which is simple to understand and not heavy on resources. Here is a script to decrease tweet ID which is a 64-bit number in JavaScript without libraries or recursion, to use with max_id or since_id in Twitter API:

function decStrNum (n) {
    n = n.toString();
    var result=n;
    var i=n.length-1;
    while (i>-1) {
      if (n[i]==="0") {
        result=result.substring(0,i)+"9"+result.substring(i+1);
        i --;
      }
      else {
        result=result.substring(0,i)+(parseInt(n[i],10)-1).toString()+result.substring(i+1);
        return result;
      }
    }
    return result;
}

To check if it works, you can run these logs:

console.log("290904187124985850");
console.log(decStrNum("290904187124985850"));
console.log("290904187124985851");
console.log(decStrNum("290904187124985851"));
console.log("290904187124985800");
console.log(decStrNum("290904187124985800"));
console.log("000000000000000001");
console.log(decStrNum("0000000000000000001"));

Alternative solution which I’ve found in a StackOverflow question was suggested by Bob Lauer, but it involves recursion and IMHO is more complicated:

function decrementHugeNumberBy1(n) {
    // make sure s is a string, as we can't do math on numbers over a certain size
    n = n.toString();
    var allButLast = n.substr(0, n.length - 1);
    var lastNumber = n.substr(n.length - 1);

    if (lastNumber === "0") {
        return decrementHugeNumberBy1(allButLast) + "9";
    }
    else {      
        var finalResult = allButLast + (parseInt(lastNumber, 10) - 1).toString();
        return trimLeft(finalResult, "0");
    }
}

function trimLeft(s, c) {
    var i = 0;
    while (i < s.length && s[i] === c) {
        i++;
    }

    return s.substring(i);
}

Now, if you’re the type of person who likes to shoot sparrows with a howitzer, there are full-blown libraries to handle operations on large numbers in JavaScript; just to name a few: BigInteger, js-numbers and javascript-bignum.

My Social Media Policies

Social media is developing rapidly. Only 5–10 years ago our lives and communications didn’t spin around Facebook, Twitter, Instagram and Path. I consider myself being somewhere in the middle of the adoption curve because two years ago I had less than 200 Facebook friends and no Twitter or Instagram accounts. Nevertheless, I find social networks greatly enhancing my life and allowing me to communicate better and more efficient with the people who are on these networks.

Social media is developing rapidly. Only 5–10 years ago our lives and communications didn’t spin around Facebook, Twitter, Instagram and Path. I consider myself being somewhere in the middle of the adoption curve because two years ago I had less than 200 Facebook friends and no Twitter or Instagram accounts. Nevertheless, I find social networks greatly enhancing my life and allowing me to communicate better and more efficient with the people who are on these networks.

Social Media
Social Media

Facebook

I request to be friends with any person I’ve met in real life. This is my way of introducing myself. I rarely friend people I’ve never met in real life.

Twitter

I follow all my friends and any interesting company or person.

Instagram

I post my own photos and almost none of them depict any of my friends.

Google Plus

I put in my circles and follow all of my friends and some of the people with whom we’ve exchanged emails.

LinkedIn

I connect to anybody with whom we’ve exchanged communication means.

Foursquare and Path

I follow my real friends and don’t let strangers for geosecurity reasons.

Pinterest

I follow random people mainly based on contents of their boards.

Here, in the Silicon Valley and San Francisco, it’s often easy to forget that there’re some people who are not on social networks yet. Saying that, I think it would be a hard thing for me to be a real friend with somebody who doesn’t have a Facebook account — it’s just weird!