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

Commit e423ec3

Browse files
refactor: Convert lib/utils to ES module (Part 1/2) (optimizely#451)
Summary: This converts these folders inside lib/utils to ESModule. lib/utils/ attributes_validator config_validator event_tags_validator event_processor_config_validator event_tag_utils Test plan: All unit test and Full stack compatibility suite tests pass Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com>
1 parent e76a20d commit e423ec3

File tree

11 files changed

+204
-197
lines changed

11 files changed

+204
-197
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { sprintf, objectValues } from '@optimizely/js-sdk-utils';
1717
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
1818

1919
import fns from '../utils/fns'
20-
import attributesValidator from '../utils/attributes_validator';
20+
import { validate } from '../utils/attributes_validator';
2121
import decisionService from '../core/decision_service';
2222
import enums from '../utils/enums';
2323
import eventBuilder from '../core/event_builder/index.js';
@@ -499,7 +499,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
499499
}
500500
}
501501
if (userAttributes) {
502-
attributesValidator.validate(userAttributes);
502+
validate(userAttributes);
503503
}
504504
if (eventTags) {
505505
eventTagsValidator.validate(eventTags);
Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 2018-2019, Optimizely
2+
* Copyright 2016, 2018-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,43 +13,45 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { sprintf } from '@optimizely/js-sdk-utils';
1617

17-
/**
18-
* Provides utility method for validating that the attributes user has provided are valid
19-
*/
18+
import fns from '../../utils/fns';
19+
import { ERROR_MESSAGES } from '../enums';
2020

21-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
22-
var fns = require('../../utils/fns');
23-
24-
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
2521
var MODULE_NAME = 'ATTRIBUTES_VALIDATOR';
2622

27-
module.exports = {
28-
/**
29-
* Validates user's provided attributes
30-
* @param {Object} attributes
31-
* @return {boolean} True if the attributes are valid
32-
* @throws If the attributes are not valid
33-
*/
34-
validate: function(attributes) {
35-
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
36-
Object.keys(attributes).forEach(function(key) {
37-
if (typeof attributes[key] === 'undefined') {
38-
throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key));
39-
}
40-
});
41-
return true;
42-
} else {
43-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME));
44-
}
45-
},
23+
/**
24+
* Validates user's provided attributes
25+
* @param {Object} attributes
26+
* @return {boolean} True if the attributes are valid
27+
* @throws If the attributes are not valid
28+
*/
29+
export var validate = function(attributes) {
30+
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
31+
Object.keys(attributes).forEach(function(key) {
32+
if (typeof attributes[key] === 'undefined') {
33+
throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key));
34+
}
35+
});
36+
return true;
37+
} else {
38+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME));
39+
}
40+
};
41+
42+
export var isAttributeValid = function(attributeKey, attributeValue) {
43+
return (
44+
typeof attributeKey === 'string' &&
45+
(typeof attributeValue === 'string' ||
46+
typeof attributeValue === 'boolean' ||
47+
(fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)))
48+
);
49+
};
4650

47-
isAttributeValid: function(attributeKey, attributeValue) {
48-
return (
49-
typeof attributeKey === 'string' &&
50-
(typeof attributeValue === 'string' ||
51-
typeof attributeValue === 'boolean' ||
52-
(fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)))
53-
);
54-
},
51+
/**
52+
* Provides utility method for validating that the attributes user has provided are valid
53+
*/
54+
export default {
55+
validate: validate,
56+
isAttributeValid: isAttributeValid,
5557
};

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 2018-2019, Optimizely
2+
* Copyright 2016, 2018-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,13 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var chai = require('chai');
17-
var assert = chai.assert;
18-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
19-
var attributesValidator = require('./');
20-
var fns = require('./../fns/');
16+
import { assert } from 'chai';
17+
import { sprintf } from '@optimizely/js-sdk-utils';
2118

22-
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
19+
import attributesValidator from './';
20+
import { ERROR_MESSAGES } from '../enums';
2321

