diff --git a/packages/optimizely-sdk/lib/core/audience_evaluator/index.js b/packages/optimizely-sdk/lib/core/audience_evaluator/index.js index ad2ef2bb0..7f7fcceaf 100644 --- a/packages/optimizely-sdk/lib/core/audience_evaluator/index.js +++ b/packages/optimizely-sdk/lib/core/audience_evaluator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018-2019 Optimizely + * Copyright 2016, 2018-2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var conditionTreeEvaluator = require('../condition_tree_evaluator'); -var customAttributeConditionEvaluator = require('../custom_attribute_condition_evaluator'); -var enums = require('../../utils/enums'); -var fns = require('../../utils/fns'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var logging = require('@optimizely/js-sdk-logging'); -var logger = logging.getLogger(); +import { sprintf } from '@optimizely/js-sdk-utils'; +import { getLogger } from '@optimizely/js-sdk-logging'; -var ERROR_MESSAGES = enums.ERROR_MESSAGES; -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; +import fns from '../../utils/fns'; +import { + LOG_LEVEL, + LOG_MESSAGES, + ERROR_MESSAGES, +} from '../../utils/enums'; +import conditionTreeEvaluator from '../condition_tree_evaluator'; +import customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator'; + +var logger = getLogger(); var MODULE_NAME = 'AUDIENCE_EVALUATOR'; /** @@ -108,4 +110,4 @@ AudienceEvaluator.prototype.evaluateConditionWithUserAttributes = function(userA return null; }; -module.exports = AudienceEvaluator; +export default AudienceEvaluator; diff --git a/packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js b/packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js index b9cea6baf..abc6fc377 100644 --- a/packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js +++ b/packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018-2019 Optimizely + * Copyright 2016, 2018-2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var AudienceEvaluator = require('./'); -var chai = require('chai'); -var conditionTreeEvaluator = require('../condition_tree_evaluator'); -var customAttributeConditionEvaluator = require('../custom_attribute_condition_evaluator'); -var sinon = require('sinon'); -var assert = chai.assert; -var logging = require('@optimizely/js-sdk-logging'); -var mockLogger = logging.getLogger(); -var enums = require('../../utils/enums'); -var LOG_LEVEL = enums.LOG_LEVEL; +import sinon from 'sinon'; +import { assert } from 'chai'; +import { getLogger } from '@optimizely/js-sdk-logging'; + +import AudienceEvaluator from './index'; +import conditionTreeEvaluator from '../condition_tree_evaluator'; +import customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator'; + +var mockLogger = getLogger(); var chromeUserAudience = { conditions: [ diff --git a/packages/optimizely-sdk/lib/core/bucketer/index.js b/packages/optimizely-sdk/lib/core/bucketer/index.js index 116801507..0b458ca15 100644 --- a/packages/optimizely-sdk/lib/core/bucketer/index.js +++ b/packages/optimizely-sdk/lib/core/bucketer/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2019 Optimizely + * Copyright 2016, 2019-2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,179 +17,186 @@ /** * Bucketer API for determining the variation id from the specified parameters */ -var enums = require('../../utils/enums'); -var murmurhash = require('murmurhash'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; +import { sprintf } from '@optimizely/js-sdk-utils'; +import murmurhash from 'murmurhash'; + +import { + ERROR_MESSAGES, + LOG_LEVEL, + LOG_MESSAGES, +} from '../../utils/enums'; -var ERROR_MESSAGES = enums.ERROR_MESSAGES; var HASH_SEED = 1; -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; var MAX_HASH_VALUE = Math.pow(2, 32); var MAX_TRAFFIC_VALUE = 10000; var MODULE_NAME = 'BUCKETER'; var RANDOM_POLICY = 'random'; -module.exports = { - /** - * Determines ID of variation to be shown for the given input params - * @param {Object} bucketerParams - * @param {string} bucketerParams.experimentId - * @param {string} bucketerParams.experimentKey - * @param {string} bucketerParams.userId - * @param {Object[]} bucketerParams.trafficAllocationConfig - * @param {Array} bucketerParams.experimentKeyMap - * @param {Object} bucketerParams.groupIdMap - * @param {Object} bucketerParams.variationIdMap - * @param {string} bucketerParams.varationIdMap[].key - * @param {Object} bucketerParams.logger - * @param {string} bucketerParams.bucketingId - * @return Variation ID that user has been bucketed into, null if user is not bucketed into any experiment - */ - bucket: function(bucketerParams) { - // Check if user is in a random group; if so, check if user is bucketed into a specific experiment - var experiment = bucketerParams.experimentKeyMap[bucketerParams.experimentKey]; - var groupId = experiment['groupId']; - if (groupId) { - var group = bucketerParams.groupIdMap[groupId]; - if (!group) { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_GROUP_ID, MODULE_NAME, groupId)); - } - if (group.policy === RANDOM_POLICY) { - var bucketedExperimentId = module.exports.bucketUserIntoExperiment( - group, - bucketerParams.bucketingId, +/** + * Determines ID of variation to be shown for the given input params + * @param {Object} bucketerParams + * @param {string} bucketerParams.experimentId + * @param {string} bucketerParams.experimentKey + * @param {string} bucketerParams.userId + * @param {Object[]} bucketerParams.trafficAllocationConfig + * @param {Array} bucketerParams.experimentKeyMap + * @param {Object} bucketerParams.groupIdMap + * @param {Object} bucketerParams.variationIdMap + * @param {string} bucketerParams.varationIdMap[].key + * @param {Object} bucketerParams.logger + * @param {string} bucketerParams.bucketingId + * @return Variation ID that user has been bucketed into, null if user is not bucketed into any experiment + */ +export var bucket = function(bucketerParams) { + // Check if user is in a random group; if so, check if user is bucketed into a specific experiment + var experiment = bucketerParams.experimentKeyMap[bucketerParams.experimentKey]; + var groupId = experiment['groupId']; + if (groupId) { + var group = bucketerParams.groupIdMap[groupId]; + if (!group) { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_GROUP_ID, MODULE_NAME, groupId)); + } + if (group.policy === RANDOM_POLICY) { + var bucketedExperimentId = this.bucketUserIntoExperiment( + group, + bucketerParams.bucketingId, + bucketerParams.userId, + bucketerParams.logger + ); + + // Return if user is not bucketed into any experiment + if (bucketedExperimentId === null) { + var notbucketedInAnyExperimentLogMessage = sprintf( + LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, + MODULE_NAME, bucketerParams.userId, - bucketerParams.logger + groupId ); + bucketerParams.logger.log(LOG_LEVEL.INFO, notbucketedInAnyExperimentLogMessage); + return null; + } - // Return if user is not bucketed into any experiment - if (bucketedExperimentId === null) { - var notbucketedInAnyExperimentLogMessage = sprintf( - LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, - MODULE_NAME, - bucketerParams.userId, - groupId - ); - bucketerParams.logger.log(LOG_LEVEL.INFO, notbucketedInAnyExperimentLogMessage); - return null; - } - - // Return if user is bucketed into a different experiment than the one specified - if (bucketedExperimentId !== bucketerParams.experimentId) { - var notBucketedIntoExperimentOfGroupLogMessage = sprintf( - LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP, - MODULE_NAME, - bucketerParams.userId, - bucketerParams.experimentKey, - groupId - ); - bucketerParams.logger.log(LOG_LEVEL.INFO, notBucketedIntoExperimentOfGroupLogMessage); - return null; - } - - // Continue bucketing if user is bucketed into specified experiment - var bucketedIntoExperimentOfGroupLogMessage = sprintf( - LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP, + // Return if user is bucketed into a different experiment than the one specified + if (bucketedExperimentId !== bucketerParams.experimentId) { + var notBucketedIntoExperimentOfGroupLogMessage = sprintf( + LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey, groupId ); - bucketerParams.logger.log(LOG_LEVEL.INFO, bucketedIntoExperimentOfGroupLogMessage); + bucketerParams.logger.log(LOG_LEVEL.INFO, notBucketedIntoExperimentOfGroupLogMessage); + return null; } - } - var bucketingId = sprintf('%s%s', bucketerParams.bucketingId, bucketerParams.experimentId); - var bucketValue = module.exports._generateBucketValue(bucketingId); - - var bucketedUserLogMessage = sprintf( - LOG_MESSAGES.USER_ASSIGNED_TO_VARIATION_BUCKET, - MODULE_NAME, - bucketValue, - bucketerParams.userId - ); - bucketerParams.logger.log(LOG_LEVEL.DEBUG, bucketedUserLogMessage); - var entityId = module.exports._findBucket(bucketValue, bucketerParams.trafficAllocationConfig); - if (!entityId) { - var userHasNoVariationLogMessage = sprintf( - LOG_MESSAGES.USER_HAS_NO_VARIATION, - MODULE_NAME, - bucketerParams.userId, - bucketerParams.experimentKey - ); - bucketerParams.logger.log(LOG_LEVEL.DEBUG, userHasNoVariationLogMessage); - } else if (!bucketerParams.variationIdMap.hasOwnProperty(entityId)) { - var invalidVariationIdLogMessage = sprintf(LOG_MESSAGES.INVALID_VARIATION_ID, MODULE_NAME); - bucketerParams.logger.log(LOG_LEVEL.WARNING, invalidVariationIdLogMessage); - return null; - } else { - var variationKey = bucketerParams.variationIdMap[entityId].key; - var userInVariationLogMessage = sprintf( - LOG_MESSAGES.USER_HAS_VARIATION, + // Continue bucketing if user is bucketed into specified experiment + var bucketedIntoExperimentOfGroupLogMessage = sprintf( + LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, - variationKey, - bucketerParams.experimentKey + bucketerParams.experimentKey, + groupId ); - bucketerParams.logger.log(LOG_LEVEL.INFO, userInVariationLogMessage); + bucketerParams.logger.log(LOG_LEVEL.INFO, bucketedIntoExperimentOfGroupLogMessage); } + } + var bucketingId = sprintf('%s%s', bucketerParams.bucketingId, bucketerParams.experimentId); + var bucketValue = this._generateBucketValue(bucketingId); - return entityId; - }, + var bucketedUserLogMessage = sprintf( + LOG_MESSAGES.USER_ASSIGNED_TO_VARIATION_BUCKET, + MODULE_NAME, + bucketValue, + bucketerParams.userId + ); + bucketerParams.logger.log(LOG_LEVEL.DEBUG, bucketedUserLogMessage); - /** - * Returns bucketed experiment ID to compare against experiment user is being called into - * @param {Object} group Group that experiment is in - * @param {string} bucketingId Bucketing ID - * @param {string} userId ID of user to be bucketed into experiment - * @param {Object} logger Logger implementation - * @return {string} ID of experiment if user is bucketed into experiment within the group, null otherwise - */ - bucketUserIntoExperiment: function(group, bucketingId, userId, logger) { - var bucketingKey = sprintf('%s%s', bucketingId, group.id); - var bucketValue = module.exports._generateBucketValue(bucketingKey); - logger.log( - LOG_LEVEL.DEBUG, - sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, MODULE_NAME, bucketValue, userId) + var entityId = this._findBucket(bucketValue, bucketerParams.trafficAllocationConfig); + if (!entityId) { + var userHasNoVariationLogMessage = sprintf( + LOG_MESSAGES.USER_HAS_NO_VARIATION, + MODULE_NAME, + bucketerParams.userId, + bucketerParams.experimentKey ); - var trafficAllocationConfig = group.trafficAllocation; - var bucketedExperimentId = module.exports._findBucket(bucketValue, trafficAllocationConfig); - return bucketedExperimentId; - }, - - /** - * Returns entity ID associated with bucket value - * @param {string} bucketValue - * @param {Object[]} trafficAllocationConfig - * @param {number} trafficAllocationConfig[].endOfRange - * @param {number} trafficAllocationConfig[].entityId - * @return {string} Entity ID for bucketing if bucket value is within traffic allocation boundaries, null otherwise - */ - _findBucket: function(bucketValue, trafficAllocationConfig) { - for (var i = 0; i < trafficAllocationConfig.length; i++) { - if (bucketValue < trafficAllocationConfig[i].endOfRange) { - return trafficAllocationConfig[i].entityId; - } - } + bucketerParams.logger.log(LOG_LEVEL.DEBUG, userHasNoVariationLogMessage); + } else if (!bucketerParams.variationIdMap.hasOwnProperty(entityId)) { + var invalidVariationIdLogMessage = sprintf(LOG_MESSAGES.INVALID_VARIATION_ID, MODULE_NAME); + bucketerParams.logger.log(LOG_LEVEL.WARNING, invalidVariationIdLogMessage); return null; - }, + } else { + var variationKey = bucketerParams.variationIdMap[entityId].key; + var userInVariationLogMessage = sprintf( + LOG_MESSAGES.USER_HAS_VARIATION, + MODULE_NAME, + bucketerParams.userId, + variationKey, + bucketerParams.experimentKey + ); + bucketerParams.logger.log(LOG_LEVEL.INFO, userInVariationLogMessage); + } + + return entityId; +}; + +/** + * Returns bucketed experiment ID to compare against experiment user is being called into + * @param {Object} group Group that experiment is in + * @param {string} bucketingId Bucketing ID + * @param {string} userId ID of user to be bucketed into experiment + * @param {Object} logger Logger implementation + * @return {string} ID of experiment if user is bucketed into experiment within the group, null otherwise + */ +export var bucketUserIntoExperiment = function(group, bucketingId, userId, logger) { + var bucketingKey = sprintf('%s%s', bucketingId, group.id); + var bucketValue = this._generateBucketValue(bucketingKey); + logger.log( + LOG_LEVEL.DEBUG, + sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, MODULE_NAME, bucketValue, userId) + ); + var trafficAllocationConfig = group.trafficAllocation; + var bucketedExperimentId = this._findBucket(bucketValue, trafficAllocationConfig); + return bucketedExperimentId; +}; - /** - * Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE) - * @param {string} bucketingKey String value for bucketing - * @return {string} the generated bucket value - * @throws If bucketing value is not a valid string - */ - _generateBucketValue: function(bucketingKey) { - try { - // NOTE: the mmh library already does cast the hash value as an unsigned 32bit int - // https://github.com/perezd/node-murmurhash/blob/master/murmurhash.js#L115 - var hashValue = murmurhash.v3(bucketingKey, HASH_SEED); - var ratio = hashValue / MAX_HASH_VALUE; - return parseInt(ratio * MAX_TRAFFIC_VALUE, 10); - } catch (ex) { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_BUCKETING_ID, MODULE_NAME, bucketingKey, ex.message)); +/** + * Returns entity ID associated with bucket value + * @param {string} bucketValue + * @param {Object[]} trafficAllocationConfig + * @param {number} trafficAllocationConfig[].endOfRange + * @param {number} trafficAllocationConfig[].entityId + * @return {string} Entity ID for bucketing if bucket value is within traffic allocation boundaries, null otherwise + */ +export var _findBucket = function(bucketValue, trafficAllocationConfig) { + for (var i = 0; i < trafficAllocationConfig.length; i++) { + if (bucketValue < trafficAllocationConfig[i].endOfRange) { + return trafficAllocationConfig[i].entityId; } - }, + } + return null; +}; + +/** + * Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE) + * @param {string} bucketingKey String value for bucketing + * @return {string} the generated bucket value + * @throws If bucketing value is not a valid string + */ +export var _generateBucketValue = function(bucketingKey) { + try { + // NOTE: the mmh library already does cast the hash value as an unsigned 32bit int + // https://github.com/perezd/node-murmurhash/blob/master/murmurhash.js#L115 + var hashValue = murmurhash.v3(bucketingKey, HASH_SEED); + var ratio = hashValue / MAX_HASH_VALUE; + return parseInt(ratio * MAX_TRAFFIC_VALUE, 10); + } catch (ex) { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_BUCKETING_ID, MODULE_NAME, bucketingKey, ex.message)); + } +}; + +export default { + bucket: bucket, + bucketUserIntoExperiment: bucketUserIntoExperiment, + _findBucket: _findBucket, + _generateBucketValue: _generateBucketValue, }; diff --git a/packages/optimizely-sdk/lib/core/bucketer/index.tests.js b/packages/optimizely-sdk/lib/core/bucketer/index.tests.js index e0a142a89..b2c45784f 100644 --- a/packages/optimizely-sdk/lib/core/bucketer/index.tests.js +++ b/packages/optimizely-sdk/lib/core/bucketer/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017, 2019 Optimizely + * Copyright 2016-2017, 2019-2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,22 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var bucketer = require('./'); -var enums = require('../../utils/enums'); -var logger = require('../../plugins/logger'); -var projectConfig = require('../project_config'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var testData = require('../../tests/test_data').getTestProjectConfig(); - -var chai = require('chai'); -var assert = chai.assert; -var expect = chai.expect; -var cloneDeep = require('lodash/cloneDeep'); -var sinon = require('sinon'); - -var ERROR_MESSAGES = enums.ERROR_MESSAGES; -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; +import sinon from 'sinon'; +import { assert, expect } from 'chai'; +import { cloneDeep } from 'lodash'; +import { sprintf } from '@optimizely/js-sdk-utils'; + +import bucketer from './'; +import { + ERROR_MESSAGES, + LOG_MESSAGES, + LOG_LEVEL, +} from '../../utils/enums'; +import logger from '../../plugins/logger'; +import projectConfig from '../project_config'; +import { getTestProjectConfig } from '../../tests/test_data'; + +var testData = getTestProjectConfig(); describe('lib/core/bucketer', function() { describe('APIs', function() { @@ -365,13 +365,14 @@ describe('lib/core/bucketer', function() { }); describe('testBucketWithBucketingId', function() { + var bucketerParams; var createdLogger = logger.createLogger({ logLevel: LOG_LEVEL.INFO, logToConsole: false, }); beforeEach(function() { - configObj = projectConfig.createProjectConfig(cloneDeep(testData)); + var configObj = projectConfig.createProjectConfig(cloneDeep(testData)); bucketerParams = { trafficAllocationConfig: configObj.experiments[0].trafficAllocation, variationIdMap: configObj.variationIdMap, diff --git a/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.js b/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.js index 0a47c800c..a773c6070 100644 --- a/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.js +++ b/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2018, Optimizely, Inc. and contributors * + * Copyright 2018, 2020, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -32,7 +32,7 @@ var DEFAULT_OPERATOR_TYPES = [AND_CONDITION, OR_CONDITION, NOT_CONDITION]; * indicates that the conditions are invalid or unable to be * evaluated */ -function evaluate(conditions, leafEvaluator) { +export var evaluate = function(conditions, leafEvaluator) { if (Array.isArray(conditions)) { var firstOperator = conditions[0]; var restOfConditions = conditions.slice(1); @@ -56,7 +56,7 @@ function evaluate(conditions, leafEvaluator) { var leafCondition = conditions; return leafEvaluator(leafCondition); -} +}; /** * Evaluates an array of conditions as if the evaluator had been applied @@ -121,6 +121,6 @@ function orEvaluator(conditions, leafEvaluator) { return sawNullResult ? null : false; } -module.exports = { +export default { evaluate: evaluate, }; diff --git a/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.tests.js b/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.tests.js index c9af1ea43..798ffeede 100644 --- a/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.tests.js +++ b/packages/optimizely-sdk/lib/core/condition_tree_evaluator/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2018, Optimizely, Inc. and contributors * + * Copyright 2018, 2020, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ +import sinon from 'sinon'; +import { assert } from 'chai'; -var chai = require('chai'); -var sinon = require('sinon'); -var assert = chai.assert; -var conditionTreeEvaluator = require('./'); + import conditionTreeEvaluator from './'; var conditionA = { name: 'browser_type', diff --git a/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.js b/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.js index a70cd3dd0..09dff134a 100644 --- a/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.js +++ b/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2018-2019, Optimizely, Inc. and contributors * + * Copyright 2018-2019, 2020 Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -13,13 +13,14 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ +import { sprintf } from '@optimizely/js-sdk-utils'; -var fns = require('../../utils/fns'); -var enums = require('../../utils/enums'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; +import fns from '../../utils/fns'; +import { + LOG_LEVEL, + LOG_MESSAGES, +} from '../../utils/enums'; -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; var MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR'; var EXACT_MATCH_TYPE = 'exact'; @@ -53,7 +54,7 @@ EVALUATORS_BY_MATCH_TYPE[SUBSTRING_MATCH_TYPE] = substringEvaluator; * null if the given user attributes and condition can't be evaluated * TODO: Change to accept and object with named properties */ -function evaluate(condition, userAttributes, logger) { +export var evaluate = function(condition, userAttributes, logger) { var conditionMatch = condition.match; if (typeof conditionMatch !== 'undefined' && MATCH_TYPES.indexOf(conditionMatch) === -1) { logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.UNKNOWN_MATCH_TYPE, MODULE_NAME, JSON.stringify(condition))); @@ -71,7 +72,7 @@ function evaluate(condition, userAttributes, logger) { var evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch] || exactEvaluator; return evaluatorForMatch(condition, userAttributes, logger); -} +}; /** * Returns true if the value is valid for exact conditions. Valid values include @@ -298,6 +299,6 @@ function substringEvaluator(condition, userAttributes, logger) { return userValue.indexOf(conditionValue) !== -1; } -module.exports = { +export default { evaluate: evaluate, }; diff --git a/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.tests.js b/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.tests.js index a56b88693..8de65e667 100644 --- a/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.tests.js +++ b/packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2018-2019, Optimizely, Inc. and contributors * + * Copyright 2018-2020, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ +import sinon from 'sinon'; +import { assert } from 'chai'; -var customAttributeEvaluator = require('./'); -var enums = require('../../utils/enums'); -var LOG_LEVEL = enums.LOG_LEVEL; -var logger = require('../../plugins/logger'); - -var chai = require('chai'); -var sinon = require('sinon'); -var assert = chai.assert; +import { LOG_LEVEL } from '../../utils/enums'; +import logger from '../../plugins/logger'; +import customAttributeEvaluator from './'; var browserConditionSafari = { name: 'browser_type', diff --git a/packages/optimizely-sdk/lib/core/decision_service/index.js b/packages/optimizely-sdk/lib/core/decision_service/index.js index 245658f9e..6c4ab46e6 100644 --- a/packages/optimizely-sdk/lib/core/decision_service/index.js +++ b/packages/optimizely-sdk/lib/core/decision_service/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2017-2019, Optimizely, Inc. and contributors * + * Copyright 2017-2020 Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -13,15 +13,14 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ +import { sprintf } from'@optimizely/js-sdk-utils'; -var AudienceEvaluator = require('../audience_evaluator'); -var bucketer = require('../bucketer'); -var enums = require('../../utils/enums'); -var fns = require('../../utils/fns'); -var projectConfig = require('../project_config'); -var stringValidator = require('../../utils/string_value_validator'); - -var sprintf = require('@optimizely/js-sdk-utils').sprintf; +import bucketer from '../bucketer'; +import enums from '../../utils/enums'; +import fns from '../../utils/fns'; +import projectConfig from '../project_config'; +import AudienceEvaluator from '../audience_evaluator'; +import stringValidator from '../../utils/string_value_validator'; var MODULE_NAME = 'DECISION_SERVICE'; var ERROR_MESSAGES = enums.ERROR_MESSAGES; @@ -718,15 +717,17 @@ DecisionService.prototype.setForcedVariation = function(configObj, experimentKey } }; -module.exports = { - /** - * Creates an instance of the DecisionService. - * @param {Object} options Configuration options - * @param {Object} options.userProfileService - * @param {Object} options.logger - * @return {Object} An instance of the DecisionService - */ - createDecisionService: function(options) { - return new DecisionService(options); - }, +/** + * Creates an instance of the DecisionService. + * @param {Object} options Configuration options + * @param {Object} options.userProfileService + * @param {Object} options.logger + * @return {Object} An instance of the DecisionService + */ +export var createDecisionService = function(options) { + return new DecisionService(options); +}; + +export default { + createDecisionService: createDecisionService, }; diff --git a/packages/optimizely-sdk/lib/core/decision_service/index.tests.js b/packages/optimizely-sdk/lib/core/decision_service/index.tests.js index 72813fe13..96bd3bdb9 100644 --- a/packages/optimizely-sdk/lib/core/decision_service/index.tests.js +++ b/packages/optimizely-sdk/lib/core/decision_service/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2017-2020, Optimizely, Inc. and contributors * + * Copyright 2017-2020 Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -13,30 +13,33 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ - -var Optimizely = require('../../optimizely').default; -var eventBuilder = require('../../core/event_builder/index.js'); -var eventDispatcher = require('../../plugins/event_dispatcher/index.node'); -var errorHandler = require('../../plugins/error_handler'); -var bucketer = require('../bucketer'); -var DecisionService = require('./'); -var enums = require('../../utils/enums'); -var cloneDeep = require('lodash/cloneDeep'); -var logger = require('../../plugins/logger'); -var projectConfig = require('../project_config'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var testData = require('../../tests/test_data').getTestProjectConfig(); -var testDataWithFeatures = require('../../tests/test_data').getTestProjectConfigWithFeatures(); -var jsonSchemaValidator = require('../../utils/json_schema_validator'); -var AudienceEvaluator = require('../audience_evaluator'); - -var chai = require('chai'); -var sinon = require('sinon'); -var assert = chai.assert; - -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; -var DECISION_SOURCES = enums.DECISION_SOURCES; +import sinon from 'sinon'; +import { assert } from 'chai'; +import cloneDeep from 'lodash/cloneDeep'; +import { sprintf } from '@optimizely/js-sdk-utils'; + +import DecisionService from './'; +import bucketer from '../bucketer'; +import { + LOG_LEVEL, + LOG_MESSAGES, + DECISION_SOURCES, +} from '../../utils/enums'; +import logger from '../../plugins/logger'; +import Optimizely from '../../optimizely'; +import projectConfig from '../project_config'; +import AudienceEvaluator from '../audience_evaluator'; +import errorHandler from '../../plugins/error_handler'; +import eventBuilder from '../../core/event_builder/index.js'; +import eventDispatcher from '../../plugins/event_dispatcher/index.node'; +import jsonSchemaValidator from '../../utils/json_schema_validator'; +import { + getTestProjectConfig, + getTestProjectConfigWithFeatures, +} from '../../tests/test_data'; + +var testData = getTestProjectConfig(); +var testDataWithFeatures = getTestProjectConfigWithFeatures(); describe('lib/core/decision_service', function() { describe('APIs', function() {