Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 81d2cac

Browse files
refactor: Convert lib/utils to ES module (Part 2/2) (optimizely#452)
Summary: This PR only converts contents inside lib/utils to ESModule. lib/utils/ fns enums, json_schema_validator string_value_validator user_profile_service_validator Test plan: All unit test and Full stack compatibility suite tests are getting passed. Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com> Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com>
1 parent 7b0a0ad commit 81d2cac

File tree

10 files changed

+194
-159
lines changed

10 files changed

+194
-159
lines changed

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

Lines changed: 44 additions & 24 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. *
@@ -14,20 +14,20 @@
1414
* limitations under the License. *
1515
***************************************************************************/
1616

17-
var jsSdkUtils = require('@optimizely/js-sdk-utils');
17+
import { NOTIFICATION_TYPES as notificationTypesEnum } from '@optimizely/js-sdk-utils';
1818

1919
/**
2020
* Contains global enums used throughout the library
2121
*/
22-
exports.LOG_LEVEL = {
22+
export var LOG_LEVEL = {
2323
NOTSET: 0,
2424
DEBUG: 1,
2525
INFO: 2,
2626
WARNING: 3,
2727
ERROR: 4,
2828
};
2929

30-
exports.ERROR_MESSAGES = {
30+
export var ERROR_MESSAGES = {
3131
CONDITION_EVALUATOR_ERROR: '%s: Error evaluating audience condition of type %s: %s',
3232
DATAFILE_AND_SDK_KEY_MISSING: '%s: You must provide at least one of sdkKey or datafile. Cannot start Optimizely',
3333
EXPERIMENT_KEY_NOT_IN_DATAFILE: '%s: Experiment key %s is not in datafile.',
@@ -65,7 +65,7 @@ exports.ERROR_MESSAGES = {
6565
INVALID_VARIATION_KEY: '%s: Provided variation key is in an invalid format.',
6666
};
6767

68-
exports.LOG_MESSAGES = {
68+
export var LOG_MESSAGES = {
6969
ACTIVATE_USER: '%s: Activating user %s in experiment %s.',
7070
DISPATCH_CONVERSION_EVENT: '%s: Dispatching conversion event to URL %s with params %s.',
7171
DISPATCH_IMPRESSION_EVENT: '%s: Dispatching impression event to URL %s with params %s.',
@@ -158,36 +158,36 @@ exports.LOG_MESSAGES = {
158158
UNABLE_TO_ATTACH_UNLOAD: '%s: unable to bind optimizely.close() to page unload event: "%s"',
159159
};
160160

161-
exports.RESERVED_EVENT_KEYWORDS = {
161+
export var RESERVED_EVENT_KEYWORDS = {
162162
REVENUE: 'revenue',
163163
VALUE: 'value',
164164
};
165165

166-
exports.CONTROL_ATTRIBUTES = {
166+
export var CONTROL_ATTRIBUTES = {
167167
BOT_FILTERING: '$opt_bot_filtering',
168168
BUCKETING_ID: '$opt_bucketing_id',
169169
STICKY_BUCKETING_KEY: '$opt_experiment_bucket_map',
170170
USER_AGENT: '$opt_user_agent',
171171
};
172172

173-
exports.JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk';
174-
exports.NODE_CLIENT_ENGINE = 'node-sdk';
175-
exports.REACT_CLIENT_ENGINE = 'react-sdk';
176-
exports.REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk';
177-
exports.REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk';
178-
exports.NODE_CLIENT_VERSION = '4.0.0-alpha.1';
173+
export var JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk';
174+
export var NODE_CLIENT_ENGINE = 'node-sdk';
175+
export var REACT_CLIENT_ENGINE = 'react-sdk';
176+
export var REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk';
177+
export var REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk';
178+
export var NODE_CLIENT_VERSION = '4.0.0-alpha.1';
179179

180-
exports.VALID_CLIENT_ENGINES = [
181-
exports.NODE_CLIENT_ENGINE,
182-
exports.REACT_CLIENT_ENGINE,
183-
exports.JAVASCRIPT_CLIENT_ENGINE,
184-
exports.REACT_NATIVE_CLIENT_ENGINE,
185-
exports.REACT_NATIVE_JS_CLIENT_ENGINE,
180+
export var VALID_CLIENT_ENGINES = [
181+
NODE_CLIENT_ENGINE,
182+
REACT_CLIENT_ENGINE,
183+
JAVASCRIPT_CLIENT_ENGINE,
184+
REACT_NATIVE_CLIENT_ENGINE,
185+
REACT_NATIVE_JS_CLIENT_ENGINE,
186186
];
187187

188-
exports.NOTIFICATION_TYPES = jsSdkUtils.NOTIFICATION_TYPES;
188+
export var NOTIFICATION_TYPES = notificationTypesEnum;
189189

190-
exports.DECISION_NOTIFICATION_TYPES = {
190+
export var DECISION_NOTIFICATION_TYPES = {
191191
AB_TEST: 'ab-test',
192192
FEATURE: 'feature',
193193
FEATURE_TEST: 'feature-test',
@@ -200,15 +200,15 @@ exports.DECISION_NOTIFICATION_TYPES = {
200200
* source is used to decide whether to dispatch an impression event to
201201
* Optimizely.
202202
*/
203-
exports.DECISION_SOURCES = {
203+
export var DECISION_SOURCES = {
204204
FEATURE_TEST: 'feature-test',
205205
ROLLOUT: 'rollout',
206206
};
207207

208208
/*
209209
* Possible types of variables attached to features
210210
*/
211-
exports.FEATURE_VARIABLE_TYPES = {
211+
export var FEATURE_VARIABLE_TYPES = {
212212
BOOLEAN: 'boolean',
213213
DOUBLE: 'double',
214214
INTEGER: 'integer',
@@ -218,8 +218,28 @@ exports.FEATURE_VARIABLE_TYPES = {
218218
/*
219219
* Supported datafile versions
220220
*/
221-
exports.DATAFILE_VERSIONS = {
221+
export var DATAFILE_VERSIONS = {
222222
V2: '2',
223223
V3: '3',
224224
V4: '4',
225225
};
226+
227+
export default {
228+
LOG_LEVEL: LOG_LEVEL,
229+
ERROR_MESSAGES: ERROR_MESSAGES,
230+
LOG_MESSAGES: LOG_MESSAGES,
231+
RESERVED_EVENT_KEYWORDS: RESERVED_EVENT_KEYWORDS,
232+
CONTROL_ATTRIBUTES: CONTROL_ATTRIBUTES,
233+
JAVASCRIPT_CLIENT_ENGINE: JAVASCRIPT_CLIENT_ENGINE,
234+
NODE_CLIENT_ENGINE: NODE_CLIENT_ENGINE,
235+
REACT_CLIENT_ENGINE: REACT_CLIENT_ENGINE,
236+
REACT_NATIVE_CLIENT_ENGINE: REACT_NATIVE_CLIENT_ENGINE,
237+
REACT_NATIVE_JS_CLIENT_ENGINE: REACT_NATIVE_JS_CLIENT_ENGINE,
238+
NODE_CLIENT_VERSION: NODE_CLIENT_VERSION,
239+
VALID_CLIENT_ENGINES: VALID_CLIENT_ENGINES,
240+
NOTIFICATION_TYPES: NOTIFICATION_TYPES,
241+
DECISION_NOTIFICATION_TYPES: DECISION_NOTIFICATION_TYPES,
242+
DECISION_SOURCES: DECISION_SOURCES,
243+
FEATURE_VARIABLE_TYPES: FEATURE_VARIABLE_TYPES,
244+
DATAFILE_VERSIONS: DATAFILE_VERSIONS,
245+
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
* See the License for the specific language governing permissions and *
1414
* limitations under the License. *
1515
***************************************************************************/
16-
17-
var chai = require('chai')
18-
var enums = require('./')
19-
var assert = chai.assert;
16+
import { assert } from 'chai';
17+
import { VALID_CLIENT_ENGINES } from './';
2018

2119
describe('lib/utils/enums', function() {
2220
describe('valid client engines', function() {
2321
it('all valid client engines should end with "-sdk"', function() {
24-
enums.VALID_CLIENT_ENGINES.forEach(function(clientEngine) {
22+
VALID_CLIENT_ENGINES.forEach(function(clientEngine) {
2523
assert.isTrue(clientEngine.endsWith('-sdk'))
2624
});
2725
});

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

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,62 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var uuid = require('uuid');
16+
import uuidLib from 'uuid';
17+
import { keyBy as keyByUtil } from '@optimizely/js-sdk-utils';
18+
1719
var MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53);
18-
var keyBy = require('@optimizely/js-sdk-utils').keyBy;
19-
module.exports = {
20-
assign: function(target) {
21-
if (!target) {
22-
return {};
23-
}
24-
if (typeof Object.assign === 'function') {
25-
return Object.assign.apply(Object, arguments);
26-
} else {
27-
var to = Object(target);
28-
for (var index = 1; index < arguments.length; index++) {
29-
var nextSource = arguments[index];
30-
if (nextSource !== null && nextSource !== undefined) {
31-
for (var nextKey in nextSource) {
32-
// Avoid bugs when hasOwnProperty is shadowed
33-
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
34-
to[nextKey] = nextSource[nextKey];
35-
}
20+
21+
export var assign = function(target) {
22+
if (!target) {
23+
return {};
24+
}
25+
if (typeof Object.assign === 'function') {
26+
return Object.assign.apply(Object, arguments);
27+
} else {
28+
var to = Object(target);
29+
for (var index = 1; index < arguments.length; index++) {
30+
var nextSource = arguments[index];
31+
if (nextSource !== null && nextSource !== undefined) {
32+
for (var nextKey in nextSource) {
33+
// Avoid bugs when hasOwnProperty is shadowed
34+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
35+
to[nextKey] = nextSource[nextKey];
3636
}
3737
}
3838
}
39-
return to;
4039
}
41-
},
42-
currentTimestamp: function() {
43-
return Math.round(new Date().getTime());
44-
},
45-
isSafeInteger: function(number) {
46-
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
47-
},
48-
keyBy: function(arr, key) {
49-
if (!arr) return {};
50-
return keyBy(arr, function(item) {
51-
return item[key];
52-
});
53-
},
54-
uuid: function() {
55-
return uuid.v4();
56-
},
57-
isNumber: function(value) {
58-
return typeof value === 'number';
59-
},
40+
return to;
41+
}
42+
};
43+
44+
export var currentTimestamp = function() {
45+
return Math.round(new Date().getTime());
46+
};
47+
48+
export var isSafeInteger = function(number) {
49+
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
50+
};
51+
52+
export var keyBy = function(arr, key) {
53+
if (!arr) return {};
54+
return keyByUtil(arr, function(item) {
55+
return item[key];
56+
});
57+
};
58+
59+
export var uuid = function() {
60+
return uuidLib.v4();
61+
};
62+
63+
export var isNumber = function(value) {
64+
return typeof value === 'number';
65+
};
66+
67+
export default {
68+
assign: assign,
69+
currentTimestamp: currentTimestamp,
70+
isSafeInteger: isSafeInteger,
71+
keyBy: keyBy,
72+
uuid: uuid,
73+
isNumber: isNumber,
6074
};

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020 Optimizely
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,10 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { assert } from 'chai';
1617

17-
var chai = require('chai');
18-
var assert = chai.assert;
19-
var fns = require('./');
18+
import fns from './';
2019

2120
describe('lib/utils/fns', function() {
2221
describe('APIs', function() {
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2020, Optimizely
2+
* Copyright 2016-2017, 2020 Optimizely
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,35 +13,37 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var validate = require('json-schema').validate;
17-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
18-
var projectConfigSchema = require('../../core/project_config/project_config_schema').default;
16+
import { sprintf } from '@optimizely/js-sdk-utils';
17+
import { validate as jsonSchemaValidator } from 'json-schema';
18+
19+
import { ERROR_MESSAGES } from '../enums';
20+
import projectConfigSchema from '../../core/project_config/project_config_schema';
1921

20-
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
2122
var MODULE_NAME = 'JSON_SCHEMA_VALIDATOR';
2223

23-
module.exports = {
24-
/**
25-
* Validate the given json object against the specified schema
26-
* @param {Object} jsonSchema The json schema to validate against
27-
* @param {Object} jsonObject The object to validate against the schema
28-
* @return {Boolean} True if the given object is valid
29-
*/
30-
validate: function(jsonObject) {
31-
if (!jsonObject) {
32-
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
33-
}
24+
/**
25+
* Validate the given json object against the specified schema
26+
* @param {Object} jsonObject The object to validate against the schema
27+
* @return {Boolean} True if the given object is valid
28+
*/
29+
export var validate = function(jsonObject) {
30+
if (!jsonObject) {
31+
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
32+
}
3433

35-
var result = validate(jsonObject, projectConfigSchema);
36-
if (result.valid) {
37-
return true;
38-
} else {
39-
if (Array.isArray(result.errors)) {
40-
throw new Error(
41-
sprintf(ERROR_MESSAGES.INVALID_DATAFILE, MODULE_NAME, result.errors[0].property, result.errors[0].message)
42-
);
43-
}
44-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME));
34+
var result = jsonSchemaValidator(jsonObject, projectConfigSchema);
35+
if (result.valid) {
36+
return true;
37+
} else {
38+
if (Array.isArray(result.errors)) {
39+
throw new Error(
40+
sprintf(ERROR_MESSAGES.INVALID_DATAFILE, MODULE_NAME, result.errors[0].property, result.errors[0].message)
41+
);
4542
}
46-
},
43+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME));
44+
}
45+
};
46+
47+
export default {
48+
validate: validate,
4749
};

0 commit comments

Comments
 (0)