Last active
March 27, 2020 08:25
-
-
Save apostololeg/784c831d58e19664c0dde3482f92f267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mask('**.**.****', '17.02'); | |
// Mask('+7 (***) ***-**-**', '9854554354123283293'); | |
// Mask('+7 (***) ***-**-**', '9854554354123283293', { isStrict: true }); | |
export function mask(maskStr, str, { isStrict = false } = {}) { | |
let decorSymbCount = 0; | |
let maskedStr = ''; | |
for (let i = 0; i < maskStr.length && str[i - decorSymbCount]; i++) { | |
const m = maskStr[i]; | |
const v = str[i - decorSymbCount]; | |
if (m !== '*') { | |
maskedStr += m; | |
if (str[i] !== m) decorSymbCount++; | |
continue; | |
} | |
maskedStr += v; | |
} | |
if (isStrict) return maskedStr; | |
const starsCount = maskStr.split('').filter(m => m === '*').length; | |
return `${maskedStr}${str.slice(starsCount)}`; | |
} | |
// unmask('**.**.****', '16.03.2020'); | |
export function unmask(maskStr, str) { | |
let unmaskedStr = ''; | |
for (let i = 0; i < maskStr.length && str[i]; i++) { | |
if (maskStr[i] === '*') unmaskedStr += str[i]; | |
} | |
return unmaskedStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment