Content SafetyGetting Started

Getting Started

Content safety library for detecting profanity, slurs, and inappropriate content across 19 languages with Unicode support.

Last updated:

Getting Started

@visulima/content-safety is a robust TypeScript library for detecting banned words, profanity, slurs, hate speech, and inappropriate content across multiple languages. It provides accurate, Unicode-aware text analysis with position information for censoring or highlighting.

Key Features

Multi-Language Support

Comprehensive banned word detection across 19 languages:

  • European: English, German, Spanish, French, Italian, Dutch, Polish, Portuguese, Swedish, Russian, Irish
  • Middle Eastern: Arabic, Persian/Farsi, Turkish, Azerbaijani
  • Asian: Japanese, Korean, Chinese, Hindi

Unicode-Aware

Full support for international scripts and complex text:

  • CJK Scripts: Japanese, Korean, Chinese (no word boundaries required)
  • RTL Scripts: Arabic, Persian, Hebrew
  • Complex Scripts: Devanagari (Hindi), Thai, and others
  • Unicode Normalization: Consistent matching with NFC normalization

High Performance

Optimized for production use:

  • Eager Regex Compilation: Cache built at module load (no first-call lag)
  • Single Pass Checking: All languages checked simultaneously
  • Efficient Pattern Matching: Longest phrases matched first

Precise Position Tracking

Detailed match information for flexible handling:

  • Start and end indices for each match
  • Language attribution (which dictionary matched)
  • Support for highlighting, censoring, or rejection

Advanced Features

  • Case-Insensitive: Matches regardless of capitalization
  • Multi-Word Phrases: Detects compound expressions (e.g., "white trash")
  • Leet-Speak Detection: Includes common variants (e.g., "b1tch")
  • Word Boundary Aware: Prevents false positives in compound words
  • Zero Dependencies: Pure TypeScript, works in browser and Node.js

Quick Start

Installation

npm install @visulima/content-safety
yarn add @visulima/content-safety
pnpm add @visulima/content-safety

Basic Usage

import { checkBannedWords } from "@visulima/content-safety";

// Check clean text
const result = checkBannedWords("Hello, how are you today?");
console.log(result.hasBannedWords); // false
console.log(result.matches); // []

// Check text with banned words
const result2 = checkBannedWords("This contains badword");
console.log(result2.hasBannedWords); // true
console.log(result2.matches[0]);
// {
//   word: "badword",
//   startIndex: 14,
//   endIndex: 21,
//   language: "en"
// }

Use Cases

Content Moderation

Filter user-generated content in forums, comments, and chat applications:

import { checkBannedWords } from "@visulima/content-safety";

function moderateContent(userInput: string) {
    const result = checkBannedWords(userInput);

    if (result.hasBannedWords) {
        return {
            allowed: false,
            reason: `Content contains ${result.matches.length} inappropriate word(s)`,
        };
    }

    return { allowed: true };
}

Text Censoring

Automatically censor inappropriate content:

function censorText(text: string): string {
    const result = checkBannedWords(text);

    if (!result.hasBannedWords) {
        return text;
    }

    let censored = text;
    // Process in reverse to maintain indices
    for (const match of result.matches.reverse()) {
        const replacement = "*".repeat(match.word.length);
        censored = censored.slice(0, match.startIndex) + replacement + censored.slice(match.endIndex);
    }

    return censored;
}

Real-Time Validation

Provide instant feedback in forms and text editors:

function validateInput(text: string) {
    const result = checkBannedWords(text);

    return {
        isValid: !result.hasBannedWords,
        matches: result.matches.map((m) => ({
            position: m.startIndex,
            length: m.word.length,
            language: m.language,
        })),
    };
}

Next Steps

Browser and Server Support

Works in all modern environments:

  • Node.js 18+
  • Browsers (Chrome, Firefox, Safari, Edge)
  • Deno
  • Bun
  • Edge Functions (Vercel, Cloudflare Workers)
  • React Native
  • Electron
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues