8000 refactor: Removed dependency on lodash.isFinite (#408) · github/optimizely-javascript-sdk@fb7b674 · GitHub
[go: up one dir, main page]

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

Commit fb7b674

Browse files
authored
refactor: Removed dependency on lodash.isFinite (optimizely#408)
Summary: To reduce package size, we are trying to gradually get rid of lodash library. This PR replaces isFinite function with a simple implementation. Test plan: Update Unit Tests
1 parent e07181e commit fb7b674

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function exactEvaluator(condition, userAttributes, logger) {
9999
var userValue = userAttributes[conditionName];
100100
var userValueType = typeof userValue;
101101

102-
if (!isValueTypeValidForExactConditions(conditionValue) || (fns.isNumber(conditionValue) && !fns.isFinite(conditionValue))) {
102+
if (!isValueTypeValidForExactConditions(conditionValue) || (fns.isNumber(conditionValue) && !fns.isSafeInteger(conditionValue))) {
103103
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.UNEXPECTED_CONDITION_VALUE, MODULE_NAME, JSON.stringify(condition)));
104104
return null;
105105
}
@@ -114,7 +114,7 @@ function exactEvaluator(condition, userAttributes, logger) {
114114
return null;
115115
}
116116

117-
if (fns.isNumber(userValue) && !fns.isFinite(userValue)) {
117+
if (fns.isNumber(userValue) && !fns.isSafeInteger(userValue)) {
118118
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.OUT_OF_BOUNDS, MODULE_NAME, JSON.stringify(condition), conditionName));
119119
return null;
120120
}
@@ -152,7 +152,7 @@ function greaterThanEvaluator(condition, userAttributes, logger) {
152152
var userValueType = typeof userValue;
153153
var conditionValue = condition.value;
154154

155-
if (!fns.isFinite(conditionValue)) {
155+
if (!fns.isSafeInteger(conditionValue)) {
156156
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.UNEXPECTED_CONDITION_VALUE, MODULE_NAME, JSON.stringify(condition)));
157157
return null;
158158
}
@@ -167,7 +167,7 @@ function greaterThanEvaluator(condition, userAttributes, logger) {
167167
return null;
168168
}
169169

170-
if (!fns.isFinite(userValue)) {
170+
if (!fns.isSafeInteger(userValue)) {
171171
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.OUT_OF_BOUNDS, MODULE_NAME, JSON.stringify(condition), conditionName));
172172
return null;
173173
}
@@ -191,7 +191,7 @@ function lessThanEvaluator(condition, userAttributes, logger) {
191191
var userValueType = typeof userValue;
192192
var conditionValue = condition.value;
193193

194-
if (!fns.isFinite(conditionValue)) {
194+
if (!fns.isSafeInteger(conditionValue)) {
195195
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.UNEXPECTED_CONDITION_VALUE, MODULE_NAME, JSON.stringify(condition)));
196196
return null;
197197
}
@@ -206,7 +206,7 @@ function lessThanEvaluator(condition, userAttributes, logger) {
206206
return null;
207207
}
208208

