-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
OpMore.ts
55 lines (48 loc) · 1.59 KB
/
OpMore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type {CompactMoreOp, OPCODE_MORE} from '../codec/compact/types';
import {AbstractPredicateOp} from './AbstractPredicateOp';
import {OperationMore} from '../types';
import {find, Path, formatJsonPointer} from '../../json-pointer';
import {OPCODE} from '../constants';
import {AbstractOp} from './AbstractOp';
import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack';
/**
* @category JSON Predicate
*/
export class OpMore extends AbstractPredicateOp<'more'> {
constructor(
path: Path,
public readonly value: number,
) {
super(path);
}
public op() {
return 'more' as 'more';
}
public code() {
return OPCODE.more;
}
public test(doc: unknown): boolean {
const {val} = find(doc, this.path);
if (typeof val !== 'number') return false;
const test = val > this.value;
return test;
}
public toJson(parent?: AbstractOp): OperationMore {
const op: OperationMore = {
op: 'more',
path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
value: this.value,
};
return op;
}
public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactMoreOp {
const opcode: OPCODE_MORE = verbose ? 'more' : OPCODE.more;
return [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
}
public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
encoder.encodeArrayHeader(3);
encoder.writer.u8(OPCODE.more);
encoder.encodeArray(parent ? this.path.slice(parent.path.length) : (this.path as unknown[]));
encoder.encodeNumber(this.value);
}
}