All files / json-patch/op OpTestString.ts

87.5% Statements 28/32
47.05% Branches 8/17
85.71% Functions 6/7
96.55% Lines 28/29

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  15x   15x 15x             15x     48x 48x 48x   48x       7x               28x 28x 28x 28x 28x 28x 28x       10x           10x 10x       8x 8x 8x       4x 4x 4x 4x 4x 4x      
import type {CompactTestStringOp, OPCODE_TEST_STRING} from '../codec/compact/types';
import {AbstractPredicateOp} from './AbstractPredicateOp';
import type {OperationTestString} from '../types';
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 Patch Extended
 */
export class OpTestString extends AbstractPredicateOp<'test_string'> {
  constructor(
    path: Path,
    public readonly pos: number,
    public readonly str: string,
    public readonly not: boolean,
  ) {
    super(path);
  }
 
  public op() {
    return 'test_string' as const;
  }
 
  public code() {
    return OPCODE.test_string;
  }
 
  public test(doc: unknown): boolean {
    const {val} = find(doc, this.path);
    Iif (typeof val !== 'string') return false;
    const length = (val as string).length;
    const start = Math.min(this.pos, length);
    const end = Math.min(this.pos + this.str.length, length);
    const test = (val as string).substring(start, end) === this.str;
    return this.not ? !test : test;
  }
 
  public toJson(parent?: AbstractOp): OperationTestString {
    const op: OperationTestString = {
      op: 'test_string',
      path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
      pos: this.pos,
      str: this.str,
    };
    Iif (this.not) (op as any).not = this.not;
    return op;
  }
 
  public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactTestStringOp {
    const opcode: OPCODE_TEST_STRING = verbose ? 'test_string' : OPCODE.test_string;
    const path = parent ? this.path.slice(parent.path.length) : this.path;
    return this.not ? [opcode, path, this.pos, this.str, 1] : [opcode, path, this.pos, this.str];
  }
 
  public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
    encoder.encodeArrayHeader(this.not ? 5 : 4);
    encoder.writer.u8(OPCODE.test_string);
    encoder.encodeArray(parent ? this.path.slice(parent.path.length) : (this.path as unknown[]));
    encoder.encodeNumber(this.pos);
    encoder.encodeString(this.str);
    Iif (this.not) encoder.writer.u8(1);
  }
}