diff --git a/src/rdrama/services/CommentParser.ts b/src/rdrama/services/CommentParser.ts index fa003c8..32e222e 100644 --- a/src/rdrama/services/CommentParser.ts +++ b/src/rdrama/services/CommentParser.ts @@ -1,22 +1,32 @@ import { Comment } from '../models/Comment'; +/** + * A utility class for parsing Reddit comments. + */ export class CommentParser { /** * Extracts Reddit usernames from the body of a single comment. - * @param comment A single Comment object to be processed. - * @returns An array of unique Reddit usernames found in the comment. + * This method looks for patterns that match Reddit usernames, which start with "/u/" or "u/", + * and collects them into a Set to ensure uniqueness. It then returns an array of these unique usernames, + * all in lowercase to maintain consistency. + * + * @param {Comment} comment - A single Comment object to be processed. + * @returns {string[]} An array of unique Reddit usernames found in the comment. */ public static extractUsernames(comment: Comment): string[] { - const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_]+)/g; + // Define the regex pattern to match Reddit usernames. It looks for the "/u/" prefix, + // optionally preceded by certain characters (like whitespace or punctuation), + // followed by the username consisting of alphanumeric characters and underscores. + const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_\-]+)/g; const foundUsernames: Set = new Set(); const matches = comment.body.match(regexPattern); if (matches) { matches.forEach(match => { - // Ensure the username is captured in a standardized format - const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/); + // Extract the username part from the match, ensuring it's in a consistent format. + const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_\-]+)/); if (usernameMatch) { - // Standardize to "username" format + // Convert the username to lowercase and add it to the Set to ensure uniqueness. const username = `${usernameMatch[1].toLowerCase()}`; foundUsernames.add(username); }