data-mask
A small string masking library in javascript.
INSTALLATION
With bower:
bower install --save data-mask
With npm:
npm install --save data-mask
USAGE
Use it by using DataMasker instance with plain text:
var dataMasker = new DataMasker('lorem ipsum', options);
var output = '';
output = dataMasker.maskLeft(2);
output = dataMasker.maskRight(2);
output = dataMasker.maskRandom(2);
output = dataMasker.maskLeft(2, ' ', '#');
output = dataMasker.maskRight(2, ' ', '@');
output = dataMasker.maskRandom(2, ' ', '-');
output = dataMasker.maskLeft(2, 4, '?');
output = dataMasker.maskLeft(2, ' ', '?', beforeMaskFn, afterMaskFn);
output = dataMasker.mask(2, ' ', '#', 1);
output = DataMasker.maskLeft('lorem ipsum', 2, ' ', '#');
output = DataMasker.maskRight('lorem ipsum', 2, ' ', '@');
output = DataMasker.maskRandom('lorem ipsum', 2, ' ', '-');
output = DataMasker.maskLeft('lorem ipsum', 2, 4, '?');
OPTIONS
Options for constructor or method params:
Option | Description |
---|
maskChar | An one-length string used for mask (Default * ) |
deliminator | A deliminator string or integer for fixed chunks (Default ' ' or min 1 ) |
direction | Mask positions. Left, right or random chars "1, -1, 0" (Default 1 ) |
range | Mask character count or percentage for token (0 < range < 1), range=0 is random character count. |
beforeMask | Callback function on before mask for each token. fn(token , range , maskChar , deliminator ), a string token expected. Return false for prevent masking. |
aftermask | Callback function on after mask for each token. fn(token , range , maskChar , deliminator ), a string token expected. Return false for exclude token. |
beforeMask and afterMask examples:
function beforeMask(token, range, maskChar, deliminator) {
if(token=='lorem'){
return 'LOREM';
}
else if(token == 'ipsum'){
return false;
}
else{
return token;
}
}
function afterMask(token, range, maskChar, deliminator) {
if(token === maskChar){
return false;
}
else{
return token;
}
}
LICENSE
Copyright (c) 2016 Soner Çökmen
Licensed under the MIT license.