import { IFormatterParserFn } from 'angular-formatter-parser/struct/formatter-parser-function' ; export function maskString ( mask , maskPatterns ) { return ( value ) => { value = ( typeof value === 'string' || value instanceof String ) ? value : '' ; mask = mask || '' ; maskPatterns = maskPatterns || { } ; let maskedValue = '' ; const valueParts = value . split ( '' ) ; const maskParts = mask . split ( '' ) ; while ( valueParts . length > 0 ) { let unmaskedChar = valueParts . shift ( ) ; while ( unmaskedChar !== null ) { const maskChar = maskParts . shift ( ) ; if ( maskChar !== undefined ) { const maskPattern = maskPatterns [ maskChar . toUpperCase ( ) ] ; if ( maskPattern !== undefined ) { let check = false ; if ( typeof maskPattern === 'function' ) { check = maskPattern ( unmaskedChar ) ; } else if ( maskPattern instanceof RegExp ) { check = maskPattern . test ( unmaskedChar ) ; } if ( check ) { maskedValue += unmaskedChar ; } else { maskParts . unshift ( maskChar ) ; } unmaskedChar = null ; } else { maskedValue += maskChar ; } } else { unmaskedChar = null ; } } } return { name : "maskString" , result : maskedValue , previous : value } ; } ; }