Repository: TheOdinProject/odin-bot-v2
Pull request #511 (2024-03-14 13:17:43)

Feat: Make Update FAQ segment logic more concise
## Because
- I was looking at this recently, and I wasn't entirely happy with how I wrote this


## This PR
- changes the delimiter switch to a sensible default of prepend unless said otherwise
- changes the adding code to a ternary instead of an if, and in doing so removes the weird double negation I had going on
- updates tests to account for this

## Pull Request Requirements
<!-- Replace the whitespace between the square brackets with an 'x', e.g. [x]. After you create the PR, they will become checkboxes that you can click on. -->
-   [x] I have thoroughly read and understand [The Odin Project Contributing Guide](https://github.com/TheOdinProject/.github/blob/main/CONTRIBUTING.md)
-   [x] The title of this PR follows the `location of change: brief description of change` format, e.g. `Callbacks command: Update verbiage`
-   [x] The `Because` section summarizes the reason for this PR
-   [x] The `This PR` section has a bullet point list describing the changes in this PR
-   [x] If this PR addresses an open issue, it is linked in the `Issue` section
-   [x] If applicable, I have ensured all tests related to any command files included in this PR pass, and/or all snapshots are up to date



Patch:
diff --git a/new-era-commands/slash/searchmdn.js b/new-era-commands/slash/searchmdn.js
deleted file mode 100644
index 9c9f13f..0000000
--- a/new-era-commands/slash/searchmdn.js
+++ /dev/null
@@ -1,28 +0,0 @@
-const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
-
-module.exports = {
-  data: new SlashCommandBuilder()
-    .setName('searchmdn')
-    .setDescription('Search for terms in MDN')
-    .addStringOption((option) => option
-      .setName('prompt')
-      .setDescription('term to search')
-      .setRequired(true))
-    .addUserOption((option) => option.setName('user').setDescription('user to ping')),
-  execute: async (interaction) => {
-    const userId = interaction.options.getUser('user')?.id;
-    const prompt = interaction.options.getString('prompt');
-    const searchUrl = `https://developer.mozilla.org/en-US/search?q=${prompt.replaceAll(' ', '+')}`;
-
-    const searchEmbed = new EmbedBuilder()
-      .setColor('#cc9543')
-      .setTitle('Search MDN')
-      .setDescription(
-        `Here are the [MDN results for "${prompt}"](${searchUrl})`,
-      );
-    await interaction.reply({
-      content: userId ? `<@${userId}>` : '',
-      embeds: [searchEmbed],
-    });
-  },
-};
diff --git a/package-lock.json b/package-lock.json
index 8d7125a..a06fdbe 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,7 +11,7 @@
       "dependencies": {
         "@discordjs/rest": "^2.2.0",
         "axios": "^1.6.7",
-        "discord-api-types": "^0.37.73",
+        "discord-api-types": "^0.37.60",
         "discord.js": "^14.14.1",
         "dotenv": "^16.4.5",
         "glob": "^10.3.10",
@@ -23,12 +23,12 @@
         "@babel/preset-env": "^7.24.0",
         "eslint": "^8.56.0",
         "eslint-config-airbnb-base": "^15.0.0",
-        "eslint-config-prettier": "^9.1.0",
+        "eslint-config-prettier": "^9.0.0",
         "eslint-plugin-import": "^2.29.1",
         "husky": "^9.0.11",
         "jest": "^29.6.1",
         "lint-staged": "^15.2.2",
-        "nodemon": "^3.1.0"
+        "nodemon": "^3.0.1"
       }
     },
     "node_modules/@aashutoshrathi/word-wrap": {
@@ -2018,11 +2018,6 @@
         "node": ">=16.11.0"
       }
     },
-    "node_modules/@discordjs/builders/node_modules/discord-api-types": {
-      "version": "0.37.61",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-    },
     "node_modules/@discordjs/collection": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-2.0.0.tgz",
