str-mask
Mask text by pattern
Note: This module works in browsers and Node.js >= 4.0
Installation
npm install str-mask
Usage
const mask = ; // { model: '9211234567',// viewValue: '+7 (921) 123-45-67',// isValid: true,// cursor: 18 } // { model: '92112',// viewValue: '+7 (921) 12',// isValid: false,// cursor: 11 } // { model: '92112',// viewValue: '+7 (921) 12_-__-__',// isValid: false,// cursor: 11 }
Special mask characters
Character | Description |
---|---|
9 |
Any numbers |
A |
Any alphanumeric character |
* |
Any letter |
U |
Any letter (All lower case character will be mapped to uppercase) |
L |
Any letter (All upper case character will be mapped to lowercase) |
\\ |
Escape character, used to escape any of the special formatting characters. |
API
mask(value, [params])
-
value (
String
) - Masked value (any values convert to string) -
params (
Object
)- mask (
String
) - mask - filler (
String
) - string for empty spaces. You can use one symbol pattern_
or string likes maskDD/MM/DDDD
- escChar (
String
) - char for escaping. Default:\\
- chars (
Object
) - map of special chars. You can add extra chars or remove default if set<char>: null
. Default:
- mask (
'9': /\d/'A': /[a-zA-Z]/'*': /[a-zA-Z0-9]/ { letter = letter; if /[a-zA-Z0-9]/ return letter; } { letter = letter; if /[a-zA-Z0-9]/ return letter; }
--
mask.curry([params])
-
params - see mask params.
-
return (
Function
) - function, which gets value and returns result of masking
phoneMask = mask // { model: '92112',// viewValue: '+7 (921) 12_-__-__',// isValid: false,// cursor: 11 } // { model: '92112',// viewValue: '+7 (921) 123-45-__',// isValid: false,// cursor: 16 }
Tests
npm installnpm test