All files / json-patch/op OpOr.ts

88.88% Statements 16/18
71.42% Branches 5/7
87.5% Functions 7/8
87.5% Lines 14/16

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  17x     17x 17x           17x     21x   21x               4x       2x         10x     30x   10x       8x 8x     24x        
import type {CompactOrOp, OPCODE_OR} from '../codec/compact/types';
import {AbstractSecondOrderPredicateOp} from './AbstractSecondOrderPredicateOp';
import type {AbstractPredicateOp} from './AbstractPredicateOp';
import type {OperationOr, PredicateOperation} from '../types';
import {OPCODE} from '../constants';
import {type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import type {AbstractOp} from './AbstractOp';
 
/**
 * @category JSON Predicate
 */
export class OpOr extends AbstractSecondOrderPredicateOp<'or'> {
  constructor(
    path: Path,
    public readonly ops: AbstractPredicateOp[],
  ) {
    super(path, ops);
  }
 
  public op() {
    return 'or' as const;
  }
 
  public code() {
    return OPCODE.or;
  }
 
  public test(doc: unknown): boolean {
    for (const op of this.ops) if (op.test(doc)) return true;
    return false;
  }
 
  public toJson(parent?: AbstractOp): OperationOr {
    const op: OperationOr = {
      op: 'or',
      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): CompactOrOp {
    const opcode: OPCODE_OR = verbose ? 'or' : OPCODE.or;
    return [
      opcode,
      parent ? this.path.slice(parent.path.length) : this.path,
      this.ops.map((op) => op.toCompact(this, verbose)),
    ];
  }
}