DEV: removes _.each from codebase (#6629)

DEV: removes _.each from codebase (#6629)

From 9672c92045f31cc47976518d22137015722805d6 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Tue, 20 Nov 2018 14:46:58 +0100
Subject: [PATCH] DEV: removes _.each from codebase (#6629)


diff --git a/app/assets/javascripts/admin/controllers/admin-customize-colors.js.es6 b/app/assets/javascripts/admin/controllers/admin-customize-colors.js.es6
index 945a46a..60a0af9 100644
--- a/app/assets/javascripts/admin/controllers/admin-customize-colors.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-customize-colors.js.es6
@@ -11,7 +11,7 @@ export default Ember.Controller.extend({
 
   baseColors: function() {
     var baseColorsHash = Em.Object.create({});
-    _.each(this.get("baseColorScheme.colors"), function(color) {
+    this.get("baseColorScheme.colors").forEach(color => {
       baseColorsHash.set(color.get("name"), color);
     });
     return baseColorsHash;
diff --git a/app/assets/javascripts/admin/controllers/admin-user-badges.js.es6 b/app/assets/javascripts/admin/controllers/admin-user-badges.js.es6
index b72cdae..784d207 100644
--- a/app/assets/javascripts/admin/controllers/admin-user-badges.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-user-badges.js.es6
@@ -20,13 +20,13 @@ export default Ember.Controller.extend(GrantBadgeController, {
     _(grouped).each(function(badges) {
       var lastGranted = badges[0].granted_at;
 
-      _.each(badges, function(badge) {
+      badges.forEaach(badge => {
         lastGranted =
           lastGranted < badge.granted_at ? badge.granted_at : lastGranted;
       });
 
       if (badges.length === 1 || expandedBadges.includes(badges[0].badge.id)) {
-        _.each(badges, badge => expanded.push(badge));
+        badges.forEach(badge => expanded.push(badge));
         return;
       }
 
diff --git a/app/assets/javascripts/admin/controllers/modals/admin-badge-preview.js.es6 b/app/assets/javascripts/admin/controllers/modals/admin-badge-preview.js.es6
index aa91a1b..2702362 100644
--- a/app/assets/javascripts/admin/controllers/modals/admin-badge-preview.js.es6
+++ b/app/assets/javascripts/admin/controllers/modals/admin-badge-preview.js.es6
@@ -21,7 +21,7 @@ export default Ember.Controller.extend({
     var raw = this.get("model.query_plan"),
       returned = "<pre class='badge-query-plan'>";
 
-    _.each(raw, function(linehash) {
+    raw.forEach(linehash => {
       returned += escapeExpression(linehash["QUERY PLAN"]);
       returned += "<br>";
     });
diff --git a/app/assets/javascripts/admin/models/admin-user.js.es6 b/app/assets/javascripts/admin/models/admin-user.js.es6
index ec230ff..fc36b9b 100644
--- a/app/assets/javascripts/admin/models/admin-user.js.es6
+++ b/app/assets/javascripts/admin/models/admin-user.js.es6
@@ -542,7 +542,7 @@ const AdminUser = Discourse.User.extend({
 
 AdminUser.reopenClass({
   bulkApprove(users) {
-    _.each(users, function(user) {
+    users.forEach(user => {
       user.setProperties({
         approved: true,
         can_approve: false,
@@ -557,7 +557,7 @@ AdminUser.reopenClass({
   },
 
   bulkReject(users) {
-    _.each(users, function(user) {
+    users.forEach(user => {
       user.set("can_approve", false);
       user.set("selected", false);
     });
diff --git a/app/assets/javascripts/admin/models/color-scheme.js.es6 b/app/assets/javascripts/admin/models/color-scheme.js.es6
index 04d661d..e781320 100644
--- a/app/assets/javascripts/admin/models/color-scheme.js.es6
+++ b/app/assets/javascripts/admin/models/color-scheme.js.es6
@@ -19,7 +19,7 @@ const ColorScheme = Discourse.Model.extend(Ember.Copyable, {
 
   schemeJson() {
     let buffer = [];
-    _.each(this.get("colors"), c => {
+    this.get("colors").forEach(c => {
       buffer.push(`  "${c.get("name")}": "${c.get("hex")}"`);
     });
 
@@ -32,7 +32,7 @@ const ColorScheme = Discourse.Model.extend(Ember.Copyable, {
       can_edit: true,
       colors: Em.A()
     });
-    _.each(this.get("colors"), function(c) {
+    this.get("colors").forEach(c => {
       newScheme.colors.pushObject(
         ColorSchemeColor.create({
           name: c.get("name"),
@@ -86,7 +86,7 @@ const ColorScheme = Discourse.Model.extend(Ember.Copyable, {
       data.name = this.name;
       data.base_scheme_id = this.get("base_scheme_id");
       data.colors = [];
-      _.each(this.get("colors"), function(c) {
+      this.get("colors").forEach(c => {
         if (!self.id || c.get("changed")) {
           data.colors.pushObject({ name: c.get("name"), hex: c.get("hex") });
         }
@@ -107,9 +107,7 @@ const ColorScheme = Discourse.Model.extend(Ember.Copyable, {
       }
       if (!opts || !opts.enabledOnly) {
         self.startTrackingChanges();
-        _.each(self.get("colors"), function(c) {
-          c.startTrackingChanges();
-        });
+        self.get("colors").forEach(c => c.startTrackingChanges());
       }
       self.set("savingStatus", I18n.t("saved"));
       self.set("saving", false);
@@ -130,7 +128,7 @@ ColorScheme.reopenClass({
   findAll: function() {
     var colorSchemes = ColorSchemes.create({ content: [], loading: true });
     return ajax("/admin/color_schemes").then(function(all) {
-      _.each(all, function(colorScheme) {
+      all.forEach(colorScheme => {
         colorSchemes.pushObject(
           ColorScheme.create({
             id: colorScheme.id,
diff --git a/app/assets/javascripts/admin/models/report.js.es6 b/app/assets/javascripts/admin/models/report.js.es6
index 5e1829f..709dac0 100644
--- a/app/assets/javascripts/admin/models/report.js.es6
+++ b/app/assets/javascripts/admin/models/report.js.es6
@@ -62,7 +62,7 @@ const Report = Discourse.Model.extend({
       let d,
         sum = 0,
         count = 0;
-      _.each(this.data, datum => {
+      this.data.forEach(datum => {
         d = moment(datum.x);
         if (d >= earliestDate && d <= latestDate) {
           sum += datum.y;
diff --git a/app/assets/javascripts/admin/routes/admin-badges.js.es6 b/app/assets/javascripts/admin/routes/admin-badges.js.es6
index 6d55997..9fee47f 100644
--- a/app/assets/javascripts/admin/routes/admin-badges.js.es6
+++ b/app/assets/javascripts/admin/routes/admin-badges.js.es6
@@ -17,9 +17,10 @@ export default Discourse.Route.extend({
     const badgeTriggers = [];
     const badgeGroupings = [];
 
-    _.each(json.admin_badges.triggers, function(v, k) {
+    Object.keys(json.admin_badges.triggers).forEach(k => {
+      const id = json.admin_badges.triggers[k];
       badgeTriggers.push({
-        id: v,
+        id,
         name: I18n.t("admin.badges.trigger_type." + k)
       });
     });
diff --git a/app/assets/javascripts/discourse/components/create-topics-notice.js.es6 b/app/assets/javascripts/discourse/components/create-topics-notice.js.es6
index a6dcff0..8a85574 100644
--- a/app/assets/javascripts/discourse/components/create-topics-notice.js.es6
+++ b/app/assets/javascripts/discourse/components/create-topics-notice.js.es6
@@ -20,7 +20,7 @@ export default Ember.Component.extend({
         postCount = 0;
 
       // Use data we already have before fetching live stats
-      _.each(this.site.get("categories"), function(c) {
+      this.site.get("categories").forEach(c => {
         if (!c.get("read_restricted")) {
           topicCount += c.get("topic_count");
           postCount += c.get("post_count");
diff --git a/app/assets/javascripts/discourse/initializers/live-development.js.es6 b/app/assets/javascripts/discourse/initializers/live-development.js.es6
index 250ce27..8104bdf 100644
--- a/app/assets/javascripts/discourse/initializers/live-development.js.es6
+++ b/app/assets/javascripts/discourse/initializers/live-development.js.es6
@@ -53,7 +53,7 @@ export default {
         // hbs notifications only happen in dev
         Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
       }
-      _.each(data, function(me) {
+      data.forEach(me => {
         if (me === "refresh") {
           // Refresh if necessary
           document.location.reloa

GitHub

1 Like

This remove a lot of null protection from the code, are you sure this is right? I did spot that you strategically added || [] in some spots, but not all.

1 Like

Yes Im unsure about this, I have added this where it would fail in tests or where I knew it could happen, but Im a bit reluctant to apply this pattern blindly.

2 Likes

I see, I guess we just have to keep our eye out for issues here.

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there: