Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to structure helpers functions in NodeJS

I am trying to build a set of utils for my NodeJS project. These helpers will include: text utils (like substringing, console logging etc.), and more specific helpers like parsing the text of a tweet.

So I am trying to divide the module in different files and with a very clear idea of what each thing is meant to do.

For example I would like to achieve this:

var helpers = require("helpers");

var Utils = new helpers.Utils();

// working with text
Utils.text.cleanText("blahblalh");
// working with a tweet
Utils.twitter.parseTweet(tweet);

As you can see I am using Utils for different things, by calling very specific methods and sub methods.

I tried to understand how inheritance works here but I got a little bit lost.

This is what I am doing (some rough sample code):

//node_modules/helpers/index.js

var Text = require('./text');
var Twitter = require('./twitter');

function Utils() {

}

Utils.prototype.text = {
    cleanText: function(text) {
        Text.cleanText(text);
    }
};

Utils.prototype.twitter = {
    parseTweet(tweet) {
        Twitter.parseTweet(tweet);
    }
};

//node_modules/helpers/text.js

function Text() {

}

Text.prototype.cleanText = function(text) {
    if (typeof text !== 'undefined') {
        return text.replace(/(\r\n|\n|\r)/gm,"");
    }
    return null;
};

module.exports = Text;

//node_modules/helpers/twitter.js

function Twitter() {

};

Twitter.prototype.parseTweet = function(data) {
    return data;
};

module.exports = Twitter

Is this a correct way? Am I doing something wrong or that could slow down the performances, etc?

like image 309
Anonymous Avatar asked May 16 '15 09:05

Anonymous


People also ask

Is Express good for large projects?

Middleware frameworks, like Express. js, are suitable for small and medium projects. If you are going to develop a large project that will be supported by a large team of developers, Express. js is not the best choice.

What is helpers in node JS?

The node helper ( node_helper. js ) is a Node. js script that is able to do some backend task to support your module. For every module type, only one node helper instance will be created. For example: if your MagicMirror uses two calendar modules, there will be only one calendar node helper instantiated.


2 Answers

To clarify how I'm understanding your post, I see two questions:

  • How do I structure code/methods within files, files that represent a category of utility functions
  • How do I organize the those categorical files into one larger library

Structuring methods within a category

Rather than making all of the category specific functions methods of objects (e.g. Twitter or Text), you could just export the functions in files named after them. Since it seems you are passing in the data you want to use, there is no need to make the functions instance methods of some empty class.

If your usage patterns of Twitter or Text usually have class variables you want to keep state on, and you want to instantiate Text or Twitter objects to use your examples, then I suppose that would be appropriate. When I setup util libs in my projects it usually is a bunch of exported functions that make up a module, rather than an exported javascript class.

To provide an example of what a text.js file made up of text-based utility functions might look like:

module.exports = {
    cleanText:function(text) {
        // clean it and return
    },

    isWithinRange(text, min, max) {
        // check if text is between min and max length
    }
}

Alternatively, you could do it this way:

exports.cleanText = function(text) {
    // clean it and return
}

exports.isWithinRange = function (text, min, max) {
    // check if text is between min and max length
}

Structuring utility category files to make a larger utility library

As far as organizing the utility methods, Luca's example is nice. I've organized some similarly like this:

utils-module/
    lib/
        text.js  <-- this is the example file shown above
        twitter.js
    test/
    index.js

Where index.js does something like

var textUtils = require('./lib/text');

exports.Text = textUtils;

Then when I want to use the util lib in say some User model in my node API, it's simply:

/*
 * Dependencies
 */
var textUtils = require('path/to/lib').Text;

/*
 * Model
 */
function User() {}

/*
 * Instance Methods
 */
User.prototype.setBio = function(data) {
    this.bio = textUtils.cleanText(data);
}

module.exports = User;

Hope that helps. When I was first learning it was very helpful to look at popular, well-respected libraries to see how more experienced node/javascript devs were doing things. There are so many good (and bad) ones out there!

like image 82
cpentra1 Avatar answered Oct 07 '22 14:10

cpentra1


You can see a utils library example with lodash.

Lodash is an utility lib like underscorejs. This library have file sustem structure like your.

It divides the functions in categories. Each category is a folder with an index.js file that includes into a namespace (literal object) each functions for that category!

Lodash/
   Objects/
       Function1.js
       Functions2.js
       ....
       Index.js
   Array/
       Function1.js
       ...
       Index.js

Then in your code you can do this:

var objectsUtils = require("lodash/objects");
var foreach = require("lodash/array/each");

You can create a similar file system structure in order to have more flexibility. You can require the entire lib, only one namespace or a single function.

This is better for performance, because you use only what you need and have a memory usage gain.

like image 25
Luca Colonnello Avatar answered Oct 07 '22 14:10

Luca Colonnello