All files / json-patch/op OpTestStringLen.ts

95.45% Statements 21/22
75% Branches 9/12
100% Functions 6/6
100% Lines 20/20

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  17x   17x 17x           17x     76x 76x   76x       14x       4x       56x 56x 56x 56x 56x       10x         10x 10x       8x 8x 8x      
import type {CompactTestStringLenOp, OPCODE_TEST_STRING_LEN} from '../codec/compact/types';
import {AbstractPredicateOp} from './AbstractPredicateOp';
import type {OperationTestStringLen} from '../types';
import {find, type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import {OPCODE} from '../constants';
import type {AbstractOp} from './AbstractOp';
 
/**
 * @category JSON Patch Extended
 */
export class OpTestStringLen extends AbstractPredicateOp<'test_string_len'> {
  constructor(
    path: Path,
    public readonly len: number,
    public readonly not: boolean,
  ) {
    super(path);
  }
 
  public op() {
    return 'test_string_len' as const;
  }
 
  public code() {
    return OPCODE.test_string_len;
  }
 
  public test(doc: unknown): boolean {
    const {val} = find(doc, this.path);
    Iif (typeof val !== 'string') return false;
    const length = (val as string).length;
    const test = length >= this.len;
    return this.not ? !test : test;
  }
 
  public toJson(parent?: AbstractOp): OperationTestStringLen {
    const op: OperationTestStringLen = {
      op: 'test_string_len',
      path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
      len: this.len,
    };
    if (this.not) (op as any).not = this.not;
    return op;
  }
 
  public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactTestStringLenOp {
    const opcode: OPCODE_TEST_STRING_LEN = verbose ? 'test_string_len' : OPCODE.test_string_len;
    const path = parent ? this.path.slice(parent.path.length) : this.path;
    return this.not ? [opcode, path, this.len, 1] : [opcode, path, this.len];
  }
}