All files / json-patch/op OpAnd.ts

92.59% Statements 25/27
100% Branches 9/9
77.77% Functions 7/9
91.3% Lines 21/23

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  15x     15x 15x             15x     52x   52x                       3x 1x       25x     60x   25x       20x 20x     48x         10x 10x 10x 10x 10x 10x 24x      
import type {CompactAndOp, OPCODE_AND} from '../codec/compact/types';
import {AbstractSecondOrderPredicateOp} from './AbstractSecondOrderPredicateOp';
import type {AbstractPredicateOp} from './AbstractPredicateOp';
import type {OperationAnd, PredicateOperation} from '../types';
import {OPCODE} from '../constants';
import {type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import type {AbstractOp} from './AbstractOp';
import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack';
 
/**
 * @category JSON Predicate
 */
export class OpAnd extends AbstractSecondOrderPredicateOp<'and'> {
  constructor(
    path: Path,
    public readonly ops: AbstractPredicateOp[],
  ) {
    super(path, ops);
  }
 
  public op() {
    return 'and' as const;
  }
 
  public code() {
    return OPCODE.and;
  }
 
  public test(doc: unknown): boolean {
    for (const op of this.ops) if (!op.test(doc)) return false;
    return true;
  }
 
  public toJson(parent?: AbstractOp): OperationAnd {
    const op: OperationAnd = {
      op: 'and',
      path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
      apply: this.ops.map((op) => op.toJson(this)) as PredicateOperation[],
    };
    return op;
  }
 
  public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactAndOp {
    const opcode: OPCODE_AND = verbose ? 'and' : OPCODE.and;
    return [
      opcode,
      parent ? this.path.slice(parent.path.length) : this.path,
      this.ops.map((op) => op.toCompact(this, verbose)),
    ];
  }
 
  public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
    const path = parent ? this.path.slice(parent.path.length) : this.path;
    encoder.encodeArrayHeader(3);
    encoder.writer.u8(OPCODE.and);
    encoder.encodeArray(path as unknown[]);
    const length = this.ops.length;
    encoder.encodeArrayHeader(length);
    for (let i = 0; i < length; i++) this.ops[i].encode(encoder, this);
  }
}