Skip to content

refactor: Convert lib/utils to ES module (Part 2/2) #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 14, 2020
Merged
68 changes: 44 additions & 24 deletions packages/optimizely-sdk/lib/utils/enums/index.js
Original file line number Diff line number Diff line change
@@ -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. *
Expand All @@ -14,20 +14,20 @@
* limitations under the License. *
***************************************************************************/

var jsSdkUtils = require('@optimizely/js-sdk-utils');
import { NOTIFICATION_TYPES as notificationTypesEnum } 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,
WARNING: 3,
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.',
Expand Down Expand Up @@ -65,7 +65,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.',
Expand Down Expand Up @@ -158,36 +158,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 var NOTIFICATION_TYPES = notificationTypesEnum;

exports.DECISION_NOTIFICATION_TYPES = {
export var DECISION_NOTIFICATION_TYPES = {
AB_TEST: 'ab-test',
FEATURE: 'feature',
FEATURE_TEST: 'feature-test',
Expand All @@ -200,15 +200,15 @@ 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',
};

/*
* Possible types of variables attached to features
*/
exports.FEATURE_VARIABLE_TYPES = {
export var FEATURE_VARIABLE_TYPES = {
BOOLEAN: 'boolean',
DOUBLE: 'double',
INTEGER: 'integer',
Expand All @@ -218,8 +218,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: LOG_LEVEL,
ERROR_MESSAGES: ERROR_MESSAGES,
LOG_MESSAGES: LOG_MESSAGES,
RESERVED_EVENT_KEYWORDS: RESERVED_EVENT_KEYWORDS,
CONTROL_ATTRIBUTES: CONTROL_ATTRIBUTES,
JAVASCRIPT_CLIENT_ENGINE: JAVASCRIPT_CLIENT_ENGINE,
NODE_CLIENT_ENGINE: NODE_CLIENT_ENGINE,
REACT_CLIENT_ENGINE: REACT_CLIENT_ENGINE,
REACT_NATIVE_CLIENT_ENGINE: REACT_NATIVE_CLIENT_ENGINE,
REACT_NATIVE_JS_CLIENT_ENGINE: REACT_NATIVE_JS_CLIENT_ENGINE,
NODE_CLIENT_VERSION: NODE_CLIENT_VERSION,
VALID_CLIENT_ENGINES: VALID_CLIENT_ENGINES,
NOTIFICATION_TYPES: NOTIFICATION_TYPES,
DECISION_NOTIFICATION_TYPES: DECISION_NOTIFICATION_TYPES,
DECISION_SOURCES: DECISION_SOURCES,
FEATURE_VARIABLE_TYPES: FEATURE_VARIABLE_TYPES,
DATAFILE_VERSIONS: DATAFILE_VERSIONS,
}
8 changes: 3 additions & 5 deletions packages/optimizely-sdk/lib/utils/enums/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

var chai = require('chai')
var enums = require('./')
var assert = chai.assert;
import { assert } from 'chai';
import { VALID_CLIENT_ENGINES } from './';

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'))
});
});
Expand Down
92 changes: 53 additions & 39 deletions packages/optimizely-sdk/lib/utils/fns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,62 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var uuid = require('uuid');
import uuidLib from 'uuid';
import { keyBy as keyByUtil } from '@optimizely/js-sdk-utils';

var MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53);
var keyBy = require('@optimizely/js-sdk-utils').keyBy;
module.exports = {
assign: function(target) {
if (!target) {
return {};
}
if (typeof Object.assign === 'function') {
return Object.assign.apply(Object, arguments);
} else {
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}

export var assign = function(target) {
if (!target) {
return {};
}
if (typeof Object.assign === 'function') {
return Object.assign.apply(Object, arguments);
} else {
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
},
currentTimestamp: function() {
return Math.round(new Date().getTime());
},
isSafeInteger: function(number) {
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
},
keyBy: function(arr, key) {
if (!arr) return {};
return keyBy(arr, function(item) {
return item[key];
});
},
uuid: function() {
return uuid.v4();
},
isNumber: function(value) {
return typeof value === 'number';
},
return to;
}
};

export var currentTimestamp = function() {
return Math.round(new Date().getTime());
};

export var isSafeInteger = function(number) {
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
};

export var keyBy = function(arr, key) {
if (!arr) return {};
return keyByUtil(arr, function(item) {
return item[key];
});
};

export var uuid = function() {
return uuidLib.v4();
};

export var isNumber = function(value) {
return typeof value === 'number';
};

export default {
assign: assign,
currentTimestamp: currentTimestamp,
isSafeInteger: isSafeInteger,
keyBy: keyBy,
uuid: uuid,
isNumber: isNumber,
};
7 changes: 3 additions & 4 deletions packages/optimizely-sdk/lib/utils/fns/index.tests.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from 'chai';

var chai = require('chai');
var assert = chai.assert;
var fns = require('./');
import fns from './';

describe('lib/utils/fns', function() {
describe('APIs', function() {
Expand Down
56 changes: 29 additions & 27 deletions packages/optimizely-sdk/lib/utils/json_schema_validator/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017, 2020, Optimizely
* Copyright 2016-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.
Expand All @@ -13,35 +13,37 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var validate = require('json-schema').validate;
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var projectConfigSchema = require('../../core/project_config/project_config_schema').default;
import { sprintf } from '@optimizely/js-sdk-utils';
import { validate as jsonSchemaValidator } from 'json-schema';

import { ERROR_MESSAGES } from '../enums';
import projectConfigSchema from '../../core/project_config/project_config_schema';

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
var MODULE_NAME = 'JSON_SCHEMA_VALIDATOR';

module.exports = {
/**
* Validate the given json object against the specified schema
* @param {Object} jsonSchema The json schema to validate against
* @param {Object} jsonObject The object to validate against the schema
* @return {Boolean} True if the given object is valid
*/
validate: function(jsonObject) {
if (!jsonObject) {
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
}
/**
* Validate the given json object against the specified schema
* @param {Object} jsonObject The object to validate against the schema
* @return {Boolean} True if the given object is valid
*/
export var validate = function(jsonObject) {
if (!jsonObject) {
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
}

var result = validate(jsonObject, projectConfigSchema);
if (result.valid) {
return true;
} else {
if (Array.isArray(result.errors)) {
throw new Error(
sprintf(ERROR_MESSAGES.INVALID_DATAFILE, MODULE_NAME, result.errors[0].property, result.errors[0].message)
);
}
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME));
var result = jsonSchemaValidator(jsonObject, projectConfigSchema);
if (result.valid) {
return true;
} else {
if (Array.isArray(result.errors)) {
throw new Error(
sprintf(ERROR_MESSAGES.INVALID_DATAFILE, MODULE_NAME, result.errors[0].property, result.errors[0].message)
);
}
},
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME));
}
};

export default {
validate: validate,
};
Loading