@@ -2042,11 +2037,6 @@
         "node": ">=16.11.0"
       }
     },
-    "node_modules/@discordjs/formatters/node_modules/discord-api-types": {
-      "version": "0.37.61",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-    },
     "node_modules/@discordjs/rest": {
       "version": "2.2.0",
       "resolved": "https://registry.npmjs.org/@discordjs/rest/-/rest-2.2.0.tgz",
@@ -2066,11 +2056,6 @@
         "node": ">=16.11.0"
       }
     },
-    "node_modules/@discordjs/rest/node_modules/discord-api-types": {
-      "version": "0.37.61",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-    },
     "node_modules/@discordjs/util": {
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/@discordjs/util/-/util-1.0.2.tgz",
@@ -2098,11 +2083,6 @@
         "node": ">=16.11.0"
       }
     },
-    "node_modules/@discordjs/ws/node_modules/discord-api-types": {
-      "version": "0.37.61",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-    },
     "node_modules/@eslint-community/eslint-utils": {
       "version": "4.4.0",
       "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz",
@@ -3855,9 +3835,9 @@
       }
     },
     "node_modules/discord-api-types": {
-      "version": "0.37.73",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.73.tgz",
-      "integrity": "sha512-mi915PBUxF1G233EwHKNegNAF/tVfiSRN9+hKwu0G3NpbtLXvWUxCuCjgSyY+QmQ6/Hvpqm0xs5HxzfvhAS20A=="
+      "version": "0.37.61",
+      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
+      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
     },
     "node_modules/discord.js": {
       "version": "14.14.1",
@@ -3891,11 +3871,6 @@
         "node": ">=16.11.0"
       }
     },
