const CAMLIS_VARIABLE_FORMAT = {
    TEST: /^T{1}\d*$/g
};

function calculateTestValue(formula) {
    CAMLIS_VARIABLE_FORMAT.TEST.lastIndex = 0;

    if (formula) {
        var parser = new exprEval.Parser();
        var expr   = parser.parse(formula);
        var variables = expr.variables();
        var map_values = {};

        for(var i in variables) {
            if (CAMLIS_VARIABLE_FORMAT.TEST.test(variables[i])) {
                var sample_test_id = variables[i].replace('T', 'result-');
                var value = $("#" + sample_test_id).val() || '';
                if (value.length > 0) map_values[variables[i]] = parseFloat(value || 0);
            }

            CAMLIS_VARIABLE_FORMAT.TEST.lastIndex = 0;
        }

        if (variables.length > _.values(map_values).length) return null;

        return expr.evaluate(map_values);
    }

    return 0;
}
