-
Notifications
You must be signed in to change notification settings - Fork 83
refactor: Removed dependency on lodash.forOwn #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
forOwn: function(obj, callback) { | ||
for(var key in obj) { | ||
if(obj.hasOwnProperty(key)){ | ||
var val = obj[key]; | ||
callback(val, key, obj); | ||
} | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good 👍 but could you double check all the places we're using this?
lodash forOwn
is more complex, for example:
- If you pass
null
orundefined
as the first argument it won't crash - If the callback returns
false
it breaks out of the loop early
There could be other differences as well. Let's just check all the places we're using it and make sure we are not relying on any of this kind of special functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
early return feature is used in some places in the code. I added that functionality and added unit tests.
2. Added unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but discussed offline with @zashraf1985. Suggestion is to explore whether we can replace usages with other util FN calls and remove forOwn
.
@@ -37,7 +37,9 @@ function NotificationCenter(options) { | |||
this.logger = options.logger; | |||
this.errorHandler = options.errorHandler; | |||
this.__notificationListeners = {}; | |||
fns.forOwn(enums.NOTIFICATION_TYPES, function(notificationTypeEnum) { | |||
|
|||
Object.keys(enums.NOTIFICATION_TYPES).forEach(function(key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(enums.NOTIFICATION_TYPES).forEach(function(key) { | |
jsSdkUtils.objectValues(enums.NOTIFICATION_TYPES).forEach(function(notificationTypeEnum) { |
if (indexToRemove !== undefined && typeToRemove !== undefined) { | ||
return false; | ||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Array.prototype.forEach
, we no longer exit early from the loop when false
is returned. I suggest Array.prototype.some
to maintain the existing behavior.
@@ -130,7 +131,8 @@ NotificationCenter.prototype.removeNotificationListener = function (listenerId) | |||
*/ | |||
NotificationCenter.prototype.clearAllNotificationListeners = function () { | |||
try{ | |||
fns.forOwn(enums.NOTIFICATION_TYPES, function (notificationTypeEnum) { | |||
Object.keys(enums.NOTIFICATION_TYPES).forEach(function (key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(enums.NOTIFICATION_TYPES).forEach(function (key) { | |
jsSdkUtils.objectValues(enums.NOTIFICATION_TYPES).forEach(function (notificationTypeEnum) { |
@@ -60,7 +60,8 @@ module.exports = { | |||
}); | |||
|
|||
projectConfig.rolloutIdMap = fns.keyBy(projectConfig.rollouts || [], 'id'); | |||
fns.forOwn(projectConfig.rolloutIdMap, function(rollout) { | |||
Object.keys(projectConfig.rolloutIdMap || {}).forEach(function (rolloutId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(projectConfig.rolloutIdMap || {}).forEach(function (rolloutId) { | |
jsSdkUtils.objectValues(projectConfig.rolloutIdMap || {}).forEach(function (rollout) { |
@@ -79,8 +80,8 @@ module.exports = { | |||
|
|||
// Creates { <variationId>: { key: <variationKey>, id: <variationId> } } mapping for quick lookup | |||
fns.assignIn(projectConfig.variationIdMap, fns.keyBy(experiment.variations, 'id')); | |||
|
|||
fns.forOwn(experiment.variationKeyMap, function(variation) { | |||
Object.keys(experiment.variationKeyMap || {}).forEach(function (variationKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(experiment.variationKeyMap || {}).forEach(function (variationKey) { | |
jsSdkUtils.objectValues(experiment.variationKeyMap || {}).forEach(function (variation) { |
@@ -92,7 +93,8 @@ module.exports = { | |||
projectConfig.experimentFeatureMap = {}; | |||
|
|||
projectConfig.featureKeyMap = fns.keyBy(projectConfig.featureFlags || [], 'key'); | |||
fns.forOwn(projectConfig.featureKeyMap, function(feature) { | |||
Object.keys(projectConfig.featureKeyMap || {}).forEach(function (featureKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(projectConfig.featureKeyMap || {}).forEach(function (featureKey) { | |
jsSdkUtils.objectValues(projectConfig.featureKeyMap || {}).forEach(function (feature) { |
} | ||
}.bind(this) | ||
); | ||
Object.keys(configObj.featureKeyMap).forEach(function (featureKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys(configObj.featureKeyMap).forEach(function (featureKey) { | |
jsSdkUtils.objectValues(configObj.featureKeyMap).forEach(function (feature) { |
@zashraf1985 LGTM - could you please resolve the merge conflicts? |
@mjc1283 this is ready to be merged |
Summary
Removed
lodash.forOwn
function and replaced all the usaes withObject.keys
. This is part of an effort to remove lodash dependency to reduce bundle size.Test Plan
All Unit tests and Full Stack SDK compatibility passed