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