Skip to content

Commit 9a4d96b

Browse files
zashraf1985mjc1283
authored andcommitted
refactor(OptimizelyConfig): Simplified feature variable merging logic. (#373)
Summary: Simplified feature variable merging logic in optimizely config. Test planL 1. Manually Tested Thoroughly 2. Passed all Unit and Integration tests
1 parent 986f6d5 commit 9a4d96b

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

packages/optimizely-sdk/lib/core/optimizely_config/index.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,55 +38,48 @@ function getExperimentsMap(configObj) {
3838
id: experiment.id,
3939
key: experiment.key,
4040
variationsMap: experiment.variations.reduce(function(variations, variation) {
41-
var variablesMap = {};
42-
if (variation.featureEnabled) {
43-
variablesMap = variation.variables.reduce(function(variables, variable) {
44-
// developing a temporary map using variable ids. the entry with ids will be deleted after merging with featurevaribles
45-
variables[variable.id] = {
46-
id: variable.id,
47-
value: variable.value,
48-
};
49-
return variables;
50-
}, {});
51-
}
5241
variations[variation.key] = {
5342
id: variation.id,
5443
key: variation.key,
5544
featureEnabled: variation.featureEnabled,
56-
variablesMap,
45+
variablesMap: getMergedVariablesMap(configObj, variation, experiment.id, featureVariablesMap)
5746
};
5847
return variations;
5948
}, {}),
6049
};
61-
var featureId = configObj.experimentFeatureMap[experiment.id];
62-
if (featureId) {
63-
mergeFeatureVariables(experiments[experiment.key], featureVariablesMap[featureId]);
64-
}
6550
}
6651
return experiments;
6752
}, {});
6853
}
6954

70-
// Merges feature varibles in variations of passed in experiment
71-
// Modifies experiment object.
72-
function mergeFeatureVariables(experiment, featureVariables) {
73-
var variationKeys = Object.keys(experiment.variationsMap);
74-
variationKeys.forEach(function(variationKey) {
75-
var variation = experiment.variationsMap[variationKey];
76-
featureVariables.forEach(function(featureVariable) {
77-
var variationVariable = variation.variablesMap[featureVariable.id];
78-
var variableValue = variationVariable ? variationVariable.value : featureVariable.defaultValue;
79-
variation.variablesMap[featureVariable.key] = {
55+
// Merges feature key and type from feature variables to variation variables.
56+
function getMergedVariablesMap(configObj, variation, experimentId, featureVariablesMap) {
57+
var featureId = configObj.experimentFeatureMap[experimentId];
58+
variablesObject = {};
59+
if (featureId) {
60+
experimentFeatureVariables = featureVariablesMap[featureId];
61+
// Temporary variation variables map to get values to merge.
62+
var tempVariablesIdMap = variation.variables.reduce(function(variablesMap, variable) {
63+
variablesMap[variable.id] = {
64+
id: variable.id,
65+
value: variable.value,
66+
};
67+
return variablesMap;
68+
}, {});
69+
variablesObject = experimentFeatureVariables.reduce(function(variablesMap, featureVariable) {
70+
variationVariable = tempVariablesIdMap[featureVariable.id];
71+
variableValue = variation.featureEnabled && variationVariable ? variationVariable.value : featureVariable.defaultValue;
72+
variablesMap[featureVariable.key] = {
8073
id: featureVariable.id,
8174
key: featureVariable.key,
8275
type: featureVariable.type,
8376
value: variableValue,
8477
};
85-
// deleting the temporary entry
86-
variationVariable && delete variation.variablesMap[featureVariable.id];
87-
})
88-
});
89-
};
78+
return variablesMap;
79+
}, {})
80+
}
81+
return variablesObject;
82+
}
9083

9184
// Gets map of all experiments
9285
function getFeaturesMap(configObj, allExperiments) {

packages/optimizely-sdk/lib/optimizely/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,15 +885,15 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
885885
* OptimizelyConfig Object Schema
886886
* {
887887
* 'experimentsMap': {
888-
* '111111': {
888+
* 'my-fist-experiment': {
889889
* 'id': '111111',
890890
* 'key': 'my-fist-experiment'
891891
* 'variationsMap': {
892-
* '121212': {
892+
* 'variation_1': {
893893
* 'id': '121212',
894894
* 'key': 'variation_1',
895895
* 'variablesMap': {
896-
* '222222': {
896+
* 'age': {
897897
* 'id': '222222',
898898
* 'key': 'age',
899899
* 'type': 'integer',
@@ -905,7 +905,7 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey
905905
* }
906906
* },
907907
* 'featuresMap': {
908-
* '333333': {
908+
* 'awesome-feature': {
909909
* 'id': '333333',
910910
* 'key': 'awesome-feature',
911911
* 'experimentsMap': Object,

0 commit comments

Comments
 (0)