textConvert is a lightweight, dependency-free TypeScript library for text conversion, validation, and analysis — including redact(), which masks PII embedded in free-form text before it hits a log or a database.
import { redact } from 'textconvert';
redact('Contact me at jordan@example.com or 555-123-4567');
// 'Contact me at jo**************** or 55**********'
npm install textconvert
// ES Module
import * as convert from 'textconvert';
// or CommonJS
const convert = require('textconvert');
| Function | Description |
|---|---|
camelCase(text) |
Convert to camelCase |
pascalCase(text) |
Convert to PascalCase |
snakeCase(text) |
Convert to snake_case |
kebabCase(text) |
Convert to kebab-case |
slugify(text) |
Convert to a URL-safe slug |
capitalize(text) |
Capitalize only the first letter |
titleCase(text) |
Capitalize the first letter of every word |
clear(text) |
Remove punctuation from text |
count(text, countNumbers?) |
Count letters (optionally including numbers) |
countWords(text) |
Count words |
countSentences(text) |
Count sentences |
reverse(text) |
Reverse a string |
spread(text, clear?) |
Split a string into an array of characters |
truncate(text, maxLength, options?) |
Shorten text to a max length, with an ellipsis |
maskText(text, options?) |
Partially mask a string for display |
redact(text, options?) |
Mask PII/secrets embedded in text |
getTextStats(text, wordsPerMinute?) |
Full text statistics (counts, averages, reading time) |
detectLanguage(text, minLength?, options?) |
Detect the language of a piece of text |
numbersToWords(number) |
Convert a number under 100 million to English words |
isEmail(text) |
Validate an email address |
isUrl(text) |
Validate a URL |
isPhoneNumber(text) |
Validate a phone number |
extractEmails(text) |
Extract all email addresses found in a block of text |
extractUrls(text) |
Extract all URLs found in a block of text |
See docs/API.md for full parameter, return type, and edge-case details on every function, or browse the auto-generated API reference site.
import { redact, camelCase, count, isEmail, isUrl, isPhoneNumber } from 'textconvert';
redact('Contact me at jordan@example.com or 555-123-4567');
// 'Contact me at jo**************** or 55**********'
camelCase('hello world'); // 'helloWorld'
count('Hello, world!'); // 10
isEmail('user@example.com'); // true
isUrl('https://example.com/path?query=123'); // true
isPhoneNumber('+1-202-555-0173'); // true
See more usage examples in docs/RECIPES.md.
// Detect language with options
import { detectLanguage, Language } from 'textconvert';
const result = detectLanguage('Hola mundo', 4, { maxCharsToAnalyze: 300 });
if (result.language === Language.Spanish) {
console.log('Spanish detected!');
}
// Get detailed text statistics
const stats = convert.getTextStats('Hello world. This is a test.');
console.log(stats.wordCount, stats.readingTimeFormatted);
// Convert numbers to words
console.log(convert.numbersToWords(987654));
// "nine hundred eighty-seven thousand six hundred and fifty-four"
Contributions are welcome! Please see CONTRIBUTING.md for guidelines and workflow.
If you are adding a new function, follow the step-by-step instructions in docs/ADDING_FUNCTION.md.
See CHANGELOG.md for release history and updates.
This project is licensed under the MIT License.
Nicolas Alkhoury 💬 🐛 💼 💻 🔣 🎨 📖 🤔 🚇 🚧 🧑🏫 📦 🔌 📆 🔬 👀 ⚠️ 🔧 📓 |
Madesh 💻 📖 💡 ⚠️ |
Kuba 💻 📖 💡 |
This project follows the all-contributors specification. Contributions of any kind welcome!