FIX: Use Map instead of Object for caching (#14887)

FIX: Use Map instead of Object for caching (#14887)

Objects have default properties, such as “constructor” that can cause issues when using similar texts as keys.

diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
index b03f30f..34384a5 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
@@ -1689,6 +1689,7 @@ var bar = 'bar';
     };
 
     assert.cookedOptions("test fun funny", opts, "<p>test times funny</p>");
+    assert.cookedOptions("constructor", opts, "<p>constructor</p>");
   });
 
   test("watched words link", function (assert) {
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/watched-words.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/watched-words.js
index d82e13f..7c09bf1 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/watched-words.js
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/watched-words.js
@@ -73,7 +73,7 @@ export function setup(helper) {
       return;
     }
 
-    const cache = {};
+    const cache = new Map();
 
     md.core.ruler.push("watched-words", (state) => {
       for (let j = 0, l = state.tokens.length; j < l; j++) {
@@ -153,8 +153,14 @@ export function setup(helper) {
 
           if (currentToken.type === "text") {
             const text = currentToken.content;
-            const matches = (cache[text] =
-              cache[text] || findAllMatches(text, matchers));
+
+            let matches;
+            if (cache.has(text)) {
+              matches = cache.get(text);
+            } else {
+              matches = findAllMatches(text, matchers);
+              cache.set(text, matches);
+            }
 
             // Now split string to nodes
             const nodes = [];

GitHub sha: 32a174d88378f6a91a66964510eb3213e4e2387b

This commit appears in #14887 which was approved by eviltrout and ZogStriP. It was merged by nbianca.