matheus-rodrigues00 / utils

✨ Library to commonly used cross-projects utilities methods ✨
17 stars 11 forks source link

Method to Evaluate String and Throw Callback Error #2

Open matheus-rodrigues00 opened 1 year ago

matheus-rodrigues00 commented 1 year ago

The method will try to run the built-in javascript eval() in a string and if the string has some evaluation error it will throw a callback function passed also by the parameters. So it must be in a try catch. The method goes like below:

function tryEval(string_code: string, callback?: (error: Error) => void): any {
    try {
        return eval(string_code);
    } catch (e) {
        if (callback) {
            callback(e);
        } else {
            throw e;
        }
    }
}

Make sure to write some tests.