-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnearObject.ts
98 lines (80 loc) · 2.1 KB
/
nearObject.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
export default class GraphQLNearObject {
private beacon?: string;
private certainty?: number;
private distance?: number;
private id?: string;
private readonly source: any;
constructor(nearObjectObj: any) {
this.source = nearObjectObj;
}
toString(wrap = true) {
this.parse();
this.validate();
let args: any[] = [];
if (this.id) {
args = [...args, `id:${JSON.stringify(this.id)}`];
}
if (this.beacon) {
args = [...args, `beacon:${JSON.stringify(this.beacon)}`];
}
if (this.certainty) {
args = [...args, `certainty:${this.certainty}`];
}
if (this.distance) {
args = [...args, `distance:${this.distance}`];
}
if (!wrap) {
return `${args.join(",")}`;
}
return `{${args.join(",")}}`;
}
validate() {
if (!this.id && !this.beacon) {
throw new Error("nearObject filter: id or beacon needs to be set");
}
}
parse() {
for (let key in this.source) {
switch (key) {
case "id":
this.parseID(this.source[key]);
break;
case "beacon":
this.parseBeacon(this.source[key]);
break;
case "certainty":
this.parseCertainty(this.source[key]);
break;
case "distance":
this.parseDistance(this.source[key]);
break;
default:
throw new Error("nearObject filter: unrecognized key '" + key + "'");
}
}
}
parseID(id: string) {
if (typeof id !== "string") {
throw new Error("nearObject filter: id must be a string");
}
this.id = id;
}
parseBeacon(beacon: string) {
if (typeof beacon !== "string") {
throw new Error("nearObject filter: beacon must be a string");
}
this.beacon = beacon;
}
parseCertainty(cert: number) {
if (typeof cert !== "number") {
throw new Error("nearObject filter: certainty must be a number");
}
this.certainty = cert;
}
parseDistance(dist: number) {
if (typeof dist !== "number") {
throw new Error("nearObject filter: distance must be a number");
}
this.distance = dist;
}
}