Skip to content

Commit e76a20d

Browse files
refactor: Convert lib/optimizely/* and lib/tests/* to ES module (#448)
Summary: This PR only converts contents inside lib/optimizely/* and lib/tests/* to ESModule. Test plan: All unit test and Full stack compatibility suite tests pass Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com> Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com>
1 parent b141a94 commit e76a20d

File tree

5 files changed

+83
-86
lines changed

5 files changed

+83
-86
lines changed

packages/optimizely-sdk/lib/core/decision_service/index.tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -14,7 +14,7 @@
1414
* limitations under the License. *
1515
***************************************************************************/
1616

17-
var Optimizely = require('../../optimizely');
17+
var Optimizely = require('../../optimizely').default;
1818
var eventBuilder = require('../../core/event_builder/index.js');
1919
var eventDispatcher = require('../../plugins/event_dispatcher/index.node');
2020
var errorHandler = require('../../plugins/error_handler');

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -13,21 +13,21 @@
1313
* See the License for the specific language governing permissions and *
1414
* limitations under the License. *
1515
***************************************************************************/
16-
17-
var fns = require('../utils/fns');
18-
var attributesValidator = require('../utils/attributes_validator');
19-
var decisionService = require('../core/decision_service');
20-
var enums = require('../utils/enums');
21-
var eventBuilder = require('../core/event_builder/index.js');
22-
var eventHelpers = require('../core/event_builder/event_helpers');
23-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
24-
var eventTagsValidator = require('../utils/event_tags_validator');
25-
var notificationCenter = require('../core/notification_center');
26-
var projectConfig = require('../core/project_config');
27-
var jsSdkUtils = require('@optimizely/js-sdk-utils');
28-
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
29-
var stringValidator = require('../utils/string_value_validator');
30-
var projectConfigManager = require('../core/project_config/project_config_manager');
16+
import { sprintf, objectValues } from '@optimizely/js-sdk-utils';
17+
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
18+
19+
import fns from '../utils/fns'
20+
import attributesValidator from '../utils/attributes_validator';
21+
import decisionService from '../core/decision_service';
22+
import enums from '../utils/enums';
23+
import eventBuilder from '../core/event_builder/index.js';
24+
import eventHelpers from '../core/event_builder/event_helpers';
25+
import eventTagsValidator from '../utils/event_tags_validator';
26+
import notificationCenter from '../core/notification_center';
27+
import projectConfig from '../core/project_config';
28+
import userProfileServiceValidator from '../utils/user_profile_service_validator';
29+
import stringValidator from '../utils/string_value_validator';
30+
import projectConfigManager from '../core/project_config/project_config_manager';
3131

3232
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
3333
var LOG_LEVEL = enums.LOG_LEVEL;
@@ -58,7 +58,7 @@ function Optimizely(config) {
5858
if (enums.VALID_CLIENT_ENGINES.indexOf(clientEngine) === -1) {
5959
config.logger.log(
6060
LOG_LEVEL.INFO,
61-
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine)
61+
sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine)
6262
);
6363
clientEngine = enums.NODE_CLIENT_ENGINE;
6464
}
@@ -81,7 +81,7 @@ function Optimizely(config) {
8181
function(configObj) {
8282
this.logger.log(
8383
LOG_LEVEL.INFO,
84-
jsSdkUtils.sprintf(LOG_MESSAGES.UPDATED_OPTIMIZELY_CONFIG, MODULE_NAME, configObj.revision, configObj.projectId)
84+
sprintf(LOG_MESSAGES.UPDATED_OPTIMIZELY_CONFIG, MODULE_NAME, configObj.revision, configObj.projectId)
8585
);
8686
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE);
8787
}.bind(this)
@@ -94,7 +94,7 @@ function Optimizely(config) {
9494
try {
9595
if (userProfileServiceValidator.validate(config.userProfileService)) {
9696
userProfileService = config.userProfileService;
97-
this.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
97+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
9898
}
9999
} catch (ex) {
100100
this.logger.log(LOG_LEVEL.WARNING, ex.message);
@@ -144,7 +144,7 @@ Optimizely.prototype.__isValidInstance = function() {
144144
Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
145145
try {
146146
if (!this.__isValidInstance()) {
147-
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'activate'));
147+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'activate'));
148148
return null;
149149
}
150150

@@ -165,7 +165,7 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
165165

