All files / json-patch/op OpMatches.ts

90.47% Statements 19/21
41.66% Branches 5/12
100% Functions 6/6
100% Lines 19/19

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    17x 17x 17x           17x           18x 18x     18x 18x       2x       2x       8x 8x 8x 8x       5x         5x 5x       4x 4x          
import type {CompactMatchesOp, OPCODE_MATCHES} from '../codec/compact/types';
import type {OperationMatches, CreateRegexMatcher, RegexMatcher} from '../types';
import {AbstractPredicateOp} from './AbstractPredicateOp';
import {find, type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import {OPCODE} from '../constants';
import type {AbstractOp} from './AbstractOp';
 
/**
 * @category JSON Predicate
 */
export class OpMatches extends AbstractPredicateOp<'matches'> {
  public readonly matcher: RegexMatcher;
 
  // tslint:disable-next-line variable-name
  constructor(
    path: Path,
    public readonly value: string,
    public readonly ignore_case: boolean,
    createMatcher: CreateRegexMatcher,
  ) {
    super(path);
    this.matcher = createMatcher(value, ignore_case);
  }
 
  public op() {
    return 'matches' as const;
  }
 
  public code() {
    return OPCODE.matches;
  }
 
  public test(doc: unknown): boolean {
    const {val} = find(doc, this.path);
    Iif (typeof val !== 'string') return false;
    const test = this.matcher(val);
    return test;
  }
 
  public toJson(parent?: AbstractOp): OperationMatches {
    const op: OperationMatches = {
      op: 'matches',
      path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
      value: this.value,
    };
    Iif (this.ignore_case) (op as any).ignore_case = this.ignore_case;
    return op;
  }
 
  public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactMatchesOp {
    const opcode: OPCODE_MATCHES = verbose ? 'matches' : OPCODE.matches;
    return this.ignore_case
      ? [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value, 1]
      : [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
  }
}