我想使用 javascript 对代码进行模块化

I want to modualrise code using javascript

我有一个文件,其中一个函数有很多代码。假设函数名称是 MessageHandler 并且在该函数中有很多 if else 条件和条件为真后要执行的代码。问题是我想使用 javascript 模块化该代码,以便它可以维护并且也很容易找到错误。可能为每个 logic.What 创建不同的文件是最好的方法

部分代码:

if (userMessage == "/start" && event.channel == "ibc") {
        quickReplies.content.text = metaData.introduction.english;
        quickReplies.msgid = "afterIntro";
        quickReplies.options = metaData.language.english;

        context.sendResponse(JSON.stringify(quickReplies));//First introduction message is sent in english language.

    }

        else if (userMessage == "hi" || userMessage == "hello" || userMessage == "help" || userMessage == "hii" || userMessage == "hey" || userMessage == "wassup") {
        // add filipino and gujarati condition....
        if (context.simpledb.roomleveldata.preferredLang == "hindi") {
            quickReplies.content.text = metaData.introduction.hindi;
            quickReplies.options = metaData.language.hindi;
        } 

    else if(context.simpledb.roomleveldata.preferredLang=="gujarati"){

                quickReplies.content.text = metaData.introduction.gujarati;
            quickReplies.options = metaData.language.gujarati
        }
            else if(context.simpledb.roomleveldata.preferredLang=="filipino"){

                quickReplies.content.text = metaData.introduction.filipino;
            quickReplies.options = metaData.language.filipino;
        }


        else {
            quickReplies.content.text = metaData.introduction.english;
            quickReplies.options = metaData.language.english;
        }
        quickReplies.msgid = "afterIntro";

        context.sendResponse(JSON.stringify(quickReplies));


    } else if (event.messageobj.refmsgid == "afterIntro" || (userMessage.indexOf("change category") > -1) || (userMessage.indexOf("श्रेणी बदलें") > -1) ||(userMessage.indexOf("કેટેગરી બદ") > -1) ||(userMessage.indexOf("baguhin ang kategorya") > -1)) {
        if (userMessage == "अंग्रेजी" || userMessage == "english" || userMessage == "ઇંગલિશ" ||userMessage=="ingles")
        {
            context.simpledb.roomleveldata.preferredLang = "english";
            context.console.log("Enter into english section");
            // context.simpledb.roomleveldata.optinFlag = '1';
            userMessage = "english";
        } else if (userMessage == "हिंदी" || userMessage == "hindi" || userMessage == "હિન્દી") 
        {
            context.simpledb.roomleveldata.preferredLang = "hindi";
            // context.simpledb.roomleveldata.optinFlag = '1';
            userMessage = "hindi";
        } else if (userMessage == "गुजराती" || userMessage == "gujarati" || userMessage == "ગુજરાતી"){
            context.simpledb.roomleveldata.preferredLang = "gujarati";
            // context.simpledb.roomleveldata.optinFlag = '1'ફિલિપિનો"
            userMessage = "gujarati";
        } else if (userMessage == "filipino" || userMessage == "फिलिपिनो" || userMessage == "ફિલિપિનો") {
            context.simpledb.roomleveldata.preferredLang = "filipino";
            // context.simpledb.roomleveldata.optinFlag = '1'ફિલિપિનો"

            userMessage = "filipino";
        } else {
            userMessage = context.simpledb.roomleveldata.preferredLang
            // context.simpledb.roomleveldata.optinFlag = '1';
        }
        var languageArray = metaData.language.english;
        for (var i = 0; i < languageArray.length; i++) {
            if (userMessage == metaData.language.english[i]) {
                console.log("language"+metaData.language.english[i]);
                quickReplies.content.text = metaData.introMessage[metaData.language.english[i].toString()];
                quickReplies.msgid = "afterLanguage";
                quickReplies.options = metaData[metaData.language.english[i].toString()];
                context.sendResponse(JSON.stringify(quickReplies));///Intro message and categories to choose from.
            }
        }
    } else if (userMessage == 'change language' || (userMessage.indexOf('hindi') > -1) || (userMessage.indexOf('english') > -1) || (userMessage.indexOf('gujarati') > -1)|| (userMessage.indexOf('filipino') > -1)) {
        var currentLanguage = context.simpledb.roomleveldata.preferredLang;
        quickReplies.content.text = metaData.introduction[currentLanguage + 'language'];
        quickReplies.options = metaData.language[currentLanguage];
        quickReplies.msgid = "afterIntro";
        context.console.log("currentLanguage=======" + currentLanguage);
        context.sendResponse(JSON.stringify(quickReplies));////give option to change the language in preferred language with prefereed messsage describing what to do.
    } else if (userMessage == 'powered by gupshup') {
        context.sendResponse("This chatbot is powered by gupshup.io- world's leading filipinoform." +
            " Join the bot bandwagon along with- VentureBeat, Infosys, Sage, ICICI etc. " +
            "to build captivating and powerful experiences for your users.");
    } else if (event.messageobj.refmsgid == "afterLanguage")
    // }else if(context.simpledb.roomleveldata.optinFlag == '1')
    {
        context.simpledb.roomleveldata.optinFlag = '0';
        var quoteType;
        var items = [];
        var currentLanguage = context.simpledb.roomleveldata.preferredLang;
        for (var i = 0; i < metaData[currentLanguage].length; i++) //["Motivational", "Love", "Life", "Funny", "Positive"]
        {
            quoteType = metaData[currentLanguage][i].toLowerCase().toString(); //["Motivational", "Love", "Life", "Funny", "Positive"]
            context.console.log(userMessage + "--------quoteType----" + quoteType);
            if (userMessage == quoteType) {
                context.console.log("Cureeeeentttt quote typeeeee"+userMessage);
                catalogue.msgid = quoteType;
                // if(currentLanguage == "filipino")
                // {
                //  quoteType = metaData['english'][i+1].toLowerCase().toString(); //
                // }else
                // {
                    quoteType = metaData['english'][i].toLowerCase().toString(); // 
                // }

                    quoteType=quoteType.replace(/ /g,"_");
                    context.console.log("QQQQQQQQQQQQUOTEEEEEEE"+quoteType);
                var imageURL = context.simpledb.botleveldata[quoteType + 'JSONObj'];
                context.console.log("imageURL--------" + imageURL);
                if(JSON.stringify(imageURL).indexOf(currentLanguage)>-1)
                {
                    for (var j = 0; j < imageURL.length; j++) // {["ImageURL:'',Language:''"]}
                    {
                        context.console.log(imageURL[j]['Language'].toString() + "---------currentLanguage" + currentLanguage);
                            if (imageURL[j]['Language'].toString() == currentLanguage) {
                                context.console.log("imageURL[j]['Language'].toString()==============" + imageURL[j]['Language'].toString())
                                if (j % 9 == 0 && j != 0) {
                                    items.push({
                                        "title": "For More Quotes",
                                        "imgurl": "https://s3.amazonaws.com/gs-bot-images/Quote-Mister/search.jpg",
                                        "options": [{
                                            "type": "text",
                                            "title": "Keep Looking"
                                        }]
                                    });
                                    break;
                                } else {
                                    items.push({
                                        "title": metaData.carousal[currentLanguage + "_title"],
                                        "imgurl": imageURL[j]['Image URL'].toString(),
                                        "options": [{
                                            "type": "url",
                                            "title": "Preview",
                                            "url": imageURL[j]['Image URL'].toString()
                                        }, {
                                            "type": "url",
                                            "title": metaData.carousal[currentLanguage + '_button_text'][0],
                                            "url": "https://www.gupshup.io/developer/bot/devNewImageQuotes/share?text=" + imageURL[j]['Image URL'].toString(),
                                            "webview_height_ratio": "compact"
                                        }, {
                                            "type": "url",
                                            "title": metaData.carousal[currentLanguage + '_button_text'][1],
                                            "url": "https://www.facebook.com/sharer/sharer.php?u=" + imageURL[j]['Image URL'].toString(),
                                            "webview_height_ratio": "compact"
                                        }]
                                    });
                                    context.simpledb.roomleveldata['index'] = j;
                                }
                            }
                    }

您可以像模块一样使用 patterns。 让你的代码更清晰,就像历史一样,把每一段代码放在一个函数中,并将这些函数保存在有意义的文件中,当你需要帮助时调用如下:

doActionOne()
doOtherAction(params)
isValidAction() // use is to validate true/false

这里有个著名的Design Pattern Book。 :)