8000 refactor: Removed dependency on lodash.forEach (#412) · github/optimizely-javascript-sdk@59cdad4 · 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 59cdad4

Browse files
refactor: Removed dependency on lodash.forEach (optimizely#412)
Summary: to reduce package size, we are trying to gradually get rid of lodash library. This PR removes the implementation of fns.forEach and replaced it with default forEach method of arrays and Object.Keys for maps Test plan: All tests are passing Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com>
1 parent a1c18ff commit 59cdad4

File tree

3 files changed

+11
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ NotificationCenter.prototype.addNotificationListener = function (notificationTyp
6666
}
6767

6868
var callbackAlreadyAdded = false;
69-
fns.forEach(this.__notificationListeners[notificationType], function (listenerEntry) {
69+
(this.__notificationListeners[notificationType] || []).forEach(function(listenerEntry) {
7070
if (listenerEntry.callback === callback) {
7171
callbackAlreadyAdded = true;
7272
return false;
@@ -102,12 +102,13 @@ NotificationCenter.prototype.removeNotificationListener = function (listenerId)
102102
var indexToRemove;
103103
var typeToRemove;
104104
fns.forOwn(this.__notificationListeners, function (listenersForType, notificationType) {
105-
fns.forEach(listenersForType, function (listenerEntry, i) {
105+
(listenersForType || []).every(function(listenerEntry, i) {
106106
if (listenerEntry.id === listenerId) {
107107
indexToRemove = i;
108108
typeToRemove = notificationType;
109109
return false;
110110
}
111+
return true
111112
});
112113
if (indexToRemove !== undefined && typeToRemove !== undefined) {
113114
return false;
@@ -160,7 +161,7 @@ NotificationCenter.prototype.clearNotificationListeners = function (notification
160161
*/
161162
NotificationCenter.prototype.sendNotifications = function (notificationType, notificationData) {
162163
try {
163-
fns.forEach(this.__notificationListeners[notificationType], function (listenerEntry) {
164+
(this.__notificationListeners[notificationType] || []).forEach(function(listenerEntry) {
164165
var callback = listenerEntry.callback;
165166
try {
166167
callback(notificationData);
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
* Conditions of audiences in projectConfig.typedAudiences are not
4242
* expected to be string-encoded as they are here in projectConfig.audiences.
4343
*/
44-
fns.forEach(projectConfig.audiences, function(audience) {
44+
(projectConfig.audiences || []).forEach(function(audience) {
4545
audience.conditions = JSON.parse(audience.conditions);
4646
});
4747
projectConfig.audiencesById = fns.keyBy(projectConfig.audiences, 'id');
@@ -52,16 +52,16 @@ module.exports = {
5252
projectConfig.groupIdMap = fns.keyBy(projectConfig.groups, 'id');
5353

5454
var experiments;
55-
fns.forEach(projectConfig.groupIdMap, function(group, Id) {
56-
experiments = fns.cloneDeep(group.experiments);
57-
fns.forEach(experiments, function(experiment) {
55+
Object.keys(projectConfig.groupIdMap || {}).forEach(function(Id) {
56+
experiments = fns.cloneDeep(projectConfig.groupIdMap[Id].experiments);
57+
(experiments || []).forEach(function(experiment) {
5858
projectConfig.experiments.push(fns.assign(experiment, {groupId: Id}));
5959
});
6060
});
6161

6262
projectConfig.rolloutIdMap = fns.keyBy(projectConfig.rollouts || [], 'id');
6363
fns.forOwn(projectConfig.rolloutIdMap, function(rollout) {
64-
fns.forEach(rollout.experiments || [], function(experiment) {
64+
(rollout.experiments || []).forEach(function(experiment) {
6565
projectConfig.experiments.push(fns.cloneDeep(experiment));
6666
// Creates { <variationKey>: <variation> } map inside of the experiment
6767
experiment.variationKeyMap = fns.keyBy(experiment.variations, 'key');
@@ -73,7 +73,7 @@ module.exports = {
7373

7474
projectConfig.variationIdMap = {};
7575
projectConfig.variationVariableUsageMap = {};
76-
fns.forEach(projectConfig.experiments, function(experiment) {
76+
(projectConfig.experiments || []).forEach(function(experiment) {
7777
// Creates { <variationKey>: <variation> } map inside of the experiment
7878
experiment.variationKeyMap = fns.keyBy(experiment.variations, 'key');
7979

@@ -94,7 +94,7 @@ module.exports = {
9494
projectConfig.featureKeyMap = fns.keyBy(projectConfig.featureFlags || [], 'key');
9595
fns.forOwn(projectConfig.featureKeyMap, function(feature) {
9696
feature.variableKeyMap = fns.keyBy(feature.variables, 'key');
97-
fns.forEach(feature.experimentIds || [], function(experimentId) {
97+
(feature.experimentIds || []).forEach(function(experimentId) {
9898
// Add this experiment in experiment-feature map.
9999
if (projectConfig.experimentFeatureMap[experimentId]) {
100100
projectConfig.experimentFeatureMap[experimentId].push(feature.id);
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ module.exports = {
5454
return item[key];
5555
});
5656
},
57-
forEach: require('lodash/forEach'),
5857
forOwn: require('lodash/forOwn'),
5958
uuid: function() {
6059
return uuid.v4();