Skip to content

Commit 4468773

Browse files
committed
feat(condition): adds possibility to associate notes with parsed condition
1 parent 254644d commit 4468773

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

packages/core/src/Condition.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
export interface Condition<T = unknown> {
2-
readonly operator: string;
3-
readonly value: T;
1+
export interface Note<T> {
2+
type: string
3+
message?: string
4+
originalValue?: T
45
}
56

6-
export class DocumentCondition<T> implements Condition<T> {
7-
public readonly operator!: string;
7+
export abstract class Condition<T = unknown> {
8+
private _notes?: Note<T>[];
89

9-
public readonly value!: T;
10+
constructor(
11+
public readonly operator: string,
12+
public readonly value: T
13+
) {}
1014

11-
constructor(operator: string, value: T) {
12-
this.operator = operator;
13-
this.value = value;
15+
get notes(): ReadonlyArray<Note<T>> | undefined {
16+
return this._notes;
17+
}
18+
19+
addNote(note: Note<T>) {
20+
this._notes = this._notes || [];
21+
this._notes.push(note);
1422
}
1523
}
1624

17-
export class CompoundCondition<T extends Condition = Condition> extends DocumentCondition<T[]> {
18-
public readonly operator!: string;
25+
export class DocumentCondition<T> extends Condition<T> {
26+
}
1927

28+
export class CompoundCondition<T extends Condition = Condition> extends DocumentCondition<T[]> {
2029
constructor(operator: string, conditions: T[]) {
2130
if (!Array.isArray(conditions)) {
2231
throw new Error(`"${operator}" operator expects to receive an array of conditions`);
@@ -27,16 +36,11 @@ export class CompoundCondition<T extends Condition = Condition> extends Document
2736
}
2837

2938
export const ITSELF = '__itself__';
30-
export class FieldCondition<T = unknown> implements Condition<T> {
31-
public readonly operator!: string;
32-
39+
export class FieldCondition<T = unknown> extends Condition<T> {
3340
public readonly field!: string | typeof ITSELF;
3441

35-
public readonly value!: T;
36-
3742
constructor(operator: string, field: string | typeof ITSELF, value: T) {
38-
this.operator = operator;
39-
this.value = value;
43+
super(operator, value);
4044
this.field = field;
4145
}
4246
}

0 commit comments

Comments
 (0)