json-joy
Version:
Collection of libraries for building collaborative editing apps.
48 lines • 1.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpTest = void 0;
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
const AbstractPredicateOp_1 = require("./AbstractPredicateOp");
const constants_1 = require("../constants");
const deepEqual_1 = require("@jsonjoy.com/util/lib/json-equal/deepEqual");
/**
* @category JSON Patch
* @category JSON Predicate
*/
class OpTest extends AbstractPredicateOp_1.AbstractPredicateOp {
constructor(path, value, not) {
super(path);
this.value = value;
this.not = not;
}
op() {
return 'test';
}
code() {
return constants_1.OPCODE.test;
}
test(doc) {
const { val } = (0, json_pointer_1.find)(doc, this.path);
if (val === undefined)
return !!this.not;
const test = (0, deepEqual_1.deepEqual)(val, this.value);
return this.not ? !test : test;
}
toJson(parent) {
const op = {
op: 'test',
path: (0, json_pointer_1.formatJsonPointer)(parent ? this.path.slice(parent.path.length) : this.path),
value: this.value,
};
if (this.not)
op.not = this.not;
return op;
}
toCompact(parent, verbose) {
const path = parent ? this.path.slice(parent.path.length) : this.path;
const opcode = verbose ? 'test' : constants_1.OPCODE.test;
return this.not ? [opcode, path, this.value, 1] : [opcode, path, this.value];
}
}
exports.OpTest = OpTest;
//# sourceMappingURL=OpTest.js.map