roll-parser - v3.4.0
    Preparing search index...

    Function evaluate

    • Evaluates a parsed AST against an RNG and returns the roll result.

      The second half of the pipeline — roll is evaluate(parse(...)). Call it directly to reuse one AST across many rolls, or to drive a hand-built AST.

      Unlike roll, the RNG is required: evaluate never invents a randomness source, so a caller can never accidentally get an unseeded roll.

      Parameters

      • ast: ASTNode

        The AST to evaluate, from parse or hand-built

      • rng: RNG

        Randomness source; one nextInt call per die

      • options: EvaluateOptions = {}

        Evaluation limits plus the original notation string, which the AST cannot supply

      Returns RollResult

      Complete RollResult

      On a limit breach, division by zero, an undefined variable, or a non-finite total

      INVALID_EVALUATION_LIMIT when a supplied limit is not an integer in range — raised before any die is rolled

      import { evaluate, parse } from 'roll-parser';
      import { createMockRng } from 'roll-parser/testing';

      const ast = parse('2d6+3');
      const result = evaluate(ast, createMockRng([4, 2]), { notation: '2d6+3' });
      result.total; // 9
      result.rendered; // '2d6[4, 2] + 3 = 9'

      Omitting notation falls back to the normalized expression, which is reconstructed from the AST — so RollResult.notation is always a string, just not necessarily the one the user typed.