DEV: polyfill Object.values and use where needed

DEV: polyfill Object.values and use where needed

We now have polyfills for values IE and entries IE

This commit uses values where appropriate to eliminate an extra lookup
This simplifies the code a bit.

Followup to: 7f089f07a773c2d6dd26ed000ff0123318c37b0f

From 912a7105d2e1647929249ec7f881c34714880c44 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Wed, 28 Nov 2018 10:07:05 +1100
Subject: [PATCH] DEV: polyfill Object.values and use where needed

We now have polyfills for `values` IE and `entries` IE

This commit uses values where appropriate to eliminate an extra lookup
This simplifies the code a bit.

Followup to: 7f089f07a773c2d6dd26ed000ff0123318c37b0f

diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6
index 8417e95..32058c6 100644
--- a/app/assets/javascripts/discourse/components/composer-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6
@@ -526,9 +526,7 @@ export default Ember.Component.extend({
       post.set("refreshedPost", true);
     }
 
-    Object.keys(oneboxes).forEach(oneboxURL => {
-      const onebox = oneboxes[oneboxURL];
-
+    Object.values(oneboxes).forEach(onebox => {
       onebox.forEach($onebox => {
         load({
           elem: $onebox,
diff --git a/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6 b/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
index b62b440..a55bce8 100644
--- a/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
+++ b/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
@@ -252,7 +252,7 @@ export default MountWidget.extend({
       uncloak(post, this);
     });
 
-    Object.keys(prev).forEach(pn => cloak(prev[pn], this));
+    Object.values(prev).forEach(node => cloak(node, this));
 
     this._previouslyNearby = newPrev;
     this.screenTrack.setOnscreen(onscreenPostNumbers);
diff --git a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6
index 4d86d44..d29f1c8 100644
--- a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6
+++ b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6
@@ -373,8 +373,7 @@ const TopicTrackingState = Discourse.Model.extend({
 
   countCategory(category_id) {
     let sum = 0;
-    Object.keys(this.states).forEach(topicState => {
-      const topic = this.states[topicState];
+    Object.values(this.states).forEach(topic => {
       if (topic.category_id === category_id && !topic.deleted) {
         sum +=
           topic.last_read_post_number === null ||
diff --git a/app/assets/javascripts/discourse/widgets/post-menu.js.es6 b/app/assets/javascripts/discourse/widgets/post-menu.js.es6
index bb18377..c796a5e 100644
--- a/app/assets/javascripts/discourse/widgets/post-menu.js.es6
+++ b/app/assets/javascripts/discourse/widgets/post-menu.js.es6
@@ -404,8 +404,7 @@ export default createWidget("post-menu", {
       ])
     );
 
-    Object.keys(_extraButtons).forEach(k => {
-      const builder = _extraButtons[k];
+    Object.values(_extraButtons).forEach(builder => {
       if (builder) {
         const buttonAtts = builder(attrs, this.state, this.siteSettings);
         if (buttonAtts) {
diff --git a/app/assets/javascripts/polyfills.js b/app/assets/javascripts/polyfills.js
index c2f9fd9..a4a90df 100644
--- a/app/assets/javascripts/polyfills.js
+++ b/app/assets/javascripts/polyfills.js
@@ -10,6 +10,19 @@ if (!Object.entries) {
   };
 }
 
+// adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
+// missing in ie only
+if (!Object.values) {
+  Object.values = function(obj) {
+    var ownProps = Object.keys(obj),
+      i = ownProps.length,
+      resArray = new Array(i); // preallocate the Array
+    while (i--) resArray[i] = obj[ownProps[i]];
+
+    return resArray;
+  };
+}
+
 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
 if (typeof Object.assign !== "function") {
   // Must be writable: true, enumerable: false, configurable: true

GitHub

1 Like