Search code examples
javascriptnode.jsmoduleextending

extending a module when module is using "module.exports"


I've read a few pages on extending a module.They revolve around using a functional form of a module and I get how to do it (from https://toddmotto.com/mastering-the-module-pattern/)

var Module = (function () {

  return {
    publicMethod: function () {
      // code
    }
  };

})();

but what I have is two modules like this

util.js

module.exports = {
    thing1: function() {// do thing1 stuff }      
}

extend.js a package I can't change (from npm)

module.exports = {
    thing2: function() {// do thing2 one stuff}
}

now pretending I am going to use my util.js module

const _ = require('util.js);

let catin = _.thing1;  // that's easy
let thehat = _.thing2;.  // this was util.js extended.

I could in util.js just do this.

const ex = require('extend.js')

module.exports = {
    thing1: function() {// do thing1 stuff }
    thing2: ex.thing2
}

and that's ok since extend.js only has one function/method to extend, but I would like to extend this into my util library https://github.com/dodekeract/bitwise/blob/master/index.js but it has 22! items to extend.

There must be a better slicker way yes?

I'm open to refactoring my util.js file (but not hand coding each extension like I showed) so it extends automatically but obviously can't refactor that package I'm not maintaining, short of a fork...ugh. Also not interested in adding a sub deal like

  ex: ex 

_.ex.thing2  

Ideas?


Solution

  • So given Molda's hint I'll share what I put together to make this question more useful for others. I put together a simple way of building a (utility) module from a folder of (utility) modules plus other one off packages (e.g. bitwise)

    Make a utils.js module in say lib/ with this (you'll need require-all or some such package)

    let utils = require('require-all')(__dirname + '/util');
    let bw = require('bitwise');
    
    let self = module.exports = (function(){
    let util={};
    for (var key in utils) {
       util = utils.object.merge_keys(util,utils[key])
     }
     util = utils.object.merge_keys(util,bw)
     return util;
     }());
    

    now make a subdirectory lib/util/ and fill it with your utility modules. Have one of those modules contain this key/function

    merge_keys: function (obj1,obj2){
        var obj3 = {};
        for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
        for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
        return obj3;
    }
    

    and be sure that module name matches the key used in this line util = utils.object.merge_keys(util,utils[key]). In my case I have a module object.js in lib/util/ containing merge_keys

    Then just require the utils.js module and all will be as one including access to the merge_keys function for other merging :-).

    let _ = require('./lib/utils');
    // see if it worked
    console.log(_);
    

    beware: there is no checking for duplicate key names between modules

    notes:
    let self= allows one to refer to any other key within the merged object itself like self.myfunctionkeyname( )