I got tired of hand-rolling PII redaction for logs, so I built it into a small text utility package
Every project eventually hits the same problem: a user pastes their email into a support ticket, or a phone number ends up in a log line, and now you're writing a one-off regex to mask it before it hits storage. You reach for a library — and the options are either a full NLP-based PII detector (Presidio, and friends) that's way more than you need, or a paid API. There's no small, dependency-free…
One common problem many developers encounter is accidentally exposing sensitive information, like email or phone numbers, in logs or support tickets. Instead of writing a one-off regular expression, they often rely on a full-fledged PII detector or a paid API. However, there's no simple, dependency-free solution that just masks the obvious information.
To address this need, I added a redact function to textConvert, a small TypeScript text-utility library I maintain. The import statement is simple: "import { redact } from 'textconvert';"
The redact function works by leveraging two existing functions in the library: extractEmails and maskText. extractEmails scans the provided text for email addresses and phone numbers, while maskText partially masks a given string, displaying only a specified number of characters at the end.
For email addresses, the redact function is straightforward: "redact('Contact me at jordan@example.com or 555-123-4567'); // Contact me at jo**************** or 55**********"
When redact encounters email and phone numbers, it uses extractEmails to find them, then masks each one with maskText. You can also specify which types of PII to target or customize the mask character: "redact('Email: jordan@example.com', { types: ['email'] }); // Email: jo****************"
The textConvert library offers a wide range of text utilities, including case conversion, validation, and text analysis. However, redact is a valuable addition for smaller projects that need just a handful of text utilities, including PII masking, without the overhead of multiple installs and TypeScript definition files.
To install textConvert, run "npm install textconvert". The package is MIT-licensed and open to contributions. Several issues are tagged as "good first issue" if you want to contribute. Feedback is welcome, especially regarding the new redact function.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.