json-joy
Version:
Collection of libraries for building collaborative editing apps.
48 lines (47 loc) • 1.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpLess = void 0;
const AbstractPredicateOp_1 = require("./AbstractPredicateOp");
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
const constants_1 = require("../constants");
/**
* @category JSON Predicate
*/
class OpLess extends AbstractPredicateOp_1.AbstractPredicateOp {
constructor(path, value) {
super(path);
this.value = value;
}
op() {
return 'less';
}
code() {
return constants_1.OPCODE.less;
}
test(doc) {
const { val } = (0, json_pointer_1.find)(doc, this.path);
if (typeof val !== 'number')
return false;
const test = val < this.value;
return test;
}
toJson(parent) {
const op = {
op: 'less',
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 ? 'less' : constants_1.OPCODE.less;
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.less);
encoder.encodeArray(parent ? this.path.slice(parent.path.length) : this.path);
encoder.encodeNumber(this.value);
}
}
exports.OpLess = OpLess;
;