39 lines
1.8 KiB
TypeScript
39 lines
1.8 KiB
TypeScript
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.
|
|
* 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[] {
|
|
// 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<string> = new Set();
|
|
|
|
const matches = comment.body.match(regexPattern);
|
|
if (matches) {
|
|
matches.forEach(match => {
|
|
// 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) {
|
|
// Convert the username to lowercase and add it to the Set to ensure uniqueness.
|
|
const username = `${usernameMatch[1].toLowerCase()}`;
|
|
foundUsernames.add(username);
|
|
}
|
|
});
|
|
}
|
|
|
|
return Array.from(foundUsernames);
|
|
}
|
|
}
|