forked from jbarrus/typescript-refactoring-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder.ts
51 lines (43 loc) · 1.11 KB
/
finder.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
import Thing from './thing';
import F from './f';
import FT from './ft';
export default class Finder {
constructor(private p: Thing[]) {}
Find(ft: FT): F {
const tr: F[] = [];
for (let i = 0; i < this.p.length - 1; i++) {
for (let j = i + 1; j < this.p.length; j++) {
const r = new F();
if (this.p[i].birthDate.getTime() < this.p[j].birthDate.getTime()) {
r.P1 = this.p[i];
r.P2 = this.p[j];
} else {
r.P1 = this.p[j];
r.P2 = this.p[i];
}
r.D = r.P2.birthDate.getTime() - r.P1.birthDate.getTime();
tr.push(r);
}
}
if (tr.length < 1) {
return new F();
}
let answer = tr[0];
for (let i=0; i<tr.length; i++) {
const result: F = tr[i];
switch (ft) {
case FT.One :
if (result.D && answer.D && result.D < answer.D) {
answer = result;
}
break;
case FT.Two :
if (result.D && answer.D && result.D > answer.D) {
answer = result;
}
break;
}
}
return answer;
}
}