Files
RedditBot/src/services/conversation-learning-redactor.ts
T

17 lines
610 B
TypeScript

const URL = /https?:\/\/\S+/gi;
const EMAIL = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi;
const PHONE = /(?<!\w)(?:\+?\d[\d ()-]{7,}\d)(?!\w)/g;
const USERNAME = /(?:^|\s)(?:u\/|@)[A-Za-z0-9_-]{2,}/g;
/** Removes direct identifiers before durable example storage or prompt use. */
export function redactLearningText(value: string, maxLength = 500): string {
return value
.replace(URL, '[url]')
.replace(EMAIL, '[email]')
.replace(PHONE, '[phone]')
.replace(USERNAME, (match) => `${match[0] === ' ' ? ' ' : ''}[username]`)
.replace(/\s+/g, ' ')
.trim()
.slice(0, maxLength);
}