-    "node_modules/discord.js/node_modules/discord-api-types": {
-      "version": "0.37.61",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-    },
     "node_modules/doctrine": {
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz",
@@ -4151,9 +4126,9 @@
       }
     },
     "node_modules/eslint-config-prettier": {
-      "version": "9.1.0",
-      "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz",
-      "integrity": "sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==",
+      "version": "9.0.0",
+      "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.0.0.tgz",
+      "integrity": "sha512-IcJsTkJae2S35pRsRAwoCE+925rJJStOdkKnLVgtE+tEpqU0EVVM7OqrwxqgptKdX29NUwC82I5pXsGFIgSevw==",
       "dev": true,
       "bin": {
         "eslint-config-prettier": "bin/cli.js"
@@ -6901,13 +6876,13 @@
       "dev": true
     },
     "node_modules/nodemon": {
-      "version": "3.1.0",
-      "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz",
-      "integrity": "sha512-xqlktYlDMCepBJd43ZQhjWwMw2obW/JRvkrLxq5RCNcuDDX1DbcPT+qT1IlIIdf+DhnWs90JpTMe+Y5KxOchvA==",
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz",
+      "integrity": "sha512-g9AZ7HmkhQkqXkRc20w+ZfQ73cHLbE8hnPbtaFbFtCumZsjyMhKk9LajQ07U5Ux28lvFjZ5X7HvWR1xzU8jHVw==",
       "dev": true,
       "dependencies": {
         "chokidar": "^3.5.2",
-        "debug": "^4",
+        "debug": "^3.2.7",
         "ignore-by-default": "^1.0.1",
         "minimatch": "^3.1.2",
         "pstree.remy": "^1.1.8",
@@ -6928,6 +6903,15 @@
         "url": "https://opencollective.com/nodemon"
       }
     },
+    "node_modules/nodemon/node_modules/debug": {
+      "version": "3.2.7",
+      "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
+      "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
+      "dev": true,
+      "dependencies": {
+        "ms": "^2.1.1"
+      }
+    },
     "node_modules/nodemon/node_modules/has-flag": {
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
@@ -10024,13 +10008,6 @@
         "fast-deep-equal": "^3.1.3",
         "ts-mixer": "^6.0.3",
         "tslib": "^2.6.2"
-      },
-      "dependencies": {
-        "discord-api-types": {
-          "version": "0.37.61",
-          "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-          "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-        }
       }
     },
     "@discordjs/collection": {
@@ -10044,13 +10021,6 @@
       "integrity": "sha512-wTcI1Q5cps1eSGhl6+6AzzZkBBlVrBdc9IUhJbijRgVjCNIIIZPgqnUj3ntFODsHrdbGU8BEG9XmDQmgEEYn3w==",
       "requires": {
         "discord-api-types": "0.37.61"
-      },
-      "dependencies": {
-        "discord-api-types": {
-          "version": "0.37.61",
-          "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-          "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-        }
       }
     },
     "@discordjs/rest": {
@@ -10067,13 +10037,6 @@
         "magic-bytes.js": "^1.5.0",
         "tslib": "^2.6.2",
         "undici": "5.27.2"
-      },
-      "dependencies": {
-        "discord-api-types": {
-          "version": "0.37.61",
-          "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-          "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-        }
       }
     },
     "@discordjs/util": {
@@ -10095,13 +10058,6 @@
         "discord-api-types": "0.37.61",
         "tslib": "^2.6.2",
         "ws": "^8.14.2"
-      },
-      "dependencies": {
-        "discord-api-types": {
-          "version": "0.37.61",
-          "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-          "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
-        }
       }
     },
     "@eslint-community/eslint-utils": {
@@ -11420,9 +11376,9 @@
       "dev": true
     },
     "discord-api-types": {
-      "version": "0.37.73",
-      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.73.tgz",
-      "integrity": "sha512-mi915PBUxF1G233EwHKNegNAF/tVfiSRN9+hKwu0G3NpbtLXvWUxCuCjgSyY+QmQ6/Hvpqm0xs5HxzfvhAS20A=="
+      "version": "0.37.61",
+      "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
+      "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
     },
     "discord.js": {
       "version": "14.14.1",
@@ -11449,11 +11405,6 @@
           "version": "1.5.3",
           "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-1.5.3.tgz",
           "integrity": "sha512-SVb428OMd3WO1paV3rm6tSjM4wC+Kecaa1EUGX7vc6/fddvw/6lg90z4QtCqm21zvVe92vMMDt9+DkIvjXImQQ=="
-        },
-        "discord-api-types": {
-          "version": "0.37.61",
-          "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
-          "integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
         }
       }
     },
@@ -11741,9 +11692,9 @@
       }
     },
     "eslint-config-prettier": {
-      "version": "9.1.0",
-      "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz",
-      "integrity": "sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==",
+      "version": "9.0.0",
+      "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.0.0.tgz",
+      "integrity": "sha512-IcJsTkJae2S35pRsRAwoCE+925rJJStOdkKnLVgtE+tEpqU0EVVM7OqrwxqgptKdX29NUwC82I5pXsGFIgSevw==",
       "dev": true,
       "requires": {}
     },