2422
describe('lib/utils/attributes_validator', function() {
2523
describe('APIs', function() {
Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 2018, 2019 Optimizely
2+
* Copyright 2016, 2018-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,70 +13,75 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
16+
import { sprintf } from '@optimizely/js-sdk-utils';
1717

18-
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
19-
var MODULE_NAME = 'CONFIG_VALIDATOR';
20-
var DATAFILE_VERSIONS = require('../enums').DATAFILE_VERSIONS;
18+
import {
19+
ERROR_MESSAGES,
20+
DATAFILE_VERSIONS,
21+
} from '../enums';
2122

23+
var MODULE_NAME = 'CONFIG_VALIDATOR';
2224
var SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4];
2325

2426
/**
25-
* Provides utility methods for validating that the configuration options are valid
27+
* Validates the given config options
28+
* @param {Object} config
29+
* @param {Object} config.errorHandler
30+
* @param {Object} config.eventDispatcher
31+
* @param {Object} config.logger
32+
* @return {Boolean} True if the config options are valid
33+
* @throws If any of the config options are not valid
2634
*/
27-
module.exports = {
28-
/**
29-
* Validates the given config options
30-
* @param {Object} config
31-
* @param {Object} config.errorHandler
32-
* @param {Object} config.eventDispatcher
33-
* @param {Object} config.logger
34-
* @return {Boolean} True if the config options are valid
35-
* @throws If any of the config options are not valid
36-
*/
37-
validate: function(config) {
38-
if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') {
39-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
40-
}
35+
export var validate = function(config) {
36+
if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') {
37+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
38+
}
4139

42-
if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') {
43-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
44-
}
40+
if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') {
41+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
42+
}
4543

46-
if (config.logger && typeof config.logger.log !== 'function') {
47-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
48-
}
44+
if (config.logger && typeof config.logger.log !== 'function') {
45+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
46+
}
4947

50-
return true;
51-
},
48+
return true;
49+
};
5250

53-
/**
54-
* Validates the datafile
55-
* @param {string} datafile
56-
* @return {Boolean} True if the datafile is valid
57-
* @throws If the datafile is not valid for any of the following reasons:
58-
- The datafile string is undefined
59-
- The datafile string cannot be parsed as a JSON object
60-
- The datafile version is not supported
61-
*/
62-
validateDatafile: function(datafile) {
63-
if (!datafile) {
64-
throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
65-
}
51+
/**
52+
* Validates the datafile
53+
* @param {string} datafile
54+
* @return {Boolean} True if the datafile is valid
55+
* @throws If the datafile is not valid for any of the following reasons:
56+
- The datafile string is undefined
57+
- The datafile string cannot be parsed as a JSON object
58+
- The datafile version is not supported
59+
*/
60+
export var validateDatafile = function(datafile) {
61+
if (!datafile) {
62+
throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
63+
}
6664

67-
if (typeof datafile === 'string' || datafile instanceof String) {
68-
// Attempt to parse the datafile string
69-
try {
70-
datafile = JSON.parse(datafile);
71-
} catch (ex) {
72-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
73-
}
65+
if (typeof datafile === 'string' || datafile instanceof String) {
66+
// Attempt to parse the datafile string
67+
try {
68+
datafile = JSON.parse(datafile);
69+
} catch (ex) {
70+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
7471
}
72+
}
7573

76-
if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
77-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
78-
}
74+
if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
75+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
76+
}
77+
78+
return true;
79+
};
7980

80-
return true;
81-
},
81+
/**
82+
* Provides utility methods for validating that the configuration options are valid
83+
*/
84+
export default {
85+
validate: validate,
86+
validateDatafile: validateDatafile,
8287
};

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 2018, 2019 Optimizely
2+
* Copyright 2016, 2018-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,13 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var chai = require('chai');
17-
var assert = chai.assert;
18-
var configValidator = require('./');
19-
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
20-
var testData = require('../../tests/test_data');
16+
import { assert } from 'chai';
17+
import { sprintf } from '@optimizely/js-sdk-utils';
2118

22-
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
19+
import configValidator from './';
20+
import { ERROR_MESSAGES } from '../enums';
21+
import testData from '../../tests/test_data';
2322

2423
describe('lib/utils/config_validator', function() {
2524
describe('APIs', function() {
Lines changed: 9 additions & 10 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,28 +13,27 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
var fns = require('../fns');
16+
import { isSafeInteger } from '../fns';
1817

1918
/**
2019
* Return true if the argument is a valid event batch size, false otherwise
2120
* @param {*} eventBatchSize
2221
* @returns boolean
2322
*/
24-
function validateEventBatchSize(eventBatchSize) {
25-
return fns.isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
26-
}
23+
export var validateEventBatchSize = function(eventBatchSize) {
24+
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
25+
};
2726

2827
/**
2928
* Return true if the argument is a valid event flush interval, false otherwise
3029
* @param {*} eventFlushInterval
3130
* @returns boolean
3231
*/
33-
function validateEventFlushInterval(eventFlushInterval) {
34-
return fns.isSafeInteger(eventFlushInterval) && eventFlushInterval > 0;
35-
}
32+
export var validateEventFlushInterval = function(eventFlushInterval) {
33+
return isSafeInteger(eventFlushInterval) && eventFlushInterval > 0;
34+
};
3635

37-
module.exports = {
36+
export default {
3837
validateEventBatchSize: validateEventBatchSize,
3938
validateEventFlushInterval: validateEventFlushInterval,
4039
};

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

Lines changed: 3 additions & 5 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,11 +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 eventProcessorConfigValidator = require('./index');
19-
20-
var assert = chai.assert;
18+
import eventProcessorConfigValidator from './index';
2119

2220
describe('utils/event_processor_config_validator', function() {
2321
describe('validateEventFlushInterval', function() {

0 commit comments

Comments
 (0)