-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathevaluator.ts
More file actions
53 lines (43 loc) · 1.17 KB
/
evaluator.ts
File metadata and controls
53 lines (43 loc) · 1.17 KB
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
import { InMemoryStore } from './in-memory-store';
import { EvaluationContext } from './interfaces';
export class Evaluator {
constructor(private readonly store: InMemoryStore) {}
isEnabled(
flagKey: string,
context: EvaluationContext = {},
defaultValue: boolean = false,
): boolean {
const entry = this.store.get(flagKey);
if (!entry) {
return defaultValue;
}
if (!entry.enabled) {
return false;
}
if (context.userId) {
const userTarget = entry.targets.users.find(
(t) => t.id === context.userId,
);
if (userTarget) {
return userTarget.enabled;
}
}
if (context.organizationId) {
const orgTarget = entry.targets.organizations.find(
(t) => t.id === context.organizationId,
);
if (orgTarget) {
return orgTarget.enabled;
}
}
return entry.default_value;
}
getAllFlags(context: EvaluationContext = {}): Record<string, boolean> {
const flags = this.store.getAll();
const result: Record<string, boolean> = {};
for (const slug of Object.keys(flags)) {
result[slug] = this.isEnabled(slug, context);
}
return result;
}
}