166166
// If experiment is not set to 'Running' status, log accordingly and return variation key
167167
if (!projectConfig.isRunning(configObj, experimentKey)) {
168-
var shouldNotDispatchActivateLogMessage = jsSdkUtils.sprintf(
168+
var shouldNotDispatchActivateLogMessage = sprintf(
169169
LOG_MESSAGES.SHOULD_NOT_DISPATCH_ACTIVATE,
170170
MODULE_NAME,
171171
experimentKey
@@ -179,7 +179,7 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
179179
return variationKey;
180180
} catch (ex) {
181181
this.logger.log(LOG_LEVEL.ERROR, ex.message);
182-
var failedActivationLogMessage = jsSdkUtils.sprintf(
182+
var failedActivationLogMessage = sprintf(
183183
LOG_MESSAGES.NOT_ACTIVATING_USER,
184184
MODULE_NAME,
185185
userId,
@@ -275,7 +275,7 @@ Optimizely.prototype.__emitNotificationCenterActivate = function(experimentKey,
275275
Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
276276
try {
277277
if (!this.__isValidInstance()) {
278-
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'track'));
278+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'track'));
279279
return;
280280
}
281281

@@ -291,9 +291,9 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
291291
if (!projectConfig.eventWithKeyExists(configObj, eventKey)) {
292292
this.logger.log(
293293
LOG_LEVEL.WARNING,
294-
jsSdkUtils.sprintf(enums.LOG_MESSAGES.EVENT_KEY_NOT_FOUND, MODULE_NAME, eventKey)
294+
sprintf(enums.LOG_MESSAGES.EVENT_KEY_NOT_FOUND, MODULE_NAME, eventKey)
295295
);
296-
this.logger.log(LOG_LEVEL.WARNING, jsSdkUtils.sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId));
296+
this.logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId));
297297
return;
298298
}
299299

@@ -308,14 +308,14 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
308308
clientVersion: this.clientVersion,
309309
configObj: configObj,
310310
});
311-
this.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(enums.LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId));
311+
this.logger.log(LOG_LEVEL.INFO, sprintf(enums.LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId));
312312
// TODO is it okay to not pass a projectConfig as second argument
313313
this.eventProcessor.process(conversionEvent);
314314
this.__emitNotificationCenterTrack(eventKey, userId, attributes, eventTags);
315315
} catch (e) {
316316
this.logger.log(LOG_LEVEL.ERROR, e.message);
317317
this.errorHandler.handleError(e);
318-
var failedTrackLogMessage = jsSdkUtils.sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId);
318+
var failedTrackLogMessage = sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId);
319319
this.logger.log(LOG_LEVEL.ERROR, failedTrackLogMessage);
320320
}
321321
};
@@ -369,7 +369,7 @@ Optimizely.prototype.__emitNotificationCenterTrack = function(eventKey, userId,
369369
Optimizely.prototype.getVariation = function(experimentKey, userId, attributes) {
370370
try {
371371
if (!this.__isValidInstance()) {
372-
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getVariation'));
372+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getVariation'));
373373
return null;
374374
}
375375

@@ -387,7 +387,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
387387
if (!experiment) {
388388
this.logger.log(
389389
LOG_LEVEL.DEBUG,
390-
jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey)
390+
sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey)
391391
);
392392
return null;
393393
}
@@ -485,7 +485,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
485485
if (stringInputs.hasOwnProperty('user_id')) {
486486
var userId = stringInputs.user_id;
487487
if (typeof userId !== 'string' || userId === null || userId === 'undefined') {
488-
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, 'user_id'));
488+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, 'user_id'));
489489
}
490490

