-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
302 lines (250 loc) · 11.7 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Author: Robert Vandenberg Huang
class InterfaceInfo {
public static readonly baseReferenceUrl: string = "https://docs.microsoft.com/en-us/dotnet/api/";
private _typeName: string;
private _namespace: string;
private _referenceUrl: string;
private _shortName: string;
get typeName(): string {
return this._typeName;
}
get namespace(): string {
return this._namespace;
}
get referenceUrl(): string {
return InterfaceInfo.baseReferenceUrl + this._referenceUrl;
}
get shortName(): string {
return this._shortName;
}
constructor(typeName: string, ns: string, referenceUrl: string, shortName: string) {
this._typeName = typeName;
this._namespace = ns;
this._referenceUrl = referenceUrl;
this._shortName = shortName;
}
static createAllInterfaces(): Array<InterfaceInfo> {
var interfaces = new Array<InterfaceInfo>();
interfaces.push(new InterfaceInfo("IEnumerable<T>", "System.Collections.Generic", "system.collections.generic.ienumerable-1", "E"));
interfaces.push(new InterfaceInfo("IProducerConsumerCollection<T>", "System.Collections.Concurrent", "system.collections.concurrent.iproducerconsumercollection-1", "PCC"));
interfaces.push(new InterfaceInfo("IReadOnlyCollection<T>", "System.Collections.Generic", "system.collections.generic.ireadonlycollection-1", "ROC"));
interfaces.push(new InterfaceInfo("IReadOnlyList<T>", "System.Collections.Generic", "system.collections.generic.ireadonlylist-1", "ROL"));
interfaces.push(new InterfaceInfo("IReadOnlyDictionary<TKey, TValue>", "System.Collections.Generic", "system.collections.generic.ireadonlydictionary-2", "ROD"));
interfaces.push(new InterfaceInfo("ICollection<T>", "System.Collections.Generic", "system.collections.generic.icollection-1", "C"));
interfaces.push(new InterfaceInfo("IList<T>", "System.Collections.Generic", "system.collections.generic.ilist-1", "L"));
interfaces.push(new InterfaceInfo("ISet<T>", "System.Collections.Generic", "system.collections.generic.iset-1", "S"));
interfaces.push(new InterfaceInfo("IDictionary<TKey, TValue>", "System.Collections.Generic", "system.collections.generic.idictionary-2", "D"));
interfaces.push(new InterfaceInfo("IEnumerable<IGrouping<TKey, TElement>>", "System.Linq", "system.linq.igrouping-2", "G"));
interfaces.push(new InterfaceInfo("ILookup<TKey, TElement>", "System.Linq", "system.linq.ilookup-2", "Lkp"));
interfaces.push(new InterfaceInfo("IOrderedEnumerable<TElement>", "System.Linq", "system.linq.iorderedenumerable-1", "OE"));
interfaces.push(new InterfaceInfo("IQueryable<T>", "System.Linq", "system.linq.iqueryable-1", "Q"));
interfaces.push(new InterfaceInfo("IOrderedQueryable<T>", "System.Linq", "system.linq.iorderedqueryable-1", "OQ"));
return interfaces;
}
}
enum QuestionType {
Questionnaire,
CheckListItem
}
interface QuestionInfo {
readonly interfaces: Array<string>;
readonly text: string;
readonly answer: boolean;
readonly description: string;
readonly questionType: QuestionType;
}
class Question implements QuestionInfo {
private _interfaces: Array<string>;
private _yourAnswer: boolean;
public readonly text: string;
/**
* true: yes
* false: no
*/
public readonly answer: boolean;
public readonly description: string;
public readonly questionType: QuestionType;
public get yourAnswer(): boolean{
return this._yourAnswer;
}
public set yourAnswer(value: boolean) {
this._yourAnswer = value;
}
public get interfaces(): Array<string> {
return this._interfaces;
}
public set interfaces(interfaces: Array<string>){
this._interfaces = interfaces == null ? new Array<string>() : interfaces;
}
constructor(question: QuestionInfo) {
this._interfaces = question.interfaces;
this._yourAnswer = false;
this.text = question.text;
this.answer = question.answer;
this.description = question.description;
this.questionType = question.questionType;
}
hasInterface(typeName: string): boolean {
if (this.answer) {
return this._interfaces.indexOf(typeName) >= 0; // Whitelisting
}
else {
return this._interfaces.indexOf(typeName) < 0; // Blacklisting
}
}
static compare(a: Question, b: Question): number {
if (a == null && b != null)
return 1;
if (a != null && b == null)
return -1;
if (a == null && b == null)
return 0;
if (a.interfaces.length < b.interfaces.length)
return -1;
if (a.interfaces.length > b.interfaces.length)
return 1;
return 0;
}
}
class Flow {
private _questions: Array<Question>;
private _doneQuestions: Array<Question>;
private _skippedQuestions: Array<Question>;
private _interfaces: Array<InterfaceInfo>;
private _currentQuestion: Question | null;
get hasNextQuestion(): boolean {
return this._questions.length > 0;
}
get currentQuestion(): Question | null {
return this._currentQuestion;
}
get answeredQuestions(): Array<Question> {
return this._doneQuestions;
}
get skippedQuestions(): Array<Question> {
return this._skippedQuestions;
}
get allQuestionTexts(): Array<string> {
return this._questions.map(q => q.text);
}
get interfaces(): Array<InterfaceInfo> {
return this._interfaces;
}
moveToNextQuestion() : Question | null {
var index = Math.floor(Math.random() * (this._questions.length - 0)) + 0;
if (this._currentQuestion == null || this._questions.length > 0) {
this._currentQuestion = this._questions[index];
this._questions.splice(index, 1);
}
else {
this._currentQuestion = null;
}
return this._currentQuestion;
}
answerCurrentQuestion(answer: boolean): void {
if (this._currentQuestion == null) return;
if (this._doneQuestions.length > 0 && this._doneQuestions[this._doneQuestions.length - 1].text == this._currentQuestion.text)
return;
if (this._currentQuestion.answer == answer) {
this._interfaces = this._interfaces.filter(i => this._currentQuestion!.interfaces.indexOf(i.typeName) >= 0);
}
else {
this._interfaces = this._interfaces.filter(i => this._currentQuestion!.interfaces.indexOf(i.typeName) < 0);
}
this._currentQuestion.yourAnswer = answer;
this._questions = this._questions.filter(q => this.questionFilter(q, this._interfaces.map(i => i.typeName))).sort(Question.compare);
this._doneQuestions.push(this._currentQuestion);
}
getInterfaceInfo(typeName: string) : InterfaceInfo {
return this._interfaces.filter(i => i.typeName == typeName)[0];
}
private questionFilter(q : Question, interfaceTypes: Array<string>) : boolean {
/* We need to eliminate two kinds of questions:
* 1. Questions that are no longer relevant.
* 2. Questions that could remove all remaining interfaces.
*/
if (q.interfaces.every(typeName => interfaceTypes.indexOf(typeName) < 0))
return false;
if (q.interfaces.length < interfaceTypes.length)
return true;
if (interfaceTypes.some(typeName => q.interfaces.indexOf(typeName) < 0))
return true;
this._skippedQuestions.push(q);
return false;
}
constructor(questions: Array<Question>, interfaces: Array<InterfaceInfo>) {
this._questions = questions.sort(Question.compare);
this._currentQuestion = null;
this._doneQuestions = new Array<Question>();
this._skippedQuestions = new Array<Question>();
this._interfaces = interfaces;
console.log("Remaining interfaces:");
console.log(this._interfaces.map(i => i.typeName));
}
static initialize(questions: Array<QuestionInfo>): Flow {
return new Flow(questions.filter(q => q.questionType == QuestionType.Questionnaire).map(q => new Question(q)), InterfaceInfo.createAllInterfaces());
}
}
class CheckList {
private _questions: Array<Question>;
private _tickedQuestions: ReadonlyArray<Question>;
private _interfaces: Array<InterfaceInfo>;
private _tickedInterfaces: ReadonlyArray<InterfaceInfo>;
get allQuestionTexts(): Array<string> {
return this._questions.map(q => q.text);
}
get interfaces(): Array<InterfaceInfo> {
return this._interfaces;
}
tickQuestion(questionText: string) {
this._questions.filter(q => q.text == questionText).forEach(q => q.yourAnswer = !q.yourAnswer);
this._tickedQuestions = this._questions.filter(q => q.yourAnswer == q.answer);
this._interfaces.sort((a, b) => {
if (this._tickedQuestions.length > 0) {
var checkStateA = this._tickedQuestions.every(q => q.interfaces.indexOf(a.typeName) >= 0);
var checkStateB = this._tickedQuestions.every(q => q.interfaces.indexOf(b.typeName) >= 0);
if (checkStateA && !checkStateB) return -1;
if (checkStateB && !checkStateA) return 1;
}
return a.typeName.localeCompare(b.typeName);
});
}
checkInterfaceAvailability(typeName: string) : boolean {
return this._tickedQuestions != null && this._tickedQuestions.length > 0 && this._tickedQuestions.every(q => q.hasInterface(typeName));
}
tickInterface(typeName: string) {
var tickedInterfaces = this._tickedInterfaces.filter(i => i.typeName != typeName);
if (tickedInterfaces.length == this._tickedInterfaces.length) {
this._interfaces.forEach(i => {
if (i.typeName == typeName) {
tickedInterfaces.push(i);
}
});
}
this._tickedInterfaces = tickedInterfaces;
this._questions.sort((a, b) => {
var countA = 0;
var countB = 0;
this._tickedInterfaces.forEach(i => { countA += a.hasInterface(i.typeName) ? 1 : 0 });
this._tickedInterfaces.forEach(i => { countB += b.hasInterface(i.typeName) ? 1 : 0 });
if (countA > countB) return -1;
if (countA < countB) return 1;
return 0;
});
}
checkQuestionAvailability(questoinText: string) : ReadonlyArray<InterfaceInfo> {
return this._tickedInterfaces.filter(i => this._questions.some(q => q.text == questoinText && q.hasInterface(i.typeName)));
}
constructor(questions: Array<Question>, interfaces: Array<InterfaceInfo>) {
this._questions = questions.sort(Question.compare);
this._questions.forEach(q => q.yourAnswer = false);
this._interfaces = interfaces;
this._interfaces.sort((a, b) => a.typeName.localeCompare(b.typeName));
this._tickedQuestions = new Array<Question>();
this._tickedInterfaces = new Array<InterfaceInfo>();
}
static initialize(questions: Array<QuestionInfo>): CheckList {
return new CheckList(questions.filter(q => q.questionType == QuestionType.CheckListItem).map(q => new Question(q)), InterfaceInfo.createAllInterfaces());
}
}