What you're building
An InputSanitizer class that cleans raw user input before it enters your application — preventing XSS injection, enforcing length limits, and normalizing whitespace. This is the first line of defense for any web form.
Requirements
escapeHtml(str)— replace&,<,>,",'with their HTML entities so the string is safe to render.stripTags(str)— remove all HTML/XML tags (anything matching<...>), leaving only the text content.normalizeWhitespace(str)— trim leading/trailing whitespace and collapse any internal runs of whitespace (spaces, tabs, newlines) into a single space.truncate(str, maxLength)— if the string exceedsmaxLengthcharacters, cut it and append"..."so the result is at mostmaxLengthcharacters total (including the ellipsis).sanitize(str, options)— run the pipeline in order based onoptions:options.escapeHtml(boolean) → applyescapeHtmloptions.stripTags(boolean) → applystripTagsoptions.normalizeWhitespace(boolean) → applynormalizeWhitespaceoptions.maxLength(number) → applytruncate
Stretch goals
- Add a
slugify(str)method that lowercases, replaces spaces with-, and strips non-alphanumeric characters (useful for URL slugs). - Add a
maskEmail(email)that turns"user@example.com"into"u***@example.com".
What this proves
String manipulation depth, security awareness, and composable utility design — the kind of utility library every team maintains.
⚡ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
escapeHtml encodes all five characters
stripTags removes HTML tags
normalizeWhitespace collapses spaces
truncate shortens to maxLength
Run your code to check these.