491491
delete stringInputs.user_id;
@@ -495,7 +495,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
495495
for (var index = 0; index < inputKeys.length; index++) {
496496
var key = inputKeys[index];
497497
if (!stringValidator.validate(stringInputs[key])) {
498-
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
498+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
499499
}
500500
}
501501
if (userAttributes) {
@@ -519,7 +519,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
519519
* @return {null}
520520
*/
521521
Optimizely.prototype.__notActivatingExperiment = function(experimentKey, userId) {
522-
var failedActivationLogMessage = jsSdkUtils.sprintf(
522+
var failedActivationLogMessage = sprintf(
523523
LOG_MESSAGES.NOT_ACTIVATING_USER,
524524
MODULE_NAME,
525525
userId,
@@ -555,7 +555,7 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
555555
if (!this.__isValidInstance()) {
556556
this.logger.log(
557557
LOG_LEVEL.ERROR,
558-
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'isFeatureEnabled')
558+
sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'isFeatureEnabled')
559559
);
560560
return false;
561561
}
@@ -594,12 +594,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
594594
if (featureEnabled === true) {
595595
this.logger.log(
596596
LOG_LEVEL.INFO,
597-
jsSdkUtils.sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
597+
sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
598598
);
599599
} else {
600600
this.logger.log(
601601
LOG_LEVEL.INFO,
602-
jsSdkUtils.sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
602+
sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
603603
);
604604
featureEnabled = false;
605605
}
@@ -639,7 +639,7 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
639639
if (!this.__isValidInstance()) {
640640
this.logger.log(
641641
LOG_LEVEL.ERROR,
642-
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getEnabledFeatures')
642+
sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getEnabledFeatures')
643643
);
644644
return enabledFeatures;
645645
}
@@ -653,7 +653,7 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
653653
return enabledFeatures;
654654
}
655655

656-
jsSdkUtils.objectValues(configObj.featureKeyMap).forEach(
656+
objectValues(configObj.featureKeyMap).forEach(
657657
function(feature) {
658658
if (this.isFeatureEnabled(feature.key, userId, attributes)) {
659659
enabledFeatures.push(feature.key);
@@ -721,7 +721,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
721721
var apiName = variableType
722722
? 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1)
723723
: 'getFeatureVariable';
724-
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
724+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
725725
return null;
726726
}
727727

@@ -749,7 +749,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
749749
} else if (variable.type !== variableType) {
750750
this.logger.log(
751751
LOG_LEVEL.WARNING,
752-
jsSdkUtils.sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
752+
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
753753
);
754754
return null;
755755
}
@@ -766,7 +766,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
766766
variableValue = value;
767767
this.logger.log(
768768
LOG_LEVEL.INFO,
769-
jsSdkUtils.sprintf(
769+
sprintf(
770770
LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE,
771771
MODULE_NAME,
772772
variableKey,
@@ -778,7 +778,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
778778
} else {
779779
this.logger.log(
780780
LOG_LEVEL.INFO,
781-
jsSdkUtils.sprintf(
781+
sprintf(
782782
LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE,
783783
MODULE_NAME,
784784
featureFlag.key,
@@ -790,7 +790,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
790790
} else {
791791
this.logger.log(
792792
LOG_LEVEL.INFO,
793-
jsSdkUtils.sprintf(
793+
sprintf(
794794
LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE,
795795
MODULE_NAME,
796796
variableKey,
@@ -801,7 +801,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
801801
} else {
802802
this.logger.log(
803803
LOG_LEVEL.INFO,
804-
jsSdkUtils.sprintf(
804+
sprintf(
805805
LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE,
806806
MODULE_NAME,
807807
userId,
@@ -1103,7 +1103,7 @@ Optimizely.prototype.onReady = function(options) {
11031103
delete this.__readyTimeouts[timeoutId];
11041104
resolveTimeoutPromise({
11051105
success: false,
1106-
reason: jsSdkUtils.sprintf('onReady timeout expired after %s ms', timeout),
1106+
reason: sprintf('onReady timeout expired after %s ms', timeout),
11071107
});
11081108
}.bind(this);
11091109
var readyTimeout = setTimeout(onReadyTimeout, timeout);
@@ -1132,4 +1132,4 @@ Optimizely.prototype.onReady = function(options) {
11321132
return Promise.race([this.__readyPromise, timeoutPromise]);
11331133
};
11341134

1135-
module.exports = Optimizely;
1135+
export default Optimizely;

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -13,31 +13,28 @@
1313
* See the License for the specific language governing permissions and *
1414
* limitations under the License. *
1515
***************************************************************************/
16-
17-
var Optimizely = require('./');
18-
var AudienceEvaluator = require('../core/audience_evaluator');
19-
var bluebird = require('bluebird');
20-
var bucketer = require('../core/bucketer');
21-
var projectConfigManager = require('../core/project_config/project_config_manager');
22-
var enums = require('../utils/enums');
23-
var eventBuilder = require('../core/event_builder/index.js');
24-
var eventDispatcher = require('../plugins/event_dispatcher/index.node');
25-
var eventProcessor = require('@optimizely/js-sdk-event-processor');
26-
var errorHandler = require('../plugins/error_handler');
27-
var fns = require('../utils/fns');
28-
var jsonSchemaValidator = require('../utils/json_schema_validator');
29-
var logger = require('../plugins/logger');
30-
var decisionService = require('../core/decision_service');
31-
var testData = require('../tests/test_data');
32-
var jsonSchemaValidator = require('../utils/json_schema_validator');
33-
var projectConfig = require('../core/project_config');
34-
var logging = require('@optimizely/js-sdk-logging');
35-
36-
var chai = require('chai');
37-
var assert = chai.assert;
38-
var sinon = require('sinon');
39-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
40-
var uuid = require('uuid');
16+
import { assert } from 'chai';
17+
import sinon from 'sinon';
18+
import uuid from 'uuid';
19+
import { sprintf } from '@optimizely/js-sdk-utils';
20+
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
21+
import * as logging from '@optimizely/js-sdk-logging';
22+
23+
import Optimizely from './';
24+
import AudienceEvaluator from '../core/audience_evaluator';
25+
import bluebird from 'bluebird';
26+
import bucketer from '../core/bucketer';
27+
import projectConfigManager from '../core/project_config/project_config_manager';
28+
import enums from '../utils/enums';
29+
import eventBuilder from '../core/event_builder/index.js';
30+
import eventDispatcher from '../plugins/event_dispatcher/index.node';
31+
import errorHandler from '../plugins/error_handler';
32+
import fns from '../utils/fns';
33+
import logger from '../plugins/logger';
34+
import decisionService from '../core/decision_service';
35+
import jsonSchemaValidator from '../utils/json_schema_validator';
36+
import projectConfig from '../core/project_config';
37+
import testData from '../tests/test_data';
4138

4239
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
4340
var LOG_LEVEL = enums.LOG_LEVEL;
@@ -6650,7 +6647,7 @@ describe('lib/optimizely', function() {
66506647
});
66516648

66526649
it('should instantiate the eventProcessor with the provided event flush interval and event batch size', function() {
6653-
optlyInstance = new Optimizely({
6650+
var optlyInstance = new Optimizely({
66546651
clientEngine: 'node-sdk',
66556652
errorHandler: errorHandler,
66566653
eventDispatcher: eventDispatcher,

0 commit comments

Comments
 (0)