CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-dustjs-helpers

Context helpers that extend the core Dust.js templating system with conditional logic, mathematical operations, and iteration utilities.

Overview
Eval results
Files

math-operations.mddocs/

Mathematical Operations

Mathematical operations helper that performs arithmetic calculations within templates with full precision control and error handling.

Capabilities

Math Helper

Performs mathematical operations on numeric values with optional rounding and conditional rendering support.

/**
 * Performs mathematical operations on numeric values
 * @param key - First operand value (required)
 * @param method - Mathematical operation to perform (required)
 * @param operand - Second operand value (required for binary operations)
 * @param round - If truthy, rounds the result to nearest integer
 */
{@math key="operand1" method="operation" operand="operand2" round="boolean"/}

// With conditional body (acts like @select helper)
{@math key="operand1" method="operation" operand="operand2"}
  {@eq value="expected"}Result matches expected value{/eq}
  {@gt value="threshold"}Result is above threshold{/gt}
{/math}

Supported Operations:

  • add: Addition (key + operand)
  • subtract: Subtraction (key - operand)
  • multiply: Multiplication (key * operand)
  • divide: Division (key / operand) - logs error if operand is 0
  • mod: Modulo (key % operand) - logs error if operand is 0
  • ceil: Ceiling (Math.ceil(key)) - operand not required
  • floor: Floor (Math.floor(key)) - operand not required
  • round: Round (Math.round(key)) - operand not required
  • abs: Absolute value (Math.abs(key)) - operand not required
  • toint: Convert to integer (parseInt(key, 10)) - operand not required

Usage Examples:

// Basic arithmetic operations
{@math key="15" method="add" operand="5"/}
<!-- Output: 20 -->

{@math key="100" method="divide" operand="3" round="true"/}
<!-- Output: 33 -->

// Using context variables
{@math key="{price}" method="multiply" operand="{quantity}"/}

// Single operand operations
{@math key="-42" method="abs"/}
<!-- Output: 42 -->

{@math key="3.7" method="floor"/}
<!-- Output: 3 -->

// With conditional body
{@math key="{score}" method="mod" operand="10"}
  {@eq value="0"}Score is divisible by 10{/eq}
  {@ne value="0"}Score has remainder: {.}{/ne}
{/math}

// Complex calculation with variables
{@math key="{$idx}" method="mod" operand="2"}
  {@eq value="0"}Even row{/eq}
  {@eq value="1"}Odd row{/eq}
{/math}

Error Handling:

  • Division by zero: Outputs NaN and logs error message "{@math}: Division by 0"
  • Missing key parameter: Logs error "{@math}: \key` or `method` was not provided"` and returns empty output
  • Missing method parameter: Logs error "{@math}: \key` or `method` was not provided"` and returns empty output
  • Invalid method: Logs error "{@math}: Method \methodName` is not supported"` and returns empty output
  • Non-numeric values: Converted using parseFloat() - may result in NaN output
  • Undefined context variables: Resolve to undefined, then convert to NaN

Type Coercion:

All key and operand values are processed as follows:

  1. Context variables are resolved using context.resolve()
  2. Resolved values are converted to numbers using parseFloat()
  3. Invalid numeric strings result in NaN
  4. Operations with NaN propagate NaN to the result

Logging:

All error messages are logged using dust.log(helperName, message, level) where:

  • helperName is "math"
  • message describes the specific error
  • level is "ERROR" for critical issues like missing parameters

Install with Tessl CLI

npx tessl i tessl/npm-dustjs-helpers

docs

conditional-logic.md

index.md

iteration-control.md

math-operations.md

utility-helpers.md

tile.json