209-
if (!fns.isFinite(userValue)) {
209+
if (!fns.isSafeInteger(userValue)) {
210210
logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.OUT_OF_BOUNDS, MODULE_NAME, JSON.stringify(condition), conditionName));
211211
return null;
212212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ Optimizely.prototype.onReady = function(options) {
10261026
if (typeof options === 'object' && options !== null) {
10271027
timeout = options.timeout;
10281028
}
1029-
if (!fns.isFinite(timeout)) {
1029+
if (!fns.isSafeInteger(timeout)) {
10301030
timeout = DEFAULT_ONREADY_TIMEOUT;
10311031
}
10321032

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ module.exports = {
4747

4848
isAttributeValid: function(attributeKey, attributeValue) {
4949
return (typeof attributeKey === 'string') &&
50-
(typeof attributeValue === 'string' || typeof attributeValue === 'boolean' || (fns.isNumber(attributeValue) && fns.isFinite(attributeValue)));
50+
(typeof attributeValue === 'string' || typeof a F438 ttributeValue === 'boolean' || (fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)));
5151
},
5252
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var fns = require('../fns');
2222
* @returns boolean
2323
*/
2424
function validateEventBatchSize(eventBatchSize) {
25-
return fns.isFinite(eventBatchSize) && eventBatchSize >= 1;
25+
return fns.isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
2626
}
2727

2828
/**
@@ -31,7 +31,7 @@ function validateEventBatchSize(eventBatchSize) {
3131
* @returns boolean
3232
*/
3333
function validateEventFlushInterval(eventFlushInterval) {
34-
return fns.isFinite(eventFlushInterval) && eventFlushInterval > 0;
34+
return fns.isSafeInteger(eventFlushInterval) && eventFlushInterval > 0;
3535
}
3636

3737
module.exports = {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
* limitations under the License.
1515
*/
1616
var uuid = require('uuid');
17-
var _isFinite = require('lodash/isFinite');
17+
var MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53);
1818
var keyBy = require('@optimizely/js-sdk-utils').keyBy;
19-
var MAX_NUMBER_LIMIT = Math.pow(2, 53);
20-
2119
module.exports = {
2220
assign: function (target) {
2321
if (!target) {
@@ -29,7 +27,7 @@ module.exports = {
2927
var to = Object(target);
3028
for (var index = 1; index < arguments.length; index++) {
3129
var nextSource = arguments[index];
32-
if (nextSource !== null && nextSource !== undefined) {
30+
if (nextSource !== null && nextSource !== undefined) {
3331
for (var nextKey in nextSource) {
3432
// Avoid bugs when hasOwnProperty is shadowed
3533
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
@@ -45,8 +43,8 @@ module.exports = {
4543
currentTimestamp: function() {
4644
return Math.round(new Date().getTime());
4745
},
48-
isFinite: function(number) {
49-
return _isFinite(number) && Math.abs(number) <= MAX_NUMBER_LIMIT;
46+
isSafeInteger: function(number) {
47+
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
5048
},
5149
keyBy: function(arr, key) {
5250
if (!arr) return {};

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ describe('lib/utils/fns', function() {
2222
describe('APIs', function() {
2323
describe('isFinite', function() {
2424
it('should return false for invalid numbers', function() {
25-
assert.isFalse(fns.isFinite(Infinity));
26-
assert.isFalse(fns.isFinite(-Infinity));
27-
assert.isFalse(fns.isFinite(NaN));
28-
assert.isFalse(fns.isFinite(Math.pow(2, 53) + 2));
29-
assert.isFalse(fns.isFinite(-Math.pow(2, 53) - 2));
25+
assert.isFalse(fns.isSafeInteger(Infinity));
26+
assert.isFalse(fns.isSafeInteger(-Infinity));
27+
assert.isFalse(fns.isSafeInteger(NaN));
28+
assert.isFalse(fns.isSafeInteger(undefined));
29+
assert.isFalse(fns.isSafeInteger('3'));
30+
assert.isFalse(fns.isSafeInteger(Math.pow(2, 53) + 2));
31+
assert.isFalse(fns.isSafeInteger(-Math.pow(2, 53) - 2));
3032
});
3133

3234
it('should return true for valid numbers', function() {
33-
assert.isTrue(fns.isFinite(0));
34-
assert.isTrue(fns.isFinite(10));
35-
assert.isTrue(fns.isFinite(10.5));
36-
assert.isTrue(fns.isFinite(Math.pow(2, 53)));
37-
assert.isTrue(fns.isFinite(-Math.pow(2, 53)));
35+
assert.isTrue(fns.isSafeInteger(0));
36+
assert.isTrue(fns.isSafeInteger(10));
37+
assert.isTrue(fns.isSafeInteger(10.5));
38+
assert.isTrue(fns.isSafeInteger(Math.pow(2, 53)));
39+
assert.isTrue(fns.isSafeInteger(-Math.pow(2, 53)));
3840
});
3941
});
4042

@@ -56,7 +58,7 @@ describe('lib/utils/fns', function() {
5658
row4: { key1: 'row4', key2: 'key2row4' }
5759
});
5860
});
59-
61+
6062
it('should return empty object when first argument is null or undefined', function() {
6163
var obj = fns.keyBy(null, 'key1');
6264
assert.isEmpty(obj);
@@ -65,7 +67,7 @@ describe('lib/utils/fns', function() {
6567
assert.isEmpty(obj);
6668
});
6769
});
68-
70+
6971
describe('isNumber', function() {
7072
it('should return true in case of number', function() {
7173
assert.isTrue(fns.isNumber(3));

0 commit comments

Comments
 (0)
0