-
Notifications
You must be signed in to change notification settings - Fork 0
/
PredicateSubjectDictionary.ts
136 lines (112 loc) · 4.08 KB
/
PredicateSubjectDictionary.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type {
TPredicateSubjectOptionsList,
TPredicateOperator,
TPredicateSubjectAsColumnDefinition,
TPredicateSubjectWithId,
TPredicateSubjectDictionary,
TPredicateSubjectProperties,
TPredicateSubjectDictionaryJson,
} from "./type";
import type { TValueLabelList } from "../index";
import { TPredicateProperties } from "../common";
import { IPredicateSubjectDictionary } from "./IPredicateSubjectDictionary";
import cloneDeep from "lodash.clonedeep";
export class PredicateSubjectDictionary implements IPredicateSubjectDictionary {
private _querySubjects: TPredicateSubjectDictionary;
constructor() {
this._querySubjects = {};
}
getOptionsList(subjectId: string): TPredicateSubjectOptionsList {
// should always return {$anyOf:[...], $nanyOf:[...], $oneOf: [...]}
// where array can empty
const querySubject = this.getSubject(subjectId);
let $anyOf = [] as TValueLabelList;
let $nanyOf = [] as TValueLabelList;
let $oneOf = [] as TValueLabelList;
if (!subjectId) {
return { $anyOf, $nanyOf, $oneOf };
}
if (
// probably caused by predicateSubjectJson in bad format
// this isn't the place to deal with that.
!querySubject ||
!querySubject.validOperators
) {
return { $anyOf, $nanyOf, $oneOf };
}
if (typeof querySubject.validOperators.$anyOf === "object") {
// coverage: blueSkies (or other) need add these conditions
$anyOf = querySubject.validOperators.$anyOf.optionList || [];
}
if (typeof querySubject.validOperators.$nanyOf === "object") {
// coverage: blueSkies (or other) need add these conditions
$nanyOf = querySubject.validOperators.$nanyOf.optionList || [];
}
if (typeof querySubject.validOperators.$oneOf === "object") {
// coverage: blueSkies (or other) need add these conditions
$oneOf = querySubject.validOperators.$oneOf.optionList || [];
}
return { $anyOf, $nanyOf, $oneOf };
}
getAllSubjects(): TPredicateSubjectDictionary {
return this._querySubjects;
}
getSubjectIds(): string[] {
return Object.keys(this._querySubjects);
}
getColumns(): TPredicateSubjectAsColumnDefinition[] {
const columns: TPredicateSubjectAsColumnDefinition[] = [];
Object.entries(this._querySubjects).forEach(([subjectId, props]) => {
columns.push({
subjectId,
datatype: props.datatype,
defaultLabel: props.defaultLabel,
});
});
return columns;
}
getSubject(subjectId: string): TPredicateSubjectWithId {
if (subjectId in this._querySubjects) {
return { subjectId, ...this._querySubjects[subjectId] };
}
// TODO - should have two functions returns null
// or throw eg *orThrow
// @ts-ignore
return null;
}
makeEmptyPredicate(): TPredicateProperties {
const { subjectId, validOperators } = this.getDefaultSubject();
return {
subjectId,
operator: Object.keys(validOperators).shift() as TPredicateOperator,
value: "",
};
}
getDefaultSubject(): TPredicateSubjectWithId {
const [subjectId, subjectProps] = Object.entries(this._querySubjects).shift() || [];
if (subjectId === undefined || subjectProps === undefined) {
return {} as TPredicateSubjectWithId;
}
return { subjectId, ...subjectProps };
}
private addSubject(subjectId: string, subjectProperties: TPredicateSubjectProperties) {
this._querySubjects[subjectId] = subjectProperties;
}
public toJson(): TPredicateSubjectDictionaryJson {
// TODO - this is probably not the right way.
// in theory it works but parse(stringify()) -- seems like a bad idea.
// return JSON.parse(JSON.stringify(this._querySubjects));
return cloneDeep(this._querySubjects);
}
static fromJson(
jsonObject: TPredicateSubjectDictionaryJson
): PredicateSubjectDictionary {
const newDictionary = new PredicateSubjectDictionary();
Object.entries(jsonObject as TPredicateSubjectDictionary).forEach(
([subjectId, subjectProperties]) => {
newDictionary.addSubject(subjectId, subjectProperties);
}
);
return newDictionary;
}
}