Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

javascript - 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(/(
|
|
)/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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...