forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags_state.js
46 lines (42 loc) · 1.38 KB
/
flags_state.js
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
function FlagsStateBuilder(valid, withReasons) {
const builder = {};
const flagValues = {};
const flagMetadata = {};
builder.addFlag = (flag, value, variation, reason, trackEvents, trackReason, detailsOnlyIfTracked) => {
flagValues[flag.key] = value;
const meta = {};
if (variation !== undefined && variation !== null) {
meta.variation = variation;
}
const omitDetails =
detailsOnlyIfTracked &&
!trackEvents &&
!trackReason &&
(flag.debugEventsUntilDate === undefined || flag.debugEventsUntilDate === null);
if (!omitDetails) {
meta.version = flag.version;
}
if (reason && (trackReason || (withReasons && !omitDetails))) {
meta.reason = reason;
}
if (trackEvents) {
meta.trackEvents = true;
}
if (trackReason) {
meta.trackReason = true;
}
if (flag.debugEventsUntilDate !== undefined && flag.debugEventsUntilDate !== null) {
meta.debugEventsUntilDate = flag.debugEventsUntilDate;
}
flagMetadata[flag.key] = meta;
};
builder.build = () => ({
valid: valid,
allValues: () => flagValues,
getFlagValue: key => flagValues[key],
getFlagReason: key => (flagMetadata[key] ? flagMetadata[key].reason : null),
toJSON: () => Object.assign({}, flagValues, { $flagsState: flagMetadata, $valid: valid }),
});
return builder;
}
module.exports = FlagsStateBuilder;