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

Commit a1c18ff

Browse files
authored
refactor: Removed dependency on lodash.assign (optimizely#395)
Summary: to reduce package size, we are trying to gradually get rid of lodash library. This PR replaces assign function with Object.assign. Also added a polyfill method to support old browsers. Polyfill was taken from MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill Test Plan: All tests pass after this change
1 parent 037f466 commit a1c18ff

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var MODULE_NAME = 'AUDIENCE_EVALUATOR';
3535
* @constructor
3636
*/
3737
function AudienceEvaluator(UNSTABLE_conditionEvaluators) {
38-
this.typeToEvaluatorMap = fns.assignIn({}, UNSTABLE_conditionEvaluators, {
38+
this.typeToEvaluatorMap = fns.assign({}, UNSTABLE_conditionEvaluators, {
3939
'custom_attribute': customAttributeConditionEvaluator
4040
});
4141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ DecisionService.prototype.__resolveExperimentBucketMap = function(userId, attrib
116116
attributes = attributes || {}
117117
var userProfile = this.__getUserProfile(userId) || {};
118118
var attributeExperimentBucketMap = attributes[enums.CONTROL_ATTRIBUTES.STICKY_BUCKETING_KEY];
119-
return fns.assignIn({}, userProfile.experiment_bucket_map, attributeExperimentBucketMap);
119+
return fns.assign({}, userProfile.experiment_bucket_map, attributeExperimentBucketMap);
120120
};
121121

122122

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
fns.forEach(projectConfig.groupIdMap, function(group, Id) {
5656
experiments = fns.cloneDeep(group.experiments);
5757
fns.forEach(experiments, function(experiment) {
58-
projectConfig.experiments.push(fns.assignIn(experiment, {groupId: Id}));
58+
projectConfig.experiments.push(fns.assign(experiment, {groupId: Id}));
5959
});
6060
});
6161

@@ -78,7 +78,7 @@ module.exports = {
7878
experiment.variationKeyMap = fns.keyBy(experiment.variations, 'key');
7979

8080
// Creates { <variationId>: { key: <variationKey>, id: <variationId> } } mapping for quick lookup
81-
fns.assignIn(projectConfig.variationIdMap, fns.keyBy(experiment.variations, 'id'));
81+
fns.assign(projectConfig.variationIdMap, fns.keyBy(experiment.variations, 'id'));
8282

8383
fns.forOwn(experiment.variationKeyMap, function(variation) {
8484
if (variation.variables) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module.exports = {
107107
eventDispatcher = config.eventDispatcher;
108108
}
109109

110-
config = fns.assignIn(
110+
config = fns.assign(
111111
{
112112
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
113113
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = {
8686
config.skipJSONValidation = true;
8787
}
8888

89-
config = fns.assignIn(
89+
config = fns.assign(
9090
{
9191
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
9292
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,28 @@ var keyBy = require('@optimizely/js-sdk-utils').keyBy;
1919
var MAX_NUMBER_LIMIT = Math.pow(2, 53);
2020

2121
module.exports = {
22-
assign: require('lodash/assign'),
23-
assignIn: require('lodash/assignIn'),
22+
assign: function (target) {
23+
if (!target) {
24+
return {};
25+
}
26+
if (typeof Object.assign === 'function') {
27+
return Object.assign.apply(Object, arguments);
28+
} else {
29+
var to = Object(target);
30+
for (var index = 1; index < arguments.length; index++) {
31+
var nextSource = arguments[index];
32+
if (nextSource !== null && nextSource !== undefined) {
33+
for (var nextKey in nextSource) {
34+
// Avoid bugs when hasOwnProperty is shadowed
35+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
36+
to[nextKey] = nextSource[nextKey];
37+
}
38+
}
39+
}
40+
}
41+
return to;
42+
}
43+
},
2444
cloneDeep: require('lodash/cloneDeep'),
2545
currentTimestamp: function() {
2646
return Math.round(new Date().getTime());

0 commit comments

Comments
 (0)