Skip to content

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

Merged
merged 11 commits into from
Feb 27, 2020

Conversation

zashraf1985
Copy link
Contributor

@zashraf1985 zashraf1985 commented Feb 18, 2020

Summary

Removed lodash.forOwn function and replaced all the usaes with Object.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

@coveralls
Copy link

coveralls commented Feb 18, 2020

Coverage Status

Coverage decreased (-0.008%) to 97.331% when pulling 721aef6 on zeeshan/replace-lodash-forown into 59cdad4 on master.

@zashraf1985 zashraf1985 marked this pull request as ready for review February 18, 2020 06:38
@zashraf1985 zashraf1985 requested a review from a team as a code owner February 18, 2020 06:38
Comment on lines 35 to 42
forOwn: function(obj, callback) {
for(var key in obj) {
if(obj.hasOwnProperty(key)){
var val = obj[key];
callback(val, key, obj);
}
}
},
Copy link
Contributor

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 or undefined 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.

Copy link
Contributor Author

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.

Copy link
Contributor

@mjc1283 mjc1283 left a 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.

@zashraf1985 zashraf1985 changed the title Removed dependency on lodash.forOwn refactor: Removed dependency on lodash.forOwn Feb 21, 2020
@zashraf1985 zashraf1985 removed their assignment Feb 21, 2020
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object.keys(enums.NOTIFICATION_TYPES).forEach(function(key) {
jsSdkUtils.objectValues(enums.NOTIFICATION_TYPES).forEach(function(notificationTypeEnum) {

Comment on lines 112 to 117
if (indexToRemove !== undefined && typeToRemove !== undefined) {
return false;
}
});

Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object.keys(projectConfig.featureKeyMap || {}).forEach(function (featureKey) {
jsSdkUtils.objectValues(projectConfig.featureKeyMap || {}).forEach(function (feature) {

}
}.bind(this)
);
Object.keys(configObj.featureKeyMap).forEach(function (featureKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object.keys(configObj.featureKeyMap).forEach(function (featureKey) {
jsSdkUtils.objectValues(configObj.featureKeyMap).forEach(function (feature) {

@mjc1283
Copy link
Contributor

mjc1283 commented Feb 26, 2020

@zashraf1985 LGTM - could you please resolve the merge conflicts?

@zashraf1985
Copy link
Contributor Author

@mjc1283 this is ready to be merged

@mjc1283 mjc1283 merged commit e07181e into master Feb 27, 2020
@mjc1283 mjc1283 deleted the zeeshan/replace-lodash-forown branch February 27, 2020 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants