UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

51 lines (50 loc) 1.49 kB
import { AbstractSecondOrderPredicateOp } from './AbstractSecondOrderPredicateOp'; import { OPCODE } from '../constants'; import { formatJsonPointer } from '@jsonjoy.com/json-pointer'; /** * @category JSON Predicate */ export class OpOr extends AbstractSecondOrderPredicateOp { ops; constructor(path, ops) { super(path, ops); this.ops = ops; } op() { return 'or'; } code() { return OPCODE.or; } test(doc) { for (const op of this.ops) if (op.test(doc)) return true; return false; } toJson(parent) { const op = { op: 'or', path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path), apply: this.ops.map((op) => op.toJson(this)), }; return op; } toCompact(parent, verbose) { const opcode = verbose ? 'or' : OPCODE.or; return [ opcode, parent ? this.path.slice(parent.path.length) : this.path, this.ops.map((op) => op.toCompact(this, verbose)), ]; } encode(encoder, parent) { encoder.encodeArrayHeader(3); encoder.writer.u8(OPCODE.or); encoder.encodeArray(parent ? this.path.slice(parent.path.length) : this.path); const length = this.ops.length; encoder.encodeArrayHeader(length); for (let i = 0; i < length; i++) this.ops[i].encode(encoder, this); } }