json-joy
Version:
Collection of libraries for building collaborative editing apps.
59 lines (58 loc) • 2.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpContains = 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 OpContains extends AbstractPredicateOp_1.AbstractPredicateOp {
// tslint:disable-next-line variable-name
constructor(path, value, ignore_case) {
super(path);
this.value = value;
this.ignore_case = ignore_case;
}
op() {
return 'contains';
}
code() {
return constants_1.OPCODE.contains;
}
test(doc) {
const { val } = (0, json_pointer_1.find)(doc, this.path);
if (typeof val !== 'string')
return false;
const testValue = this.ignore_case ? val.toLowerCase() : val;
const testString = this.ignore_case ? this.value.toLowerCase() : this.value;
const test = testValue.indexOf(testString) > -1;
return test;
}
toJson(parent) {
const op = {
op: 'contains',
path: (0, json_pointer_1.formatJsonPointer)(parent ? this.path.slice(parent.path.length) : this.path),
value: this.value,
};
if (this.ignore_case)
op.ignore_case = this.ignore_case;
return op;
}
toCompact(parent, verbose) {
const opcode = verbose ? 'contains' : constants_1.OPCODE.contains;
return this.ignore_case
? [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value, 1]
: [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
}
encode(encoder, parent) {
const ignoreCase = this.ignore_case;
encoder.encodeArrayHeader(ignoreCase ? 4 : 3);
encoder.writer.u8(constants_1.OPCODE.contains);
encoder.encodeArray(parent ? this.path.slice(parent.path.length) : this.path);
encoder.encodeString(this.value);
if (ignoreCase)
encoder.writer.u8(1);
}
}
exports.OpContains = OpContains;