Javascript - 在NodeJS中构造帮助程序函数的最佳方法 [英] Javascript - Best way to structure helpers functions in NodeJS

查看:93
本文介绍了Javascript - 在NodeJS中构造帮助程序函数的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的NodeJS项目构建一组utils。
这些帮助器将包括:text utils(如子串,控制台日志等),以及更具体的帮助程序,如解析推文的文本。

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.

例如我想实现这个:

var helpers = require("helpers");

var Utils = new helpers.Utils();

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

正如您所看到的,我通过调用非常具体的方法和子方法来使用Utils来处理不同的事情。

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

//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

//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

//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?

我对Node很陌生,我想以正确的方式开始。

I am pretty new to Node and I want to start in the right way.

推荐答案

为了澄清我对你的帖子的理解,我看到两个问题:

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


  • 如何在文件中构建代码/方法,代表一类实用功能的文件

  • 如何将这些分类文件组织到一个更大的库中

在类别中构建方法

相反除了制作对象的所有类别特定函数方法(例如Twitter或Text)之外,您可以将函数导出到以它们命名的文件中。由于您似乎传递了要使用的数据,因此无需使用某些空类的函数实例方法。

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.

如果您的Twitter或Text的使用模式通常都有类变量,您希望保持状态,并且您希望实例化Text或Twitter对象以使用您的示例,那么我假设这是合适的。当我在我的项目中设置util libs时,它通常是一组构成模块的导出函数,而不是导出的javascript类。

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.

提供一个示例由基于文本的实用程序函数组成的text.js文件可能如下所示:

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
}

构建实用程序类别文件以制作更大的实用程序库

就组织实用程序而言ods,Luca的例子很好。我已经组织了类似这样的事情:

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

其中index.js的作用类似于

Where index.js does something like

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

exports.Text = textUtils;

然后当我想在我的节点API中使用util lib说一些User模型时,它只是:

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;

希望有所帮助。当我第一次学习时,查看流行的,备受推崇的库是非常有帮助的,看看更有经验的节点/ javascript开发者是如何做的。那里有很多好的(和坏的)!

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!

最好,

克雷格

这篇关于Javascript - 在NodeJS中构造帮助程序函数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