Anonymous
Anonymous

Reputation: 1031

NodeJS - Export multiple functions

I am trying to build my first NodeJS module. This is what I am doing:

var text = function(){
    this.padRight = function(width, string, padding){
        return (width <= string.length) ? string : this.padRight(width, string + padding, padding);
    };
    this.cleanText = function(text){
        if (typeof text !== 'undefined') {
            return text.replace(/(\r\n|\n|\r)/gm,"");
        }
        return null;
    };
    this.printOut = function(outputObj){
        var module = this,
            output = "";

        outputObj.forEach(function(obj){
            switch(obj.type){
                case "date" :
                    var date = obj.contents;
                    if(typeof date != "undefined") output += date.toString().substring(4, 25) + "\t";
                    break;
                case "string":
                    var string = obj.contents;
                    if(typeof string != "undefined"){
                        string = module.cleanText(string);
                        if(typeof obj.substring != "undefined" && obj.substring != 0) {
                            string = string.substring(0, obj.substring);
                        }
                        if(typeof obj.padRight != "undefined" && obj.padRight != 0) {
                            string = module.padRight(15, string, " ");
                        }
                        output += string + "\t";
                    }
                    break;
            }
        });
        console.log(output);
    };
};

module.exports.text = text;

I am trying to have different kind of helpers, so I want to be able to call this module like this:

require("helpers");    
helpers.text.printOut();

But I am getting an error.

How do I export different functions in the same module and call them individually?

Thanks

Upvotes: 1

Views: 1442

Answers (2)

mauritslamers
mauritslamers

Reputation: 527

Your code is somewhat confusing, as you are defining a constructor function, which you are exporting (as James identified). It is confusing because it is customary in JS to write constructors with a capital.

I would suggest though a different solution than James', which is to not export a new Text(), but to export the constructor itself. In the module where you need this object, import this module as var Text = require('./text'); and do the new Text() part. Exporting the new Text() has as disadvantage that you effectively created a singleton, and this might or might not be your intention. Keep in mind that require() on a module is effectively only executed once, and when a different module loads the module you described above, they are the same object.

Upvotes: 1

James
James

Reputation: 82096

The problem is text is a function in itself, it looks to me like you want to be exporting an instance of text rather than the function itself i.e.

module.exports.text = new text();

Upvotes: 1

Related Questions