UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

55 lines (54 loc) 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpType = void 0; const AbstractPredicateOp_1 = require("./AbstractPredicateOp"); const json_pointer_1 = require("@jsonjoy.com/json-pointer"); const constants_1 = require("../constants"); const { isArray } = Array; /** * @category JSON Predicate */ class OpType extends AbstractPredicateOp_1.AbstractPredicateOp { constructor(path, value) { super(path); this.value = value; } op() { return 'type'; } code() { return constants_1.OPCODE.type; } test(doc) { const { val } = (0, json_pointer_1.find)(doc, this.path); if (val === null) return this.value === 'null'; if (isArray(val)) return this.value === 'array'; // biome-ignore lint: comparison to value is on purpose if (typeof val === this.value) return true; if (typeof val === 'number' && val === Math.round(val) && this.value === 'integer') return true; return false; } toJson(parent) { const op = { op: 'type', path: (0, json_pointer_1.formatJsonPointer)(parent ? this.path.slice(parent.path.length) : this.path), value: this.value, }; return op; } toCompact(parent, verbose) { const opcode = verbose ? 'type' : constants_1.OPCODE.type; return [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value]; } encode(encoder, parent) { encoder.encodeArrayHeader(3); encoder.writer.u8(constants_1.OPCODE.type); encoder.encodeArray(parent ? this.path.slice(parent.path.length) : this.path); encoder.encodeString(this.value); } } exports.OpType = OpType;