All files / json-patch/op OpType.ts

85.18% Statements 23/27
46.66% Branches 7/15
85.71% Functions 6/7
95.65% Lines 22/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  15x   15x 15x       15x         15x     80x   80x       2x               10x 10x 10x   10x 5x 5x       35x         35x       28x 28x       14x 14x 14x 14x      
import type {CompactTypeOp, OPCODE_TYPE} from '../codec/compact/types';
import {AbstractPredicateOp} from './AbstractPredicateOp';
import type {OperationType, JsonPatchTypes} 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';
 
const {isArray} = Array;
 
/**
 * @category JSON Predicate
 */
export class OpType extends AbstractPredicateOp<'type'> {
  constructor(
    path: Path,
    public readonly value: JsonPatchTypes,
  ) {
    super(path);
  }
 
  public op() {
    return 'type' as const;
  }
 
  public code() {
    return OPCODE.type;
  }
 
  public test(doc: unknown): boolean {
    const {val} = find(doc, this.path);
    Iif (val === null) return this.value === 'null';
    Iif (isArray(val)) return this.value === 'array';
    // biome-ignore lint: comparison to value is on purpose
    if (typeof val === this.value) return true;
    Iif (typeof val === 'number' && val === Math.round(val) && this.value === 'integer') return true;
    return false;
  }
 
  public toJson(parent?: AbstractOp): OperationType {
    const op: OperationType = {
      op: 'type',
      path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
      value: this.value,
    };
    return op;
  }
 
  public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactTypeOp {
    const opcode: OPCODE_TYPE = verbose ? 'type' : OPCODE.type;
    return [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
  }
 
  public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
    encoder.encodeArrayHeader(3);
    encoder.writer.u8(OPCODE.type);
    encoder.encodeArray(parent ? this.path.slice(parent.path.length) : (this.path as unknown[]));
    encoder.encodeString(this.value);
  }
}