From 801a505c7f97f4453201112c87d5eb15e74d72c2 Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 17:24:01 +0500 Subject: [PATCH 1/8] converted attributes_validator in utils --- .../lib/utils/attributes_validator/index.js | 68 ++++++++++--------- .../utils/attributes_validator/index.tests.js | 13 ++-- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js b/packages/optimizely-sdk/lib/utils/attributes_validator/index.js index a080e895d..516d2e356 100644 --- a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/attributes_validator/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. @@ -18,38 +18,42 @@ * Provides utility method for validating that the attributes user has provided are valid */ -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var fns = require('../../utils/fns'); +import { sprintf } from '@optimizely/js-sdk-utils'; + +import fns from '../../utils/fns'; +import { ERROR_MESSAGES } from '../enums'; -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; var MODULE_NAME = 'ATTRIBUTES_VALIDATOR'; -module.exports = { - /** - * Validates user's provided attributes - * @param {Object} attributes - * @return {boolean} True if the attributes are valid - * @throws If the attributes are not valid - */ - validate: function(attributes) { - if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) { - Object.keys(attributes).forEach(function(key) { - if (typeof attributes[key] === 'undefined') { - throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key)); - } - }); - return true; - } else { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME)); - } - }, +/** + * Validates user's provided attributes + * @param {Object} attributes + * @return {boolean} True if the attributes are valid + * @throws If the attributes are not valid + */ +export var validate = function(attributes) { + if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) { + Object.keys(attributes).forEach(function(key) { + if (typeof attributes[key] === 'undefined') { + throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key)); + } + }); + return true; + } else { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME)); + } +} + +export var isAttributeValid = function(attributeKey, attributeValue) { + return ( + typeof attributeKey === 'string' && + (typeof attributeValue === 'string' || + typeof attributeValue === 'boolean' || + (fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue))) + ); +} - isAttributeValid: function(attributeKey, attributeValue) { - return ( - typeof attributeKey === 'string' && - (typeof attributeValue === 'string' || - typeof attributeValue === 'boolean' || - (fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue))) - ); - }, -}; +export default { + validate: validate, + isAttributeValid: isAttributeValid, +} diff --git a/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js index 647695a72..f430ddb23 100644 --- a/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/attributes_validator/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,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var chai = require('chai'); -var assert = chai.assert; -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var attributesValidator = require('./'); -var fns = require('./../fns/'); +import { sprintf } from '@optimizely/js-sdk-utils'; -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; +import attributesValidator from './'; +import { ERROR_MESSAGES } from '../enums'; + +import { assert } from 'chai'; describe('lib/utils/attributes_validator', function() { describe('APIs', function() { From d30f58926dbad044e30a440771cc638b0ea5014e Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 17:34:37 +0500 Subject: [PATCH 2/8] converted config_validator --- .../lib/utils/config_validator/index.js | 111 +++++++++--------- .../lib/utils/config_validator/index.tests.js | 14 +-- 2 files changed, 64 insertions(+), 61 deletions(-) diff --git a/packages/optimizely-sdk/lib/utils/config_validator/index.js b/packages/optimizely-sdk/lib/utils/config_validator/index.js index 499916a9d..8edf45aeb 100644 --- a/packages/optimizely-sdk/lib/utils/config_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/config_validator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018, 2019 Optimizely + * Copyright 2016, 2018, 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,70 +13,73 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var sprintf = require('@optimizely/js-sdk-utils').sprintf; +import { sprintf } from '@optimizely/js-sdk-utils'; -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; -var MODULE_NAME = 'CONFIG_VALIDATOR'; -var DATAFILE_VERSIONS = require('../enums').DATAFILE_VERSIONS; +import { ERROR_MESSAGES } from '../enums'; +import { DATAFILE_VERSIONS } from '../enums'; +var MODULE_NAME = 'CONFIG_VALIDATOR'; var SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4]; /** - * Provides utility methods for validating that the configuration options are valid + * Validates the given config options + * @param {Object} config + * @param {Object} config.errorHandler + * @param {Object} config.eventDispatcher + * @param {Object} config.logger + * @return {Boolean} True if the config options are valid + * @throws If any of the config options are not valid */ -module.exports = { - /** - * Validates the given config options - * @param {Object} config - * @param {Object} config.errorHandler - * @param {Object} config.eventDispatcher - * @param {Object} config.logger - * @return {Boolean} True if the config options are valid - * @throws If any of the config options are not valid - */ - validate: function(config) { - if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME)); - } +export var validate = function(config) { + if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME)); + } - if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME)); - } + if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME)); + } - if (config.logger && typeof config.logger.log !== 'function') { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME)); - } + if (config.logger && typeof config.logger.log !== 'function') { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME)); + } - return true; - }, + return true; +} - /** - * Validates the datafile - * @param {string} datafile - * @return {Boolean} True if the datafile is valid - * @throws If the datafile is not valid for any of the following reasons: - - The datafile string is undefined - - The datafile string cannot be parsed as a JSON object - - The datafile version is not supported - */ - validateDatafile: function(datafile) { - if (!datafile) { - throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME)); - } +/** + * Validates the datafile + * @param {string} datafile + * @return {Boolean} True if the datafile is valid + * @throws If the datafile is not valid for any of the following reasons: + - The datafile string is undefined + - The datafile string cannot be parsed as a JSON object + - The datafile version is not supported + */ +export var validateDatafile = function(datafile) { + if (!datafile) { + throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME)); + } - if (typeof datafile === 'string' || datafile instanceof String) { - // Attempt to parse the datafile string - try { - datafile = JSON.parse(datafile); - } catch (ex) { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME)); - } + if (typeof datafile === 'string' || datafile instanceof String) { + // Attempt to parse the datafile string + try { + datafile = JSON.parse(datafile); + } catch (ex) { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME)); } + } - if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version)); - } + if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version)); + } + + return true; +} - return true; - }, -}; +/** + * Provides utility methods for validating that the configuration options are valid + */ +export default { + validate: validate, + validateDatafile: validateDatafile, +} diff --git a/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js index 1776bdc1f..4523ad8b3 100644 --- a/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018, 2019 Optimizely + * Copyright 2016, 2018, 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,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var chai = require('chai'); -var assert = chai.assert; -var configValidator = require('./'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var testData = require('../../tests/test_data'); +import { sprintf } from '@optimizely/js-sdk-utils'; -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; +import configValidator from './'; +import { ERROR_MESSAGES } from '../enums'; +import testData from '../../tests/test_data'; + +import { assert } from 'chai'; describe('lib/utils/config_validator', function() { describe('APIs', function() { From fd9c137a251d13200e51f4751b5b1ee867be3ffc Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 18:03:01 +0500 Subject: [PATCH 3/8] converted enums --- .../optimizely-sdk/lib/optimizely/index.js | 4 +- .../lib/utils/attributes_validator/index.js | 8 +-- .../optimizely-sdk/lib/utils/enums/index.js | 69 ++++++++++++------- .../lib/utils/enums/index.tests.js | 7 +- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/packages/optimizely-sdk/lib/optimizely/index.js b/packages/optimizely-sdk/lib/optimizely/index.js index 9b2d1e5e4..fc99ced13 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.js +++ b/packages/optimizely-sdk/lib/optimizely/index.js @@ -17,7 +17,7 @@ import { sprintf, objectValues } from '@optimizely/js-sdk-utils'; import * as eventProcessor from '@optimizely/js-sdk-event-processor'; import fns from '../utils/fns' -import attributesValidator from '../utils/attributes_validator'; +import { validate } from '../utils/attributes_validator'; import decisionService from '../core/decision_service'; import enums from '../utils/enums'; import eventBuilder from '../core/event_builder/index.js'; @@ -499,7 +499,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e } } if (userAttributes) { - attributesValidator.validate(userAttributes); + validate(userAttributes); } if (eventTags) { eventTagsValidator.validate(eventTags); diff --git a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js b/packages/optimizely-sdk/lib/utils/attributes_validator/index.js index 516d2e356..2dc5836ea 100644 --- a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/attributes_validator/index.js @@ -13,11 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/** - * Provides utility method for validating that the attributes user has provided are valid - */ - import { sprintf } from '@optimizely/js-sdk-utils'; import fns from '../../utils/fns'; @@ -53,6 +48,9 @@ export var isAttributeValid = function(attributeKey, attributeValue) { ); } +/** + * Provides utility method for validating that the attributes user has provided are valid + */ export default { validate: validate, isAttributeValid: isAttributeValid, diff --git a/packages/optimizely-sdk/lib/utils/enums/index.js b/packages/optimizely-sdk/lib/utils/enums/index.js index 743aed808..a3b209791 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2016-2019, Optimizely, Inc. and contributors * + * Copyright 2016-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,12 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ - -var jsSdkUtils = require('@optimizely/js-sdk-utils'); +import { NOTIFICATION_TYPES } from '@optimizely/js-sdk-utils'; /** * Contains global enums used throughout the library */ -exports.LOG_LEVEL = { +export var LOG_LEVEL = { NOTSET: 0, DEBUG: 1, INFO: 2, @@ -27,7 +26,7 @@ exports.LOG_LEVEL = { ERROR: 4, }; -exports.ERROR_MESSAGES = { +export var ERROR_MESSAGES = { CONDITION_EVALUATOR_ERROR: '%s: Error evaluating audience condition of type %s: %s', DATAFILE_AND_SDK_KEY_MISSING: '%s: You must provide at least one of sdkKey or datafile. Cannot start Optimizely', EXPERIMENT_KEY_NOT_IN_DATAFILE: '%s: Experiment key %s is not in datafile.', @@ -65,7 +64,7 @@ exports.ERROR_MESSAGES = { INVALID_VARIATION_KEY: '%s: Provided variation key is in an invalid format.', }; -exports.LOG_MESSAGES = { +export var LOG_MESSAGES = { ACTIVATE_USER: '%s: Activating user %s in experiment %s.', DISPATCH_CONVERSION_EVENT: '%s: Dispatching conversion event to URL %s with params %s.', DISPATCH_IMPRESSION_EVENT: '%s: Dispatching impression event to URL %s with params %s.', @@ -158,36 +157,36 @@ exports.LOG_MESSAGES = { UNABLE_TO_ATTACH_UNLOAD: '%s: unable to bind optimizely.close() to page unload event: "%s"', }; -exports.RESERVED_EVENT_KEYWORDS = { +export var RESERVED_EVENT_KEYWORDS = { REVENUE: 'revenue', VALUE: 'value', }; -exports.CONTROL_ATTRIBUTES = { +export var CONTROL_ATTRIBUTES = { BOT_FILTERING: '$opt_bot_filtering', BUCKETING_ID: '$opt_bucketing_id', STICKY_BUCKETING_KEY: '$opt_experiment_bucket_map', USER_AGENT: '$opt_user_agent', }; -exports.JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk'; -exports.NODE_CLIENT_ENGINE = 'node-sdk'; -exports.REACT_CLIENT_ENGINE = 'react-sdk'; -exports.REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk'; -exports.REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk'; -exports.NODE_CLIENT_VERSION = '4.0.0-alpha.1'; +export var JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk'; +export var NODE_CLIENT_ENGINE = 'node-sdk'; +export var REACT_CLIENT_ENGINE = 'react-sdk'; +export var REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk'; +export var REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk'; +export var NODE_CLIENT_VERSION = '4.0.0-alpha.1'; -exports.VALID_CLIENT_ENGINES = [ - exports.NODE_CLIENT_ENGINE, - exports.REACT_CLIENT_ENGINE, - exports.JAVASCRIPT_CLIENT_ENGINE, - exports.REACT_NATIVE_CLIENT_ENGINE, - exports.REACT_NATIVE_JS_CLIENT_ENGINE, +export var VALID_CLIENT_ENGINES = [ + NODE_CLIENT_ENGINE, + REACT_CLIENT_ENGINE, + JAVASCRIPT_CLIENT_ENGINE, + REACT_NATIVE_CLIENT_ENGINE, + REACT_NATIVE_JS_CLIENT_ENGINE, ]; -exports.NOTIFICATION_TYPES = jsSdkUtils.NOTIFICATION_TYPES; +export { NOTIFICATION_TYPES }; -exports.DECISION_NOTIFICATION_TYPES = { +export var DECISION_NOTIFICATION_TYPES = { AB_TEST: 'ab-test', FEATURE: 'feature', FEATURE_TEST: 'feature-test', @@ -200,7 +199,7 @@ exports.DECISION_NOTIFICATION_TYPES = { * source is used to decide whether to dispatch an impression event to * Optimizely. */ -exports.DECISION_SOURCES = { +export var DECISION_SOURCES = { FEATURE_TEST: 'feature-test', ROLLOUT: 'rollout', }; @@ -208,7 +207,7 @@ exports.DECISION_SOURCES = { /* * Possible types of variables attached to features */ -exports.FEATURE_VARIABLE_TYPES = { +export var FEATURE_VARIABLE_TYPES = { BOOLEAN: 'boolean', DOUBLE: 'double', INTEGER: 'integer', @@ -218,8 +217,28 @@ exports.FEATURE_VARIABLE_TYPES = { /* * Supported datafile versions */ -exports.DATAFILE_VERSIONS = { +export var DATAFILE_VERSIONS = { V2: '2', V3: '3', V4: '4', }; + +export default { + LOG_LEVEL, + ERROR_MESSAGES, + LOG_MESSAGES, + RESERVED_EVENT_KEYWORDS, + CONTROL_ATTRIBUTES, + JAVASCRIPT_CLIENT_ENGINE, + NODE_CLIENT_ENGINE, + REACT_CLIENT_ENGINE, + REACT_NATIVE_CLIENT_ENGINE, + REACT_NATIVE_JS_CLIENT_ENGINE, + NODE_CLIENT_VERSION, + VALID_CLIENT_ENGINES, + NOTIFICATION_TYPES, + DECISION_NOTIFICATION_TYPES, + DECISION_SOURCES, + FEATURE_VARIABLE_TYPES, + DATAFILE_VERSIONS, +} diff --git a/packages/optimizely-sdk/lib/utils/enums/index.tests.js b/packages/optimizely-sdk/lib/utils/enums/index.tests.js index 5e464247c..f95d71dbf 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2020, Optimizely, Inc. and contributors * + * Copyright 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,10 +13,9 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ +import enums from './'; -var chai = require('chai') -var enums = require('./') -var assert = chai.assert; +import { assert } from 'chai'; describe('lib/utils/enums', function() { describe('valid client engines', function() { From 2688ea4b2daec2ce5dc22a4a050bfcf3db1841f1 Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 18:40:03 +0500 Subject: [PATCH 4/8] converted event_processor_config_validator and event_tag_utils --- packages/optimizely-sdk/lib/index.browser.js | 6 +- .../lib/index.browser.umdtests.js | 2 +- packages/optimizely-sdk/lib/index.node.js | 4 +- .../optimizely-sdk/lib/index.node.tests.js | 2 +- .../optimizely-sdk/lib/index.react_native.js | 4 +- .../optimizely-sdk/lib/optimizely/index.js | 2 +- .../lib/optimizely/index.tests.js | 2 +- .../optimizely-sdk/lib/utils/enums/index.js | 22 +---- .../lib/utils/enums/index.tests.js | 4 +- .../event_processor_config_validator/index.js | 13 ++- .../index.tests.js | 8 +- .../lib/utils/event_tag_utils/index.js | 97 ++++++++++--------- .../lib/utils/event_tag_utils/index.tests.js | 10 +- 13 files changed, 79 insertions(+), 97 deletions(-) diff --git a/packages/optimizely-sdk/lib/index.browser.js b/packages/optimizely-sdk/lib/index.browser.js index c368cef8b..22e0efc4f 100644 --- a/packages/optimizely-sdk/lib/index.browser.js +++ b/packages/optimizely-sdk/lib/index.browser.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { +import { getLogger, setLogHandler, setLogLevel, @@ -27,7 +27,7 @@ import fns from './utils/fns'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; import defaultEventDispatcher from './plugins/event_dispatcher/index.browser'; -import enums from './utils/enums'; +import * as enums from './utils/enums'; import loggerPlugin from './plugins/logger'; import Optimizely from './optimizely'; import eventProcessorConfigValidator from './utils/event_processor_config_validator'; @@ -163,7 +163,7 @@ export { setLogHandler as setLogger, setLogLevel, createInstance, - __internalResetRetryState, + __internalResetRetryState, } export default { diff --git a/packages/optimizely-sdk/lib/index.browser.umdtests.js b/packages/optimizely-sdk/lib/index.browser.umdtests.js index f18a3dbcb..339d85aa2 100644 --- a/packages/optimizely-sdk/lib/index.browser.umdtests.js +++ b/packages/optimizely-sdk/lib/index.browser.umdtests.js @@ -17,7 +17,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import configValidator from './utils/config_validator'; -import enums from './utils/enums'; +import * as enums from './utils/enums'; import logger from './plugins/logger'; import Optimizely from './optimizely'; import testData from './tests/test_data'; diff --git a/packages/optimizely-sdk/lib/index.node.js b/packages/optimizely-sdk/lib/index.node.js index a3a983d2d..8b5868881 100644 --- a/packages/optimizely-sdk/lib/index.node.js +++ b/packages/optimizely-sdk/lib/index.node.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ -import { +import { getLogger, setLogHandler, setLogLevel, @@ -24,7 +24,7 @@ import { import fns from './utils/fns'; import Optimizely from './optimizely'; -import enums from './utils/enums'; +import * as enums from './utils/enums'; import loggerPlugin from './plugins/logger'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/index.node.tests.js b/packages/optimizely-sdk/lib/index.node.tests.js index 220176be0..628d8caaf 100644 --- a/packages/optimizely-sdk/lib/index.node.tests.js +++ b/packages/optimizely-sdk/lib/index.node.tests.js @@ -17,7 +17,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import * as eventProcessor from '@optimizely/js-sdk-event-processor'; -import enums from './utils/enums'; +import * as enums from './utils/enums'; import Optimizely from './optimizely'; import testData from './tests/test_data'; import loggerPlugin from './plugins/logger'; diff --git a/packages/optimizely-sdk/lib/index.react_native.js b/packages/optimizely-sdk/lib/index.react_native.js index f94407977..2517d56e9 100644 --- a/packages/optimizely-sdk/lib/index.react_native.js +++ b/packages/optimizely-sdk/lib/index.react_native.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { +import { getLogger, setLogHandler, setLogLevel, @@ -23,7 +23,7 @@ import { } from '@optimizely/js-sdk-logging'; import fns from './utils/fns'; -import enums from './utils/enums'; +import * as enums from './utils/enums'; import Optimizely from './optimizely'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/optimizely/index.js b/packages/optimizely-sdk/lib/optimizely/index.js index fc99ced13..6f3580151 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.js +++ b/packages/optimizely-sdk/lib/optimizely/index.js @@ -19,7 +19,7 @@ import * as eventProcessor from '@optimizely/js-sdk-event-processor'; import fns from '../utils/fns' import { validate } from '../utils/attributes_validator'; import decisionService from '../core/decision_service'; -import enums from '../utils/enums'; +import * as enums from '../utils/enums'; import eventBuilder from '../core/event_builder/index.js'; import eventHelpers from '../core/event_builder/event_helpers'; import eventTagsValidator from '../utils/event_tags_validator'; diff --git a/packages/optimizely-sdk/lib/optimizely/index.tests.js b/packages/optimizely-sdk/lib/optimizely/index.tests.js index 917820493..a62c4469c 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely/index.tests.js @@ -25,7 +25,7 @@ import AudienceEvaluator from '../core/audience_evaluator'; import bluebird from 'bluebird'; import bucketer from '../core/bucketer'; import projectConfigManager from '../core/project_config/project_config_manager'; -import enums from '../utils/enums'; +import * as enums from '../utils/enums'; import eventBuilder from '../core/event_builder/index.js'; import eventDispatcher from '../plugins/event_dispatcher/index.node'; import errorHandler from '../plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/utils/enums/index.js b/packages/optimizely-sdk/lib/utils/enums/index.js index a3b209791..d216c1346 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.js @@ -184,7 +184,7 @@ export var VALID_CLIENT_ENGINES = [ REACT_NATIVE_JS_CLIENT_ENGINE, ]; -export { NOTIFICATION_TYPES }; +export { NOTIFICATION_TYPES as NOTIFICATION_TYPES }; export var DECISION_NOTIFICATION_TYPES = { AB_TEST: 'ab-test', @@ -222,23 +222,3 @@ export var DATAFILE_VERSIONS = { V3: '3', V4: '4', }; - -export default { - LOG_LEVEL, - ERROR_MESSAGES, - LOG_MESSAGES, - RESERVED_EVENT_KEYWORDS, - CONTROL_ATTRIBUTES, - JAVASCRIPT_CLIENT_ENGINE, - NODE_CLIENT_ENGINE, - REACT_CLIENT_ENGINE, - REACT_NATIVE_CLIENT_ENGINE, - REACT_NATIVE_JS_CLIENT_ENGINE, - NODE_CLIENT_VERSION, - VALID_CLIENT_ENGINES, - NOTIFICATION_TYPES, - DECISION_NOTIFICATION_TYPES, - DECISION_SOURCES, - FEATURE_VARIABLE_TYPES, - DATAFILE_VERSIONS, -} diff --git a/packages/optimizely-sdk/lib/utils/enums/index.tests.js b/packages/optimizely-sdk/lib/utils/enums/index.tests.js index f95d71dbf..1db6200e3 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.tests.js @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ -import enums from './'; +import { VALID_CLIENT_ENGINES } from './'; import { assert } from 'chai'; describe('lib/utils/enums', function() { describe('valid client engines', function() { it('all valid client engines should end with "-sdk"', function() { - enums.VALID_CLIENT_ENGINES.forEach(function(clientEngine) { + VALID_CLIENT_ENGINES.forEach(function(clientEngine) { assert.isTrue(clientEngine.endsWith('-sdk')) }); }); diff --git a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js index c7fb146f7..dc78464bb 100644 --- a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2019, Optimizely + * Copyright 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,15 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -var fns = require('../fns'); +import fns from '../fns'; /** * Return true if the argument is a valid event batch size, false otherwise * @param {*} eventBatchSize * @returns boolean */ -function validateEventBatchSize(eventBatchSize) { +export var validateEventBatchSize = function(eventBatchSize) { return fns.isSafeInteger(eventBatchSize) && eventBatchSize >= 1; } @@ -30,11 +29,11 @@ function validateEventBatchSize(eventBatchSize) { * @param {*} eventFlushInterval * @returns boolean */ -function validateEventFlushInterval(eventFlushInterval) { +export var validateEventFlushInterval = function(eventFlushInterval) { return fns.isSafeInteger(eventFlushInterval) && eventFlushInterval > 0; } -module.exports = { +export default { validateEventBatchSize: validateEventBatchSize, validateEventFlushInterval: validateEventFlushInterval, -}; +} diff --git a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js index a6c9e9ad6..765ba6c1e 100644 --- a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2019, Optimizely + * Copyright 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,11 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import eventProcessorConfigValidator from './index'; -var chai = require('chai'); -var eventProcessorConfigValidator = require('./index'); - -var assert = chai.assert; +import { assert } from 'chai'; describe('utils/event_processor_config_validator', function() { describe('validateEventFlushInterval', function() { diff --git a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js index 9d83f3a2d..f605d9d64 100644 --- a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js +++ b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, 2019 Optimizely + * Copyright 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,57 +13,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { sprintf } from '@optimizely/js-sdk-utils'; + +import { + LOG_LEVEL, + LOG_MESSAGES, + RESERVED_EVENT_KEYWORDS, +} from '../enums'; /** * Provides utility method for parsing event tag values */ -var enums = require('../enums'); -var sprintf = require('@optimizely/js-sdk-utils').sprintf; - -var LOG_LEVEL = enums.LOG_LEVEL; -var LOG_MESSAGES = enums.LOG_MESSAGES; var MODULE_NAME = 'EVENT_TAG_UTILS'; -var REVENUE_EVENT_METRIC_NAME = enums.RESERVED_EVENT_KEYWORDS.REVENUE; -var VALUE_EVENT_METRIC_NAME = enums.RESERVED_EVENT_KEYWORDS.VALUE; +var REVENUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.REVENUE; +var VALUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.VALUE; -module.exports = { - /** - * Grab the revenue value from the event tags. "revenue" is a reserved keyword. - * @param {Object} eventTags - * @param {Object} logger - * @return {Integer|null} - */ - getRevenueValue: function(eventTags, logger) { - if (eventTags && eventTags.hasOwnProperty(REVENUE_EVENT_METRIC_NAME)) { - var rawValue = eventTags[REVENUE_EVENT_METRIC_NAME]; - var parsedRevenueValue = parseInt(rawValue, 10); - if (isNaN(parsedRevenueValue)) { - logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_REVENUE, MODULE_NAME, rawValue)); - return null; - } - logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue)); - return parsedRevenueValue; +/** + * Grab the revenue value from the event tags. "revenue" is a reserved keyword. + * @param {Object} eventTags + * @param {Object} logger + * @return {Integer|null} + */ +export var getRevenueValue = function(eventTags, logger) { + if (eventTags && eventTags.hasOwnProperty(REVENUE_EVENT_METRIC_NAME)) { + var rawValue = eventTags[REVENUE_EVENT_METRIC_NAME]; + var parsedRevenueValue = parseInt(rawValue, 10); + if (isNaN(parsedRevenueValue)) { + logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_REVENUE, MODULE_NAME, rawValue)); + return null; } - return null; - }, + logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue)); + return parsedRevenueValue; + } + return null; +} - /** - * Grab the event value from the event tags. "value" is a reserved keyword. - * @param {Object} eventTags - * @param {Object} logger - * @return {Number|null} - */ - getEventValue: function(eventTags, logger) { - if (eventTags && eventTags.hasOwnProperty(VALUE_EVENT_METRIC_NAME)) { - var rawValue = eventTags[VALUE_EVENT_METRIC_NAME]; - var parsedEventValue = parseFloat(rawValue); - if (isNaN(parsedEventValue)) { - logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_VALUE, MODULE_NAME, rawValue)); - return null; - } - logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue)); - return parsedEventValue; +/** + * Grab the event value from the event tags. "value" is a reserved keyword. + * @param {Object} eventTags + * @param {Object} logger + * @return {Number|null} + */ +export var getEventValue = function(eventTags, logger) { + if (eventTags && eventTags.hasOwnProperty(VALUE_EVENT_METRIC_NAME)) { + var rawValue = eventTags[VALUE_EVENT_METRIC_NAME]; + var parsedEventValue = parseFloat(rawValue); + if (isNaN(parsedEventValue)) { + logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_VALUE, MODULE_NAME, rawValue)); + return null; } - return null; - }, -}; + logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue)); + return parsedEventValue; + } + return null; +} + +export default { + getRevenueValue: getRevenueValue, + getEventValue: getEventValue, +} diff --git a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js index 910689a01..23a3c1ae9 100644 --- a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Optimizely + * Copyright 2017, 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,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var chai = require('chai'); -var assert = chai.assert; -var sinon = require('sinon'); -var eventTagUtils = require('./'); +import eventTagUtils from './'; + +import sinon from 'sinon'; +import { assert } from 'chai'; describe('lib/utils/event_tag_utils', function() { var mockLogger; From 07a4ac3adf3ecf308d98acd6d53806f98d0f7280 Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 18:44:14 +0500 Subject: [PATCH 5/8] update export in enum --- packages/optimizely-sdk/lib/utils/enums/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/optimizely-sdk/lib/utils/enums/index.js b/packages/optimizely-sdk/lib/utils/enums/index.js index d216c1346..4c3bf5e1c 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.js @@ -184,7 +184,7 @@ export var VALID_CLIENT_ENGINES = [ REACT_NATIVE_JS_CLIENT_ENGINE, ]; -export { NOTIFICATION_TYPES as NOTIFICATION_TYPES }; +export { NOTIFICATION_TYPES }; export var DECISION_NOTIFICATION_TYPES = { AB_TEST: 'ab-test', From 3710bc045697cce6459357be7ac08c377d57854b Mon Sep 17 00:00:00 2001 From: fayyazarshad Date: Fri, 10 Apr 2020 19:17:49 +0500 Subject: [PATCH 6/8] converted event_tags_validator --- packages/optimizely-sdk/lib/index.browser.js | 2 +- .../lib/index.browser.umdtests.js | 2 +- packages/optimizely-sdk/lib/index.node.js | 2 +- .../optimizely-sdk/lib/index.node.tests.js | 2 +- .../optimizely-sdk/lib/index.react_native.js | 2 +- .../optimizely-sdk/lib/optimizely/index.js | 2 +- .../lib/optimizely/index.tests.js | 2 +- .../optimizely-sdk/lib/utils/enums/index.js | 49 ++++++++++--------- .../lib/utils/enums/index.tests.js | 9 ++-- .../lib/utils/event_tags_validator/index.js | 39 ++++++++------- .../utils/event_tags_validator/index.tests.js | 20 ++++---- 11 files changed, 67 insertions(+), 64 deletions(-) diff --git a/packages/optimizely-sdk/lib/index.browser.js b/packages/optimizely-sdk/lib/index.browser.js index 22e0efc4f..b4943aba7 100644 --- a/packages/optimizely-sdk/lib/index.browser.js +++ b/packages/optimizely-sdk/lib/index.browser.js @@ -27,7 +27,7 @@ import fns from './utils/fns'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; import defaultEventDispatcher from './plugins/event_dispatcher/index.browser'; -import * as enums from './utils/enums'; +import enums from './utils/enums'; import loggerPlugin from './plugins/logger'; import Optimizely from './optimizely'; import eventProcessorConfigValidator from './utils/event_processor_config_validator'; diff --git a/packages/optimizely-sdk/lib/index.browser.umdtests.js b/packages/optimizely-sdk/lib/index.browser.umdtests.js index 339d85aa2..f18a3dbcb 100644 --- a/packages/optimizely-sdk/lib/index.browser.umdtests.js +++ b/packages/optimizely-sdk/lib/index.browser.umdtests.js @@ -17,7 +17,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import configValidator from './utils/config_validator'; -import * as enums from './utils/enums'; +import enums from './utils/enums'; import logger from './plugins/logger'; import Optimizely from './optimizely'; import testData from './tests/test_data'; diff --git a/packages/optimizely-sdk/lib/index.node.js b/packages/optimizely-sdk/lib/index.node.js index 8b5868881..2e5ce3c50 100644 --- a/packages/optimizely-sdk/lib/index.node.js +++ b/packages/optimizely-sdk/lib/index.node.js @@ -24,7 +24,7 @@ import { import fns from './utils/fns'; import Optimizely from './optimizely'; -import * as enums from './utils/enums'; +import enums from './utils/enums'; import loggerPlugin from './plugins/logger'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/index.node.tests.js b/packages/optimizely-sdk/lib/index.node.tests.js index 628d8caaf..220176be0 100644 --- a/packages/optimizely-sdk/lib/index.node.tests.js +++ b/packages/optimizely-sdk/lib/index.node.tests.js @@ -17,7 +17,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import * as eventProcessor from '@optimizely/js-sdk-event-processor'; -import * as enums from './utils/enums'; +import enums from './utils/enums'; import Optimizely from './optimizely'; import testData from './tests/test_data'; import loggerPlugin from './plugins/logger'; diff --git a/packages/optimizely-sdk/lib/index.react_native.js b/packages/optimizely-sdk/lib/index.react_native.js index 2517d56e9..c7b8478be 100644 --- a/packages/optimizely-sdk/lib/index.react_native.js +++ b/packages/optimizely-sdk/lib/index.react_native.js @@ -23,7 +23,7 @@ import { } from '@optimizely/js-sdk-logging'; import fns from './utils/fns'; -import * as enums from './utils/enums'; +import enums from './utils/enums'; import Optimizely from './optimizely'; import configValidator from './utils/config_validator'; import defaultErrorHandler from './plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/optimizely/index.js b/packages/optimizely-sdk/lib/optimizely/index.js index 6f3580151..fc99ced13 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.js +++ b/packages/optimizely-sdk/lib/optimizely/index.js @@ -19,7 +19,7 @@ import * as eventProcessor from '@optimizely/js-sdk-event-processor'; import fns from '../utils/fns' import { validate } from '../utils/attributes_validator'; import decisionService from '../core/decision_service'; -import * as enums from '../utils/enums'; +import enums from '../utils/enums'; import eventBuilder from '../core/event_builder/index.js'; import eventHelpers from '../core/event_builder/event_helpers'; import eventTagsValidator from '../utils/event_tags_validator'; diff --git a/packages/optimizely-sdk/lib/optimizely/index.tests.js b/packages/optimizely-sdk/lib/optimizely/index.tests.js index a62c4469c..917820493 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely/index.tests.js @@ -25,7 +25,7 @@ import AudienceEvaluator from '../core/audience_evaluator'; import bluebird from 'bluebird'; import bucketer from '../core/bucketer'; import projectConfigManager from '../core/project_config/project_config_manager'; -import * as enums from '../utils/enums'; +import enums from '../utils/enums'; import eventBuilder from '../core/event_builder/index.js'; import eventDispatcher from '../plugins/event_dispatcher/index.node'; import errorHandler from '../plugins/error_handler'; diff --git a/packages/optimizely-sdk/lib/utils/enums/index.js b/packages/optimizely-sdk/lib/utils/enums/index.js index 4c3bf5e1c..743aed808 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2016-2020, Optimizely, Inc. and contributors * + * Copyright 2016-2019, 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,12 +13,13 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ -import { NOTIFICATION_TYPES } from '@optimizely/js-sdk-utils'; + +var jsSdkUtils = require('@optimizely/js-sdk-utils'); /** * Contains global enums used throughout the library */ -export var LOG_LEVEL = { +exports.LOG_LEVEL = { NOTSET: 0, DEBUG: 1, INFO: 2, @@ -26,7 +27,7 @@ export var LOG_LEVEL = { ERROR: 4, }; -export var ERROR_MESSAGES = { +exports.ERROR_MESSAGES = { CONDITION_EVALUATOR_ERROR: '%s: Error evaluating audience condition of type %s: %s', DATAFILE_AND_SDK_KEY_MISSING: '%s: You must provide at least one of sdkKey or datafile. Cannot start Optimizely', EXPERIMENT_KEY_NOT_IN_DATAFILE: '%s: Experiment key %s is not in datafile.', @@ -64,7 +65,7 @@ export var ERROR_MESSAGES = { INVALID_VARIATION_KEY: '%s: Provided variation key is in an invalid format.', }; -export var LOG_MESSAGES = { +exports.LOG_MESSAGES = { ACTIVATE_USER: '%s: Activating user %s in experiment %s.', DISPATCH_CONVERSION_EVENT: '%s: Dispatching conversion event to URL %s with params %s.', DISPATCH_IMPRESSION_EVENT: '%s: Dispatching impression event to URL %s with params %s.', @@ -157,36 +158,36 @@ export var LOG_MESSAGES = { UNABLE_TO_ATTACH_UNLOAD: '%s: unable to bind optimizely.close() to page unload event: "%s"', }; -export var RESERVED_EVENT_KEYWORDS = { +exports.RESERVED_EVENT_KEYWORDS = { REVENUE: 'revenue', VALUE: 'value', }; -export var CONTROL_ATTRIBUTES = { +exports.CONTROL_ATTRIBUTES = { BOT_FILTERING: '$opt_bot_filtering', BUCKETING_ID: '$opt_bucketing_id', STICKY_BUCKETING_KEY: '$opt_experiment_bucket_map', USER_AGENT: '$opt_user_agent', }; -export var JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk'; -export var NODE_CLIENT_ENGINE = 'node-sdk'; -export var REACT_CLIENT_ENGINE = 'react-sdk'; -export var REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk'; -export var REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk'; -export var NODE_CLIENT_VERSION = '4.0.0-alpha.1'; +exports.JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk'; +exports.NODE_CLIENT_ENGINE = 'node-sdk'; +exports.REACT_CLIENT_ENGINE = 'react-sdk'; +exports.REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk'; +exports.REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk'; +exports.NODE_CLIENT_VERSION = '4.0.0-alpha.1'; -export var VALID_CLIENT_ENGINES = [ - NODE_CLIENT_ENGINE, - REACT_CLIENT_ENGINE, - JAVASCRIPT_CLIENT_ENGINE, - REACT_NATIVE_CLIENT_ENGINE, - REACT_NATIVE_JS_CLIENT_ENGINE, +exports.VALID_CLIENT_ENGINES = [ + exports.NODE_CLIENT_ENGINE, + exports.REACT_CLIENT_ENGINE, + exports.JAVASCRIPT_CLIENT_ENGINE, + exports.REACT_NATIVE_CLIENT_ENGINE, + exports.REACT_NATIVE_JS_CLIENT_ENGINE, ]; -export { NOTIFICATION_TYPES }; +exports.NOTIFICATION_TYPES = jsSdkUtils.NOTIFICATION_TYPES; -export var DECISION_NOTIFICATION_TYPES = { +exports.DECISION_NOTIFICATION_TYPES = { AB_TEST: 'ab-test', FEATURE: 'feature', FEATURE_TEST: 'feature-test', @@ -199,7 +200,7 @@ export var DECISION_NOTIFICATION_TYPES = { * source is used to decide whether to dispatch an impression event to * Optimizely. */ -export var DECISION_SOURCES = { +exports.DECISION_SOURCES = { FEATURE_TEST: 'feature-test', ROLLOUT: 'rollout', }; @@ -207,7 +208,7 @@ export var DECISION_SOURCES = { /* * Possible types of variables attached to features */ -export var FEATURE_VARIABLE_TYPES = { +exports.FEATURE_VARIABLE_TYPES = { BOOLEAN: 'boolean', DOUBLE: 'double', INTEGER: 'integer', @@ -217,7 +218,7 @@ export var FEATURE_VARIABLE_TYPES = { /* * Supported datafile versions */ -export var DATAFILE_VERSIONS = { +exports.DATAFILE_VERSIONS = { V2: '2', V3: '3', V4: '4', diff --git a/packages/optimizely-sdk/lib/utils/enums/index.tests.js b/packages/optimizely-sdk/lib/utils/enums/index.tests.js index 1db6200e3..5e464247c 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/enums/index.tests.js @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2020 Optimizely, Inc. and contributors * + * Copyright 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,14 +13,15 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ -import { VALID_CLIENT_ENGINES } from './'; -import { assert } from 'chai'; +var chai = require('chai') +var enums = require('./') +var assert = chai.assert; describe('lib/utils/enums', function() { describe('valid client engines', function() { it('all valid client engines should end with "-sdk"', function() { - VALID_CLIENT_ENGINES.forEach(function(clientEngine) { + enums.VALID_CLIENT_ENGINES.forEach(function(clientEngine) { assert.isTrue(clientEngine.endsWith('-sdk')) }); }); diff --git a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js index 42268a716..b12c67589 100644 --- a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Optimizely + * Copyright 2017, 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,24 +17,25 @@ /** * Provides utility method for validating that event tags user has provided are valid */ +import { sprintf } from '@optimizely/js-sdk-utils'; -var sprintf = require('@optimizely/js-sdk-utils').sprintf; - -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; +import { ERROR_MESSAGES } from '../enums'; var MODULE_NAME = 'EVENT_TAGS_VALIDATOR'; -module.exports = { - /** - * Validates user's provided event tags - * @param {Object} event tags - * @return {boolean} True if event tags are valid - * @throws If event tags are not valid - */ - validate: function(eventTags) { - if (typeof eventTags === 'object' && !Array.isArray(eventTags) && eventTags !== null) { - return true; - } else { - throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, MODULE_NAME)); - } - }, -}; +/** + * Validates user's provided event tags + * @param {Object} event tags + * @return {boolean} True if event tags are valid + * @throws If event tags are not valid + */ +export var validate = function(eventTags) { + if (typeof eventTags === 'object' && !Array.isArray(eventTags) && eventTags !== null) { + return true; + } else { + throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, MODULE_NAME)); + } +} + +export default { + validate, +} diff --git a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js index ce8a0be49..8113de8f8 100644 --- a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Optimizely + * Copyright 2017, 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,30 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var chai = require('chai'); -var assert = chai.assert; -var sprintf = require('@optimizely/js-sdk-utils').sprintf; -var eventTagsValidator = require('./'); +import { sprintf } from '@optimizely/js-sdk-utils'; -var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; +import { validate } from './'; +import { ERROR_MESSAGES } from'../enums'; + +import { assert } from 'chai'; describe('lib/utils/event_tags_validator', function() { describe('APIs', function() { describe('validate', function() { it('should validate the given event tags if event tags is an object', function() { - assert.isTrue(eventTagsValidator.validate({ testAttribute: 'testValue' })); + assert.isTrue(validate({ testAttribute: 'testValue' })); }); it('should throw an error if event tags is an array', function() { var eventTagsArray = ['notGonnaWork']; assert.throws(function() { - eventTagsValidator.validate(eventTagsArray); + validate(eventTagsArray); }, sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, 'EVENT_TAGS_VALIDATOR')); }); it('should throw an error if event tags is null', function() { assert.throws(function() { - eventTagsValidator.validate(null); + validate(null); }, sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, 'EVENT_TAGS_VALIDATOR')); }); @@ -45,7 +45,7 @@ describe('lib/utils/event_tags_validator', function() { console.log('This is an invalid input!'); } assert.throws(function() { - eventTagsValidator.validate(invalidInput); + validate(invalidInput); }, sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, 'EVENT_TAGS_VALIDATOR')); }); }); From 3c8ceb9a27b07e6402023f248bf92a623e218102 Mon Sep 17 00:00:00 2001 From: Zeeshan Ashraf Date: Fri, 10 Apr 2020 12:02:26 -0700 Subject: [PATCH 7/8] Removed whitespace changes --- packages/optimizely-sdk/lib/index.browser.js | 4 ++-- packages/optimizely-sdk/lib/index.node.js | 2 +- packages/optimizely-sdk/lib/index.react_native.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/optimizely-sdk/lib/index.browser.js b/packages/optimizely-sdk/lib/index.browser.js index b4943aba7..c368cef8b 100644 --- a/packages/optimizely-sdk/lib/index.browser.js +++ b/packages/optimizely-sdk/lib/index.browser.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { +import { getLogger, setLogHandler, setLogLevel, @@ -163,7 +163,7 @@ export { setLogHandler as setLogger, setLogLevel, createInstance, - __internalResetRetryState, + __internalResetRetryState, } export default { diff --git a/packages/optimizely-sdk/lib/index.node.js b/packages/optimizely-sdk/lib/index.node.js index 2e5ce3c50..a3a983d2d 100644 --- a/packages/optimizely-sdk/lib/index.node.js +++ b/packages/optimizely-sdk/lib/index.node.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * * limitations under the License. * ***************************************************************************/ -import { +import { getLogger, setLogHandler, setLogLevel, diff --git a/packages/optimizely-sdk/lib/index.react_native.js b/packages/optimizely-sdk/lib/index.react_native.js index c7b8478be..f94407977 100644 --- a/packages/optimizely-sdk/lib/index.react_native.js +++ b/packages/optimizely-sdk/lib/index.react_native.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { +import { getLogger, setLogHandler, setLogLevel, From e03e368b2b6250520c4948870ee889ba4cf5b6b9 Mon Sep 17 00:00:00 2001 From: Zeeshan Ashraf Date: Fri, 10 Apr 2020 12:21:49 -0700 Subject: [PATCH 8/8] minor cleanup --- .../lib/utils/attributes_validator/index.js | 6 +++--- .../lib/utils/attributes_validator/index.tests.js | 3 +-- .../lib/utils/config_validator/index.js | 14 ++++++++------ .../lib/utils/config_validator/index.tests.js | 5 ++--- .../event_processor_config_validator/index.js | 14 +++++++------- .../index.tests.js | 6 +++--- .../lib/utils/event_tag_utils/index.js | 6 +++--- .../lib/utils/event_tag_utils/index.tests.js | 6 +++--- .../lib/utils/event_tags_validator/index.js | 3 ++- .../lib/utils/event_tags_validator/index.tests.js | 3 +-- 10 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js b/packages/optimizely-sdk/lib/utils/attributes_validator/index.js index 2dc5836ea..2b15c27a1 100644 --- a/packages/optimizely-sdk/lib/utils/attributes_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/attributes_validator/index.js @@ -37,7 +37,7 @@ export var validate = function(attributes) { } else { throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME)); } -} +}; export var isAttributeValid = function(attributeKey, attributeValue) { return ( @@ -46,7 +46,7 @@ export var isAttributeValid = function(attributeKey, attributeValue) { typeof attributeValue === 'boolean' || (fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue))) ); -} +}; /** * Provides utility method for validating that the attributes user has provided are valid @@ -54,4 +54,4 @@ export var isAttributeValid = function(attributeKey, attributeValue) { export default { validate: validate, isAttributeValid: isAttributeValid, -} +}; diff --git a/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js index f430ddb23..96ebbd7f6 100644 --- a/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/attributes_validator/index.tests.js @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { assert } from 'chai'; import { sprintf } from '@optimizely/js-sdk-utils'; import attributesValidator from './'; import { ERROR_MESSAGES } from '../enums'; -import { assert } from 'chai'; - describe('lib/utils/attributes_validator', function() { describe('APIs', function() { describe('validate', function() { diff --git a/packages/optimizely-sdk/lib/utils/config_validator/index.js b/packages/optimizely-sdk/lib/utils/config_validator/index.js index 8edf45aeb..dfdb51dea 100644 --- a/packages/optimizely-sdk/lib/utils/config_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/config_validator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018, 2019-2020 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. @@ -15,8 +15,10 @@ */ import { sprintf } from '@optimizely/js-sdk-utils'; -import { ERROR_MESSAGES } from '../enums'; -import { DATAFILE_VERSIONS } from '../enums'; +import { + ERROR_MESSAGES, + DATAFILE_VERSIONS, +} from '../enums'; var MODULE_NAME = 'CONFIG_VALIDATOR'; var SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4]; @@ -44,7 +46,7 @@ export var validate = function(config) { } return true; -} +}; /** * Validates the datafile @@ -74,7 +76,7 @@ export var validateDatafile = function(datafile) { } return true; -} +}; /** * Provides utility methods for validating that the configuration options are valid @@ -82,4 +84,4 @@ export var validateDatafile = function(datafile) { export default { validate: validate, validateDatafile: validateDatafile, -} +}; diff --git a/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js index 4523ad8b3..65c4c24d9 100644 --- a/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/config_validator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 2018, 2019-2020 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,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { assert } from 'chai'; import { sprintf } from '@optimizely/js-sdk-utils'; import configValidator from './'; import { ERROR_MESSAGES } from '../enums'; import testData from '../../tests/test_data'; -import { assert } from 'chai'; - describe('lib/utils/config_validator', function() { describe('APIs', function() { describe('validate', function() { diff --git a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js index dc78464bb..41521cc48 100644 --- a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Optimizely + * Copyright 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,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import fns from '../fns'; +import { isSafeInteger } from '../fns'; /** * Return true if the argument is a valid event batch size, false otherwise @@ -21,8 +21,8 @@ import fns from '../fns'; * @returns boolean */ export var validateEventBatchSize = function(eventBatchSize) { - return fns.isSafeInteger(eventBatchSize) && eventBatchSize >= 1; -} + return isSafeInteger(eventBatchSize) && eventBatchSize >= 1; +}; /** * Return true if the argument is a valid event flush interval, false otherwise @@ -30,10 +30,10 @@ export var validateEventBatchSize = function(eventBatchSize) { * @returns boolean */ export var validateEventFlushInterval = function(eventFlushInterval) { - return fns.isSafeInteger(eventFlushInterval) && eventFlushInterval > 0; -} + return isSafeInteger(eventFlushInterval) && eventFlushInterval > 0; +}; export default { validateEventBatchSize: validateEventBatchSize, validateEventFlushInterval: validateEventFlushInterval, -} +}; diff --git a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js index 765ba6c1e..6ecc6a134 100644 --- a/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_processor_config_validator/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Optimizely + * Copyright 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,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import eventProcessorConfigValidator from './index'; - import { assert } from 'chai'; +import eventProcessorConfigValidator from './index'; + describe('utils/event_processor_config_validator', function() { describe('validateEventFlushInterval', function() { it('returns false for null & undefined', function() { diff --git a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js index f605d9d64..18f8f38cc 100644 --- a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js +++ b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.js @@ -46,7 +46,7 @@ export var getRevenueValue = function(eventTags, logger) { return parsedRevenueValue; } return null; -} +}; /** * Grab the event value from the event tags. "value" is a reserved keyword. @@ -66,9 +66,9 @@ export var getEventValue = function(eventTags, logger) { return parsedEventValue; } return null; -} +}; export default { getRevenueValue: getRevenueValue, getEventValue: getEventValue, -} +}; diff --git a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js index 23a3c1ae9..7f15f1296 100644 --- a/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, 2020 Optimizely + * Copyright 2017, 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,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import eventTagUtils from './'; - import sinon from 'sinon'; import { assert } from 'chai'; +import eventTagUtils from './'; + describe('lib/utils/event_tag_utils', function() { var mockLogger; beforeEach(function() { diff --git a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js index b12c67589..2d6303d8a 100644 --- a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js +++ b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.js @@ -20,6 +20,7 @@ import { sprintf } from '@optimizely/js-sdk-utils'; import { ERROR_MESSAGES } from '../enums'; + var MODULE_NAME = 'EVENT_TAGS_VALIDATOR'; /** @@ -37,5 +38,5 @@ export var validate = function(eventTags) { } export default { - validate, + validate: validate, } diff --git a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js index 8113de8f8..4dde65d18 100644 --- a/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js +++ b/packages/optimizely-sdk/lib/utils/event_tags_validator/index.tests.js @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { assert } from 'chai'; import { sprintf } from '@optimizely/js-sdk-utils'; import { validate } from './'; import { ERROR_MESSAGES } from'../enums'; -import { assert } from 'chai'; - describe('lib/utils/event_tags_validator', function() { describe('APIs', function() { describe('validate', function() {