@@ -13633,13 +13584,13 @@
       "dev": true
     },
     "nodemon": {
-      "version": "3.1.0",
-      "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz",
-      "integrity": "sha512-xqlktYlDMCepBJd43ZQhjWwMw2obW/JRvkrLxq5RCNcuDDX1DbcPT+qT1IlIIdf+DhnWs90JpTMe+Y5KxOchvA==",
+      "version": "3.0.1",
+      "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz",
+      "integrity": "sha512-g9AZ7HmkhQkqXkRc20w+ZfQ73cHLbE8hnPbtaFbFtCumZsjyMhKk9LajQ07U5Ux28lvFjZ5X7HvWR1xzU8jHVw==",
       "dev": true,
       "requires": {
         "chokidar": "^3.5.2",
-        "debug": "^4",
+        "debug": "^3.2.7",
         "ignore-by-default": "^1.0.1",
         "minimatch": "^3.1.2",
         "pstree.remy": "^1.1.8",
@@ -13650,6 +13601,15 @@
         "undefsafe": "^2.0.5"
       },
       "dependencies": {
+        "debug": {
+          "version": "3.2.7",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
+          "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
+          "dev": true,
+          "requires": {
+            "ms": "^2.1.1"
+          }
+        },
         "has-flag": {
           "version": "3.0.0",
           "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
diff --git a/package.json b/package.json
index 0949593..fcedf23 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,7 @@
   "dependencies": {
     "@discordjs/rest": "^2.2.0",
     "axios": "^1.6.7",
-    "discord-api-types": "^0.37.73",
+    "discord-api-types": "^0.37.60",
     "discord.js": "^14.14.1",
     "dotenv": "^16.4.5",
     "glob": "^10.3.10",
@@ -26,12 +26,12 @@
     "@babel/preset-env": "^7.24.0",
     "eslint": "^8.56.0",
     "eslint-config-airbnb-base": "^15.0.0",
-    "eslint-config-prettier": "^9.1.0",
+    "eslint-config-prettier": "^9.0.0",
     "eslint-plugin-import": "^2.29.1",
     "husky": "^9.0.11",
     "jest": "^29.6.1",
     "lint-staged": "^15.2.2",
-    "nodemon": "^3.1.0"
+    "nodemon": "^3.0.1"
   },
   "husky": {
     "hooks": {
diff --git a/services/update-faq/update-faq.service.js b/services/update-faq/update-faq.service.js
index 7efe461..7dcca51 100644
--- a/services/update-faq/update-faq.service.js
+++ b/services/update-faq/update-faq.service.js
@@ -46,29 +46,28 @@ class UpdateFAQsService {
     prev.forEach((message) => message.delete());
   }
 
-  static segments(string, length, delimiter, doNotPrependDelimiter) {
+  static segments(string, length, delimiter, prependDelimiter = true) {
     const segmentedString = string.split(delimiter)
     const segments = []
     let currentSection = ""
 
     for (let i = 0; i < segmentedString.length; i += 1) {
-      let segment = segmentedString[i]
+      const segment = prependDelimiter ? delimiter + segmentedString[i] : segmentedString[i];
 
-      if (!doNotPrependDelimiter) {
-        segment = delimiter + segmentedString[i]
-      }
       if (segmentedString[i] === '') {
         // eslint-disable-next-line no-continue
-        continue
+        continue;
       }
+
       if ((currentSection.length + segment.length) >= length) {
-          segments.push(currentSection)
-          currentSection = segment
+        segments.push(currentSection);
+        currentSection = segment;
       } else {
-        currentSection += segment
+        currentSection += segment;
       }
     }
-    segments.push(currentSection)
+
+    segments.push(currentSection);
     return segments;
   }
 }


Test patch:
diff --git a/services/update-faq/update-faq.service.test.js b/services/update-faq/update-faq.service.test.js
index 0494750..9a9429b 100644
--- a/services/update-faq/update-faq.service.test.js
+++ b/services/update-faq/update-faq.service.test.js
@@ -49,7 +49,7 @@ describe('UpdateFAQsService', () => {
         const string = '### The Odin Project.### Your Career in Web Development starts here';
         const delimiter = '###'
         const length = 25;
-        const result = UpdateFAQsService.segments(string, length, delimiter, true);
+        const result = UpdateFAQsService.segments(string, length, delimiter, false);
         expect(result).toEqual([' The Odin Project.', ' Your Career in Web Development starts here']);
       })
     })
@@ -69,7 +69,7 @@ describe('UpdateFAQsService', () => {
         const string = 'The Odin Project. Your Career in Web Development starts here';
         const delimiter = '###'
         const length = 75;
-        const result = UpdateFAQsService.segments(string, length, delimiter, true);
+        const result = UpdateFAQsService.segments(string, length, delimiter, false);
         expect(result).toEqual(['The Odin Project. Your Career in Web Development starts here']);
       })
     })