javascript - Node js中解析编辑并保存js文件

标签 javascript node.js

我有一个 javascript 文件,其中包含一个对象数组。该数组还包含一些作为对象的函数。因此请记住,我不能使用 JSON 解析,因为它会返回错误。现在,我需要读取该 js 文件并访问该数组,并在该数组中添加一个函数作为对象,并将其保存到硬盘。有没有任何解析库可以帮助我解决这个问题以及如何帮助我?

这是js文件

let notifications = {
"Welcome": {
    title: "Welcome",
    body: "",
    icon: ""
}};

let script = {
"Start": [
    "notify Welcome",
    {
        "Input": {
            "Text": "What is your name?",
            "Validation": function (input) {
                return input.trim().length > 0;
            },
            "Save": function (input) {
                storage.player.Name = input;
                return true;
            },
            "Warning": "You must enter a name!"
        }
    },

    "h Hi {{player.Name}} Welcome!",

    "h Currently you have {{player.intelligence}} points of Intelligence but you seem far more intelligent, how about we add five points?",
    {"Function":{
        "Apply": function () {
            storage.player.intelligence += 5;
            return true;
        },

        "Reverse": function () {
            storage.player.intelligence -= 5;
        }
    }},
    "h There you have it, you now have {{player.intelligence}} points of Intelligence",

    {
        "Choice": {
            "Dialog": "h Have you already read some documentation?",
            "Yes": {
                "Text": "Yes",
                "Do": "jump Yes"
            },
            "No": {
                "Text": "No",
                "Do": "jump No"
            }
        }
    }
],

"Yes": [

    "h That's awesome!",
    "h Then you are ready to go ahead and create an amazing Game!",
    "h I can't wait to see what story you'll tell!",
    "end"
],

"No": [

    "h You can do it now.",

    "display message Help",

    "h Go ahead and create an amazing Game!",
    "h I can't wait to see what story you'll tell!",
    "end"
]};

现在我需要以编程方式在脚本数组中添加更多选择并保存。

最佳答案

您应该看看JSON-fn 。它可用于执行以下操作:

Javascript (both node.js and browser) plugin to stringify / parse / clone jsvascript objects with

  • Functions
  • RegExp
  • Date

关于javascript - Node js中解析编辑并保存js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49877408/

相关文章:

javascript - 使用 firebase 动态更改 img ng-repeat

javascript - 发送私有(private)消息 Discord-api

javascript - ejs/node.js 中的字符串连接错误

node.js - Lambda 函数错误 : EROFS: read-only file system, 打开 './tmp/test.zip' 进程在完成请求之前退出

javascript - 删除 var 的内容仍然失败

javascript - 如何阻止错误 'read ECONNRESET' 的发生

javascript - JavaScript 中的数字选择器

javascript - Chrome 扩展程序 : Capture URL of the newest tab opened

javascript - gremlin javascript 中的 gremlin 查询相当于什么?

javascript - 我怎样才能让底部的 console.log 等到它全部完成,然后告诉我答案而不是等待?