All files / src/ast/collection selection.ts

96.49% Statements 55/57
87.87% Branches 29/33
100% Functions 7/7
98.03% Lines 50/51

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114            22x   22x   22x   22x                             37x 37x 37x       1x       1x       32x   32x 2x 2x       29x   29x 8x 19x 19x 19x 19x 7x     19x     1x     21x 6x 6x 6x 22x 22x 22x 22x 12x 12x     22x     6x       15x 15x 47x 47x 47x 47x 29x     47x     15x       29x 4x 3x 1x         3x 3x      
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';
 
/** Selection mode: controls which matching elements are returned */
export const enum SelectMode {
  /** .?[predicate] — return all matching elements */
  ALL = 'all',
  /** .^[predicate] or .$[predicate] — return first matching element */
  FIRST = 'first',
  /** .*[predicate] — return last matching element */
  LAST = 'last',
}
 
export class Selection extends SpelNodeImpl {
  private readonly nullSafe: boolean;
  private readonly mode: SelectMode;
 
  constructor(
    startPos: number,
    endPos: number,
    nullSafe: boolean,
    target: SpelNodeImpl,
    predicate: SpelNodeImpl,
    mode: SelectMode = SelectMode.ALL,
  ) {
    super(NodeType.SELECTION, startPos, endPos, target, predicate);
    this.nullSafe = nullSafe;
    this.mode = mode;
  }
 
  public getSelectMode(): SelectMode {
    return this.mode;
  }
 
  public isNullSafe(): boolean {
    return this.nullSafe;
  }
 
  public getValueInternal(state: ExpressionState): TypedValue {
    const targetValue = this.children[0]!.getValue(state).getValue();
 
    if (targetValue === null || targetValue === undefined) {
      Iif (this.nullSafe) return TypedValue.NULL;
      Eif (this.mode === SelectMode.ALL) return new TypedValue([]);
      return TypedValue.NULL;
    }
 
    const collection = this.toCollection(targetValue);
 
    if (this.mode === SelectMode.FIRST) {
      for (const item of collection) {
        state.pushHeadIndex(new TypedValue(item));
        try {
          const match = this.children[1]!.getValue(state).getValue();
          if (match) {
            return new TypedValue(item);
          }
        } finally {
          state.popHeadIndex();
        }
      }
      return TypedValue.NULL;
    }
 
    if (this.mode === SelectMode.LAST) {
      let lastMatch: unknown = null;
      let found = false;
      for (const item of collection) {
        state.pushHeadIndex(new TypedValue(item));
        try {
          const match = this.children[1]!.getValue(state).getValue();
          if (match) {
            lastMatch = item;
            found = true;
          }
        } finally {
          state.popHeadIndex();
        }
      }
      return found ? new TypedValue(lastMatch) : TypedValue.NULL;
    }
 
    // SelectMode.ALL
    const result: unknown[] = [];
    for (const item of collection) {
      state.pushHeadIndex(new TypedValue(item));
      try {
        const match = this.children[1]!.getValue(state).getValue();
        if (match) {
          result.push(item);
        }
      } finally {
        state.popHeadIndex();
      }
    }
    return new TypedValue(result);
  }
 
  private toCollection(value: unknown): unknown[] {
    if (Array.isArray(value)) return value;
    if (value instanceof Set) return [...value];
    if (value instanceof Map) return [...value.values()];
    return [value];
  }
 
  public toStringAST(): string {
    const prefix =
      this.mode === SelectMode.FIRST ? '.^[' : this.mode === SelectMode.LAST ? '.*[' : '.?[';
    return `${this.children[0]!.toStringAST()}${prefix}${this.children[1]!.toStringAST()}]`;
  }
}