Last active
August 5, 2020 07:38
-
-
Save maximkott/f41918e83c46e8e8a4066d4793eccd31 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var IBAN_MASKS = { | |
'default': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS SSSS', | |
'AD': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'AL': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS', | |
'AT': 'SSSS SSSS SSSS SSSS SSSS', | |
'BA': 'SSSS SSSS SSSS SSSS SSSS', | |
'BE': 'SSSS SSSS SSSS SSSS', | |
'BG': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'CH': 'SSSS SSSS SSSS SSSS SSSS S', | |
'CY': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS', | |
'CZ': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'DE': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'DK': 'SSSS SSSS SSSS SSSS SS', | |
'EE': 'SSSS SSSS SSSS SSSS SSSS', | |
'ES': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'FI': 'SSSS SSSS SSSS SSSS SS', | |
'FO': 'SSSS SSSS SSSS SSSS SS', | |
'FR': 'SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'GB': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'GE': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'GG': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'GI': 'SSSS SSSS SSSS SSSS SSSS SSS', | |
'GL': 'SSSS SSSS SSSS SSSS SS', | |
'GR': 'SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'HR': 'SSSS SSSS SSSS SSSS SSSS S', | |
'HU': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS', | |
'IE': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'IS': 'SSSS SSSS SSSS SSSS SSSS SSSS SS', | |
'IT': 'SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'LI': 'SSSS SSSS SSSS SSSS SSSS S', | |
'LT': 'SSSS SSSS SSSS SSSS SSSS', | |
'LU': 'SSSS SSSS SSSS SSSS SSSS', | |
'LV': 'SSSS SSSS SSSS SSSS SSSS S', | |
'MC': 'SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'MD': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'ME': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'MK': 'SSSS SSSS SSSS SSSS SSS', | |
'MT': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'NL': 'SSSS SSSS SSSS SSSS SS', | |
'NO': 'SSSS SSSS SSSS SSS', | |
'PL': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS', | |
'PT': 'SSSS SSSS SSSS SSSS SSSS SSSS S', | |
'RO': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'RS': 'SSSS SSSS SSSS SSSS SSSS SS', | |
'SE': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'SI': 'SSSS SSSS SSSS SSSS SSS', | |
'SK': 'SSSS SSSS SSSS SSSS SSSS SSSS', | |
'SM': 'SSSS SSSS SSSS SSSS SSSS SSSS SSS', | |
'UA': 'SSSS SSSS SSSS SSSS SSSS SSSS SSSS S', | |
}; | |
var mask = IBAN_MASKS[country_code] || IBAN_MASKS['default']; | |
var placeholder = ' '; | |
var is_swiss_postfinance_number = country_code == 'CH' && (bic == 'POFICHBEXXX' || bic == '09000'); | |
if (is_swiss_postfinance_number) { | |
mask = '00-0000-0'; | |
if (value.length == 10) { | |
mask = '00-00000-0'; | |
} else if (value.length == 11) { | |
mask = '00-000000-0'; | |
} | |
placeholder = '-' | |
} | |
value = $dummyInput.mask(mask, { | |
translation: { | |
'S': { | |
pattern: /[0-9A-Za-z]/ | |
} | |
}, | |
placeholder: placeholder | |
}).val(); | |
return maskString(value, mask, { | |
'S': /[0-9A-Za-z]/, | |
'0': /[0-9]/, | |
}); | |
} | |
return value; | |
function maskString(value, mask, maskPatterns) { | |
value = value || '' | |
mask = mask || ''; | |
maskPatterns = maskPatterns || {}; | |
var maskedValue = ''; | |
// array representation of string under test | |
var valueParts = value.split(''); | |
// array representation of the mask | |
var maskParts = mask.split(''); | |
// as long as there are still characters left in | |
// the original string, one must try to mask them | |
while (valueParts.length > 0) { | |
// take the first character and remove | |
// it from the original string | |
var unmaskedChar = valueParts.shift(); | |
// as long as the character has not been masked | |
// one must try to find a masking character | |
while (unmaskedChar !== null) { | |
// take the first mask character from | |
// the mask string | |
var maskChar = maskParts.shift(); | |
// make sure the masking character exists | |
// otherwise this means that the original string | |
// exceeds the masking pattern length | |
if (maskChar !== undefined) { | |
// try to find a pattern for the particular | |
// mask character | |
var maskPattern = maskPatterns[maskChar.toUpperCase()]; | |
// if there is no pattern configured for | |
// this particular mask character, assume | |
// the mask character is a placeholder | |
// that must be added to the string | |
if (maskPattern !== undefined) { | |
var check = false; | |
// mask pattern can be either a function | |
if (typeof maskPattern === 'function') { | |
check = maskPattern(unmaskedChar); | |
} | |
// or a regex string | |
else if (maskPattern instanceof RegExp) { | |
check = maskPattern.test(unmaskedChar); | |
} | |
// if character has passed the mask check, | |
// it can bee added to the final masked value | |
if (check) { | |
maskedValue += unmaskedChar; | |
} | |
// otherwise one must put the pattern back into | |
// the array so the next character can try to | |
// pass the mask check | |
else { | |
maskParts.unshift(maskChar); | |
} | |
unmaskedChar = null; | |
} | |
// mask character is a placeholder / formatting value | |
// and must be added to the masked string | |
else { | |
maskedValue += maskChar; | |
} | |
} | |
// no masking character has been found, | |
// the original string is probably longer | |
// then the mask pattern and therefore | |
// the leftovers can be cut off | |
else { | |
// reset current character to continue the loop | |
unmaskedChar = null; | |
} | |
} | |
} | |
return maskedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment