All files / src/ast/operator range-operator.ts

100% Statements 26/26
100% Branches 7/7
100% Functions 3/3
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  1x 1x 1x             1x 1x 6x 6x   1x 5x 5x   5x 5x 4x 12x 12x 5x 1x 5x 5x 1x 5x 5x   1x 1x 1x 1x  
import type { ExpressionState } from '../../expression-state.js';
import { TypedValue } from '../../typed-value.js';
import { SpelNodeImpl } from '../spel-node.js';
import { NodeType } from '../../language/node-type.js';
 
/**
 * Range operator `..` — parallels Spring OpRange
 *
 * a..b generates an integer sequence from a to b (inclusive)
 */
export class RangeOperator extends SpelNodeImpl {
  constructor(startPos: number, endPos: number, left: SpelNodeImpl, right: SpelNodeImpl) {
    super(NodeType.RANGE_OPERATOR, startPos, endPos, left, right);
  }
 
  public getValueInternal(state: ExpressionState): TypedValue {
    const start = Number(this.children[0]!.getValue(state).getValue());
    const end = Number(this.children[1]!.getValue(state).getValue());
 
    const result: number[] = [];
    if (start <= end) {
      for (let i = start; i <= end; i++) {
        result.push(i);
      }
    } else {
      for (let i = start; i >= end; i--) {
        result.push(i);
      }
    }
    return new TypedValue(result);
  }
 
  public toStringAST(): string {
    return `(${this.children[0]!.toStringAST()}..${this.children[1]!.toStringAST()})`;
  }
}