JavaScript and Bridge modules

If you are in this industry as a developer, you probably constantly have to deal with changing 3rd party dependencies for many reasons. I would like to share a strategy I have been using could benefit your process as well. So in this case I will use momentJS as an example but this can be any big or small library you are using in your application and time to time for some reason you need to change to something else. And this is not a new thing or I invented. So don't roast me if I am doing all wrong or you don't think it is a good practice or something. It just helps me to organize things better in my projects and make change less headache.

So Let's say we have momentJs fully integrated in our project. We calculate local times, utc timestamps, display durations and differences in many places and have momentJS hugged our entire project like a spider web. Then the day comes, management says, we need to get rid of momentJS because it is huge ( size-wise ) we need something similar but smaller footprint. How would you approach? You start searching for every instance of moment( and moment. and start implementing new library methods and thinking gosh, why don't you just use same method names and structure so I don't have to rewrite all these stuff. And once you are all done, minds are changed again, moment was big but better, let's go back.. :)

If you are at that stage, change is either way very painful and I can't make it easier for you. But if you are at the first stages of your project, or have to change it now, with a little effort you can make life easier for yourself for the future. The trick is using a local bridge module ( I come up with this name but if there is a common name out there feel free to let me know )

So bridge module is simply a router in between your code and library translating your methods to library's methods or properties. So when you need to change something, all you need to edit your bridge module.

So for instance create a library like this one, and map the methods you use from moment ,

import moment from 'moment'
class DateBridge {
        constructor(){
           return moment();
        }
        now() {
            return moment.now();
        },
        diff(date1, date2, unitOfTime='seconds') {
           return moment(date1).diff(moment(date2), unitOfTime);
       },
       ..........
};

export default new DateBridge();

Why are we exporting a new instance? This is something I found out that, exporting a new instance of a class will be persisted. Meaning, it doesn't mean every time you import you will get a new instance, but you will get same instance of the class. Which is pretty cool since now we can just call DateBridge.now() and will get class instance.

Once you are done with your bridge module, now you can replace all your original moment calls in your code with bridge version. When it comes to use datejs instead of momentJs, all you need to do is update your bridge module with corresponding new methods of new library and you are good to go.

PS: I would not suggest using bridge module as extending of another class since it will bring up more complexity when it comes to change. The main point of bridge module is a gateway or a router and it should be its own so changing the relative module should be slightly easy to implement rather than overriding inherited methods or props since new library may not have those to be overridden.