@alexiom/mask-data-lite
v1.0.0
Published
A lightweight JavaScript/TypeScript utility for masking and desensitizing sensitive data like phone numbers, emails, ID cards, and bank cards.
Maintainers
Readme
mask-data-lite
A lightweight JavaScript/TypeScript utility for masking and desensitizing sensitive data like phone numbers, emails, ID cards, and bank cards.
Features
- Phone Number Masking - Mask phone numbers with customizable format
- Email Masking - Mask email addresses while preserving domain
- ID Card Masking - Mask Chinese ID card numbers
- Bank Card Masking - Mask bank card numbers
- Auto-detection - Automatically detect data type and apply appropriate masking
- Customizable - Configure masking characters, prefix/suffix length
- TypeScript Support - Full TypeScript type definitions included
- Zero Dependencies - Lightweight with no external dependencies
Installation
npm install mask-data-liteUsage
ES Modules
import { maskPhone, maskEmail, maskIdCard, maskBankCard, mask } from 'mask-data-lite';CommonJS
const { maskPhone, maskEmail, maskIdCard, maskBankCard, mask } = require('mask-data-lite');API
maskPhone(phone, options?)
Mask a phone number.
maskPhone('13812345678')
// '138****5678'
maskPhone('13812345678', { keepStart: 3, keepEnd: 2 })
// '138*****78'
maskPhone('13812345678', { maskChar: 'X' })
// '138XXXX5678'Options:
keepStart(default: 3) - Number of digits to keep at the startkeepEnd(default: 4) - Number of digits to keep at the endmaskChar(default: '*') - Character to use for masking
maskEmail(email, options?)
Mask an email address.
maskEmail('[email protected]')
// 'a***@gmail.com'
maskEmail('[email protected]', { keepStart: 2, keepEnd: 1 })
// 'b**[email protected]'
maskEmail('[email protected]', { maskDomain: true })
// 'c***@***'Options:
keepStart(default: 1) - Number of characters to keep at the start of usernamekeepEnd(default: 0) - Number of characters to keep at the end of usernamemaskChar(default: '*') - Character to use for maskingmaskDomain(default: false) - Whether to mask the domain part
maskIdCard(idCard, options?)
Mask an ID card number (Chinese format).
maskIdCard('110101199001011234')
// '110***********1234'
maskIdCard('110101199001011234', { keepStart: 6, keepEnd: 4 })
// '110101******1234'Options:
keepStart(default: 3) - Number of digits to keep at the startkeepEnd(default: 4) - Number of digits to keep at the endmaskChar(default: '*') - Character to use for masking
maskBankCard(cardNumber, options?)
Mask a bank card number.
maskBankCard('6222021234567890123')
// '6222***********0123'
maskBankCard('6222021234567890123', { keepStart: 6, keepEnd: 4 })
// '622202*******0123'Options:
keepStart(default: 4) - Number of digits to keep at the startkeepEnd(default: 4) - Number of digits to keep at the endmaskChar(default: '*') - Character to use for masking
maskString(str, options?)
Mask any string with custom options.
maskString('Hello World')
// 'H*********d'
maskString('Hello World', { keepStart: 2, keepEnd: 2 })
// 'He*******ld'Options:
keepStart(default: 1) - Number of characters to keep at the startkeepEnd(default: 1) - Number of characters to keep at the endmaskChar(default: '*') - Character to use for masking
mask(data, options?)
Auto-detect data type and apply appropriate masking.
mask('13812345678') // '138****5678' (phone)
mask('[email protected]') // 'a***@gmail.com' (email)
mask('110101199001011234') // '110***********1234' (ID card)
mask('6222021234567890123') // '6222***********0123' (bank card)
mask('Hello World') // 'H*********d' (generic string)TypeScript
This package includes TypeScript type definitions out of the box.
import { maskPhone, MaskPhoneOptions } from 'mask-data-lite';
const options: MaskPhoneOptions = {
keepStart: 3,
keepEnd: 4,
maskChar: '*'
};
const masked = maskPhone('13812345678', options);Author
Created with ❤️ by the team behind CPM Calculator - a free tool for calculating and optimizing your digital advertising costs. Check it out for more useful utilities!
License
ISC
