Best way to structure helpers functions in NodeJS

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!


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.