@2toad/profanity

中文文档
3.3.0发布于 2026/03/24MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 41.3万

A multi-language profanity filter with full TypeScript support

原始使用文档
文档目录11 节
  1. Getting Started
  2. Usage
  3. Options
  4. languages
  5. wholeWord
  6. grawlix
  7. grawlixChar
  8. Customize the word list
  9. Whitelist
  10. Benchmarking ⏱️
  11. Contributing 🤝

Profanity 🧼

GitHub Release Downloads Build status

A multi-language profanity filter with full TypeScript support

Getting Started

Install the package

npm i @2toad/profanity

If you're using Node 11.x or older, you'll need to install Profanity 1.x

Usage

import { profanity, CensorType } from '@2toad/profanity';
// or
const { profanity, CensorType } = require('@2toad/profanity');
profanity.exists('I like big butts and I cannot lie');
// true

profanity.exists('I like big glutes and I cannot lie');
// false

profanity.censor('I like big butts (aka arses) and I cannot lie');
// I like big @#$%&! (aka @#$%&!) and I cannot lie

profanity.censor('I like big butts (aka arses) and I cannot lie', CensorType.FirstChar);
// I like big *utts (aka *rses) and I cannot lie

Options

Create an instance of the Profanity class to change the default options:

import { Profanity } from '@2toad/profanity';

const profanity = new Profanity({
    languages: ['de'],
    wholeWord: false,
    grawlix: '*****',
    grawlixChar: '$',
});

languages

By default, this is set to ['en'] (English). You can change the default to any supported language, including multiple languages:

const profanity = new Profanity({
    languages: ['en', 'de'],
});

You can override this option by specifying the languages in exists or censor:

profanity.exists('Je suis un connard', ['fr']);
// true

profanity.censor('I like big butts and je suis un connard', CensorType.Word, ['en', 'de', 'fr']);
// I like big @#$%&! and je suis un @#$%&!

If no languages are specified in the method call, it will use the languages specified in the options.

wholeWord

By default, this is set to true so profanity only matches on whole words:

profanity.exists('Arsenic is poisonous but not profane');
// false

Setting this to false, results in partial word matches:

profanity.exists('Arsenic is poisonous but not profane');
// true (matched on arse)

Compound Words

Profanity detection works on parts of compound words, rather than treating hyphenated or underscore-separated words as indivisible.

When wholeWord is true, each portion of a compound word is analyzed for a match:

profanity.exists("Don't be an arsenic-monster");
// false

profanity.exists("Don't be an arse-monster");
// true (matched on arse)

Setting wholeWord to false, results in partial word matches on each portion of a compound word:

profanity.exists("Don't be an arsenic-monster");
// true (matched on arse)

unicodeWordBoundaries

When wholeWord is true, this controls whether word boundaries are Unicode-aware. By default this is set to false due to the performance impact.

  • When false (default), whole-word matching uses ASCII-style boundaries (similar to \b) plus underscore _ as a separator. This is fastest and ideal for ASCII inputs.
  • When true, whole-word matching uses Unicode-aware boundaries so words with diacritics (e.g., vehículo, horário) and compound separators are handled correctly.
const profanity = new Profanity({ unicodeWordBoundaries: true });

profanity.exists('vehículo horario');
// false (does not match on "culo" inside "vehículo")

grawlix

By default this is set to @#$%&!:

profanity.censor('I like big butts and I cannot lie');
// I like big @#$%&! and I cannot lie

Setting this to ****, results in:

profanity.censor('I like big butts and I cannot lie');
// I like big **** and I cannot lie

grawlixChar

When specifying a CensorType other than CensorType.Word, this is the character used by the censor function.

By default this is set to *:

profanity.censor('I like big butts and I cannot lie', CensorType.AllVowels);
// I like big b*tts and I cannot lie

Setting this to $, results in:

profanity.censor('I like big butts and I cannot lie', CensorType.AllVowels);
// I like big b$tts and I cannot lie

Customize the word list

Add words:

profanity.addWords(['aardvark', 'zebra']);

Remove words:

profanity.removeWords(['butt', 'arse']);

Whitelist

The whitelist allows you to specify words that are always ignored by the profanity filter.

This can be useful if you want to enable partial word matching (wholeWord = false), so combined words are caught (e.g., arselicker), while specific words you add to the whitelist are ignored (e.g., arsenic).

Add words to the whitelist:

profanity.whitelist.addWords(['arsenic', 'buttress']);

Remove words from the whitelist:

profanity.whitelist.removeWords(['arsenic', 'buttress']);

Benchmarking ⏱️

To see how Profanity performs, check out our benchmark results.

Contributing 🤝

So you want to contribute to the Profanity project? Fantastic! Please read the Contribute doc to get started.

版本与发布记录

@2toad/profanity 共发布 26 个版本,首次发布于 2019/10/14;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.3.0latest2026/03/247 个月前
3.2.02025/09/131.1 年前
3.1.12024/12/281.9 年前
3.0.12024/10/232 年前
3.0.02024/09/192 年前
2.5.02024/09/142 年前

关于 @2toad/profanity 的常见问题

@2toad/profanity 是什么?

A multi-language profanity filter with full TypeScript support

如何安装 @2toad/profanity?

在项目目录运行 npm install @2toad/profanity,或使用 pnpm add / yarn add / bun add。

@2toad/profanity 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@2toad/profanity 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@2toad/profanity 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@2toad/profanity 可以商用吗?

@2toad/profanity 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@2toad/profanity 的下载量有多少?

根据 npm 官方下载统计,@2toad/profanity 在 2026/10/05 – 2026/11/03 这 30 天内被下载 413,379 次,日均约 13,779 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@2toad/profanity 还在维护吗?

最新版本 3.3.0 发布于 2026/03/24(7 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。