All files / src/ast/collection selection.ts

97.84% Statements 91/93
88.88% Branches 32/36
100% Functions 7/7
97.84% Lines 91/93

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  1x 1x 1x     1x   1x   1x   1x     1x 18x 18x   18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x 1x 1x   18x 1x 1x   18x 13x   13x 1x 1x       12x   12x 3x 9x 9x 9x 9x 2x 2x 9x 9x 9x 9x 1x 1x   12x 3x 3x 3x 12x 12x 12x 12x 5x 5x 5x 12x 12x 12x 12x 3x 3x     6x 12x 21x 21x 21x 21x 14x 14x 21x 21x 21x 21x 6x 13x   18x 12x 6x 6x 1x 12x   18x 3x 3x 3x 3x 18x  
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) {
      if (this.nullSafe) return TypedValue.NULL;
      if (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()}]`;
  }
}