Skip to content

Debug API

Options

_debug

Enable debug mode.

javascript
calc("a + b", { a: 1, b: 2, _debug: true });

_on_debug

Callback for structured debug info. Called regardless of _debug_console setting.

javascript
calc("a + b", {
  a: 1,
  b: 2,
  _debug: true,
  _on_debug: (info) => {
    console.log(info.result);
    // or send to logging service
  },
});

Global Config

javascript
import { set_config } from "a-calc";

// Disable console output
set_config({ _debug_console: false });

// Only enable in development
set_config({ _debug_console: process.env.NODE_ENV === "development" });

DebugInfo Structure

typescript
interface DebugInfo {
  expression: string;
  variables: Record<string, any>;
  steps: Array<{
    expression: string;
    substituted: string;
    result: string;
  }>;
  format: string | null;
  result: any;
  error?: string;
}

Console Output Format

┌──────────────────────────────────────────────────┐
│ [a-calc debug]                                   │
├──────────────────────────────────────────────────┤
│ Expression: price * qty                          │
│                                                  │
│ Variables:                                       │
│   price = 100                                    │
│   qty = 3                                        │
│                                                  │
│ Steps:                                           │
│   1. price * qty                                 │
│      → 100 * 3 = 300                             │
│                                                  │
│ Result: "300"                                    │
└──────────────────────────────────────────────────┘

Testing Usage

javascript
let debugInfo;
calc("a + b * c", {
  a: 1,
  b: 2,
  c: 3,
  _debug: true,
  _on_debug: (info) => {
    debugInfo = info;
  },
});

expect(debugInfo.result).toBe("7");

Released under the MIT License