Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ exports.buildConversionEvent = function buildConversionEvent(config) {
function buildVisitorAttributes(configObj, attributes) {
var builtAttributes = [];
// Omit attribute values that are not supported by the log endpoint.
fns.forOwn(attributes, function(attributeValue, attributeKey) {
Object.keys(attributes || {}).forEach(function(attributeKey) {
var attributeValue = attributes[attributeKey];
if (attributesValidator.isAttributeValid(attributeKey, attributeValue)) {
var attributeId = projectConfig.getAttributeId(configObj, attributeKey, logger);
if (attributeId) {
Expand Down
3 changes: 2 additions & 1 deletion packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function getCommonEventParams(options) {
};

// Omit attribute values that are not supported by the log endpoint.
fns.forOwn(attributes, function(attributeValue, attributeKey) {
Object.keys(attributes || {}).forEach(function(attributeKey) {
var attributeValue = attributes[attributeKey];
if (attributeValidator.isAttributeValid(attributeKey, attributeValue)) {
var attributeId = projectConfig.getAttributeId(options.configObj, attributeKey, options.logger);
if (attributeId) {
Expand Down
16 changes: 9 additions & 7 deletions packages/optimizely-sdk/lib/core/notification_center/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

var enums = require('../../utils/enums');
var fns = require('../../utils/fns');
var jsSdkUtils = require('@optimizely/js-sdk-utils');

var LOG_LEVEL = enums.LOG_LEVEL;
Expand All @@ -37,7 +36,8 @@ function NotificationCenter(options) {
this.logger = options.logger;
this.errorHandler = options.errorHandler;
this.__notificationListeners = {};
fns.forOwn(enums.NOTIFICATION_TYPES, function(notificationTypeEnum) {

jsSdkUtils.objectValues(enums.NOTIFICATION_TYPES).forEach(function(notificationTypeEnum) {
this.__notificationListeners[notificationTypeEnum] = [];
}.bind(this));
this.__listenerId = 1;
Expand Down Expand Up @@ -101,7 +101,9 @@ NotificationCenter.prototype.removeNotificationListener = function (listenerId)
try {
var indexToRemove;
var typeToRemove;
fns.forOwn(this.__notificationListeners, function (listenersForType, notificationType) {

Object.keys(this.__notificationListeners).some(function(notificationType) {
var listenersForType = this.__notificationListeners[notificationType];
(listenersForType || []).every(function(listenerEntry, i) {
if (listenerEntry.id === listenerId) {
indexToRemove = i;
Expand All @@ -111,10 +113,10 @@ NotificationCenter.prototype.removeNotificationListener = function (listenerId)
return true
});
if (indexToRemove !== undefined && typeToRemove !== undefined) {
return false;
return true;
}
});

}.bind(this));
if (indexToRemove !== undefined && typeToRemove !== undefined) {
this.__notificationListeners[typeToRemove].splice(indexToRemove, 1);
return true;
Expand All @@ -131,7 +133,7 @@ NotificationCenter.prototype.removeNotificationListener = function (listenerId)
*/
NotificationCenter.prototype.clearAllNotificationListeners = function () {
try{
fns.forOwn(enums.NOTIFICATION_TYPES, function (notificationTypeEnum) {
jsSdkUtils.objectValues(enums.NOTIFICATION_TYPES).forEach(function (notificationTypeEnum) {
this.__notificationListeners[notificationTypeEnum] = [];
}.bind(this));
} catch (e) {
Expand Down
45 changes: 22 additions & 23 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
var fns = require('../../utils/fns');
var enums = require('../../utils/enums');
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var jsSdkUtils = require('@optimizely/js-sdk-utils');
var configValidator = require('../../utils/config_validator');
var projectConfigSchema = require('./project_config_schema');

Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = {
});

projectConfig.rolloutIdMap = fns.keyBy(projectConfig.rollouts || [], 'id');
fns.forOwn(projectConfig.rolloutIdMap, function(rollout) {
jsSdkUtils.objectValues(projectConfig.rolloutIdMap || {}).forEach(function (rollout) {
(rollout.experiments || []).forEach(function(experiment) {
projectConfig.experiments.push(fns.cloneDeep(experiment));
// Creates { <variationKey>: <variation> } map inside of the experiment
Expand All @@ -79,8 +79,7 @@ module.exports = {

// Creates { <variationId>: { key: <variationKey>, id: <variationId> } } mapping for quick lookup
fns.assign(projectConfig.variationIdMap, fns.keyBy(experiment.variations, 'id'));

fns.forOwn(experiment.variationKeyMap, function(variation) {
jsSdkUtils.objectValues(experiment.variationKeyMap || {}).forEach(function (variation) {
if (variation.variables) {
projectConfig.variationVariableUsageMap[variation.id] = fns.keyBy(variation.variables, 'id');
}
Expand All @@ -92,7 +91,7 @@ module.exports = {
projectConfig.experimentFeatureMap = {};

projectConfig.featureKeyMap = fns.keyBy(projectConfig.featureFlags || [], 'key');
fns.forOwn(projectConfig.featureKeyMap, function(feature) {
jsSdkUtils.objectValues(projectConfig.featureKeyMap || {}).forEach(function (feature) {
feature.variableKeyMap = fns.keyBy(feature.variables, 'key');
(feature.experimentIds || []).forEach(function(experimentId) {
// Add this experiment in experiment-feature map.
Expand Down Expand Up @@ -123,7 +122,7 @@ module.exports = {
getExperimentId: function(projectConfig, experimentKey) {
var experiment = projectConfig.experimentKeyMap[experimentKey];
if (!experiment) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
}
return experiment.id;
},
Expand All @@ -138,7 +137,7 @@ module.exports = {
getLayerId: function(projectConfig, experimentId) {
var experiment = projectConfig.experimentIdMap[experimentId];
if (!experiment) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
}
return experiment.layerId;
},
Expand All @@ -156,14 +155,14 @@ module.exports = {
if (attribute) {
if (hasReservedPrefix) {
logger.log(LOG_LEVEL.WARN,
sprintf('Attribute %s unexpectedly has reserved prefix %s; using attribute ID instead of reserved attribute name.', attributeKey, RESERVED_ATTRIBUTE_PREFIX));
jsSdkUtils.sprintf('Attribute %s unexpectedly has reserved prefix %s; using attribute ID instead of reserved attribute name.', attributeKey, RESERVED_ATTRIBUTE_PREFIX));
}
return attribute.id;
} else if (hasReservedPrefix) {
return attributeKey;
}

logger.log(LOG_LEVEL.DEBUG, sprintf(ERROR_MESSAGES.UNRECOGNIZED_ATTRIBUTE, MODULE_NAME, attributeKey));
logger.log(LOG_LEVEL.DEBUG, jsSdkUtils.sprintf(ERROR_MESSAGES.UNRECOGNIZED_ATTRIBUTE, MODULE_NAME, attributeKey));
return null;
},

Expand Down Expand Up @@ -191,7 +190,7 @@ module.exports = {
getExperimentStatus: function(projectConfig, experimentKey) {
var experiment = projectConfig.experimentKeyMap[experimentKey];
if (!experiment) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
}
return experiment.status;
},
Expand Down Expand Up @@ -225,7 +224,7 @@ module.exports = {
getExperimentAudienceConditions: function(projectConfig, experimentKey) {
var experiment = projectConfig.experimentKeyMap[experimentKey];
if (!experiment) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
}

return experiment.audienceConditions || experiment.audienceIds;
Expand Down Expand Up @@ -274,7 +273,7 @@ module.exports = {
}
}

throw new Error(sprintf(ERROR_MESSAGES.EXPERIMENT_KEY_NOT_IN_DATAFILE, MODULE_NAME, experimentKey));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.EXPERIMENT_KEY_NOT_IN_DATAFILE, MODULE_NAME, experimentKey));
},

/**
Expand All @@ -287,7 +286,7 @@ module.exports = {
getTrafficAllocation: function(projectConfig, experimentKey) {
var experiment = projectConfig.experimentKeyMap[experimentKey];
if (!experiment) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
}
return experiment.trafficAllocation;
},
Expand All @@ -307,7 +306,7 @@ module.exports = {
}
}

logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
return null;
},

Expand All @@ -328,7 +327,7 @@ module.exports = {
}
}

logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
return null;
},

Expand All @@ -346,13 +345,13 @@ module.exports = {
getVariableForFeature: function(projectConfig, featureKey, variableKey, logger) {
var feature = projectConfig.featureKeyMap[featureKey];
if (!feature) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
return null;
}

var variable = feature.variableKeyMap[variableKey];
if (!variable) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.VARIABLE_KEY_NOT_IN_DATAFILE, MODULE_NAME, variableKey, featureKey));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.VARIABLE_KEY_NOT_IN_DATAFILE, MODULE_NAME, variableKey, featureKey));
return null;
}

Expand All @@ -377,7 +376,7 @@ module.exports = {
}

if (!projectConfig.variationVariableUsageMap.hasOwnProperty(variation.id)) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT, MODULE_NAME, variation.id));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT, MODULE_NAME, variation.id));
return null;
}

Expand Down Expand Up @@ -409,7 +408,7 @@ module.exports = {
switch (variableType) {
case FEATURE_VARIABLE_TYPES.BOOLEAN:
if (variableValue !== 'true' && variableValue !== 'false') {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
castValue = null;
} else {
castValue = variableValue === 'true';
Expand All @@ -419,15 +418,15 @@ module.exports = {
case FEATURE_VARIABLE_TYPES.INTEGER:
castValue = parseInt(variableValue, 10);
if (isNaN(castValue)) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
castValue = null;
}
break;

case FEATURE_VARIABLE_TYPES.DOUBLE:
castValue = parseFloat(variableValue);
if (isNaN(castValue)) {
logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
castValue = null;
}
break;
Expand Down Expand Up @@ -485,10 +484,10 @@ module.exports = {
tryCreatingProjectConfig: function(config) {
configValidator.validateDatafile(config.datafile);
if (config.skipJSONValidation === true) {
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
config.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
} else if (config.jsonSchemaValidator) {
config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile);
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
config.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
}
return module.exports.createProjectConfig(config.datafile);
},
Expand Down
Loading