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