All files / json-patch/op OpContains.ts

86.2% Statements 25/29
52.38% Branches 11/21
85.71% Functions 6/7
96.15% Lines 25/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 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    15x 15x 15x             15x       63x 63x   63x       9x               43x 43x 43x 43x 43x 43x       10x         10x 10x       8x 8x           4x 4x 4x 4x 4x 4x      
import type {CompactContainsOp, OPCODE_CONTAINS} from '../codec/compact/types';
import type {OperationContains} 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';
import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack';
 
/**
 * @category JSON Predicate
 */
export class OpContains extends AbstractPredicateOp<'contains'> {
  // tslint:disable-next-line variable-name
  constructor(
    path: Path,
    public readonly value: string,
    public readonly ignore_case: boolean,
  ) {
    super(path);
  }
 
  public op() {
    return 'contains' as const;
  }
 
  public code() {
    return OPCODE.contains;
  }
 
  public test(doc: unknown): boolean {
    const {val} = find(doc, this.path);
    Iif (typeof val !== 'string') return false;
    const testValue = this.ignore_case ? val.toLowerCase() : val;
    const testString = this.ignore_case ? this.value.toLowerCase() : this.value;
    const test = testValue.indexOf(testString) > -1;
    return test;
  }
 
  public toJson(parent?: AbstractOp): OperationContains {
    const op: OperationContains = {
      op: 'contains',
      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): CompactContainsOp {
    const opcode: OPCODE_CONTAINS = verbose ? 'contains' : OPCODE.contains;
    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];
  }
 
  public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
    const ignoreCase = this.ignore_case;
    encoder.encodeArrayHeader(ignoreCase ? 4 : 3);
    encoder.writer.u8(OPCODE.contains);
    encoder.encodeArray(parent ? this.path.slice(parent.path.length) : (this.path as unknown[]));
    encoder.encodeString(this.value);
    Iif (ignoreCase) encoder.writer.u8(1);
  }
}