npm install obscure-stringconst { obscureString } = require('obscure-string');
// Simple masking
obscureString('mysecretkey');
// → 'mys*****key'
// Custom options
obscureString('john.doe@example.com', {
prefixLength: 2,
suffixLength: 4,
maskChar: '#'
});
// → 'jo##############.com'obscureString('support@company.com', { preset: 'email' });
// → 'su*****@company.com'obscureString('4111-1111-1111-1111', { preset: 'creditCard' });
// → '************1111'obscureString('(555) 123-4567', { preset: 'phone' });
// → '******4567'obscureString('sensitive', { fullMask: true });
// → '*********'const { obscureStringBatch } = require('obscure-string');
obscureStringBatch(['secret1', 'secret2', 'secret3']);
// → ['sec**t1', 'sec**t2', 'sec**t3']# Install globally
npm install -g obscure-string
# Use directly
obscure-string "mysecret"
# → mys***et
# With options
obscure-string "john@example.com" --preset email
# → jo**@example.com
# Show help
obscure-string --helpconsole.log('API Key:', obscureString(apiKey, {
prefixLength: 7,
suffixLength: 4
}));const sanitize = (user) => ({
...user,
email: obscureString(user.email, { preset: 'email' }),
phone: obscureString(user.phone, { preset: 'phone' }),
});const { getMaskInfo } = require('obscure-string');
const smartMask = (value) => {
const info = getMaskInfo(value);
return info.willBeMasked
? obscureString(value)
: obscureString(value, { fullMask: true });
};| Option | Type | Default | Description |
|---|---|---|---|
maskChar |
string | '*' |
Character to use for masking |
prefixLength |
number | 3 |
Visible chars at start |
suffixLength |
number | 3 |
Visible chars at end |
minMaskLength |
number | 0 |
Min masked chars required |
fullMask |
boolean | false |
Mask entire string |
reverseMask |
boolean | false |
Show middle, hide edges |
percentage |
number | - | Mask percentage (0-100) |
maxLength |
number | 1000000 |
Max string length |
preset |
string | - | Use preset pattern |
import { obscureString, type ObscureStringOptions } from 'obscure-string';
const options: ObscureStringOptions = {
maskChar: '#',
preset: 'email'
};
const result = obscureString('test@example.com', options);v2.0 Features: ✨ Smart Presets | ⚡ 2-3x Faster | 🛡️ DoS Protection | 🌍 Unicode-Safe | 📦 Zero Dependencies