1

I have a javascript file which contains an array of objects. That array also contains some functions as objects. So keep in mind I can't use JSON parsing as it will return an error. Now I need to read that js file and access that array and add one more function as an object in that array and save it to a hard disk. Is there any parsing library which can help me with that and how?

Here is the js file

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"
]};

Now I need to add some more choices in scripts array programmatically and save it.

1 Answer 1

1

You should take a look at JSON-fn. It can be used to do the following:

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

  • Functions
  • RegExp
  • Date
Sign up to request clarification or add additional context in comments.

1 Comment

This is a handy tool indeed. But it doesn't help me to parse full js files a d get individual variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.