Last active
April 15, 2024 20:43
-
-
Save maximkott/31dfdd37d4c0fee499de453faca17c83 to your computer and use it in GitHub Desktop.
Revisions
-
Maxim Kott revised this gist
Jan 10, 2023 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -100,7 +100,7 @@ export const maskString = ( }; // now you can do console.log(maskString("+41 767007070", "+YXX? YX XXX XX XX", { X: /[0-9]/, Y: /[1-9]/ })) // +41 76 700 70 70 console.log(maskString("+3800767007070", "+YXX? YX XXX XX XX", { X: /[0-9]/, Y: /[1-9]/ })) // +380 76 700 70 70 -
maximkott revised this gist
Aug 5, 2020 . 1 changed file with 34 additions and 14 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,52 @@ export const maskString = ( value: any, mask: string, maskPatterns: Record<string, RegExp | ((char: string) => boolean)> ) => { value = value || ''; mask = mask || ''; maskPatterns = maskPatterns || {}; let maskedValue = ''; // array representation of string under test const valueParts = value.split(''); // array representation of the mask const 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 let unmaskedChar = valueParts.shift(); // as long as the character has not been masked // one must try to find a mask while (unmaskedChar !== null) { // take the first mask character from // the mask string const maskChar = maskParts.shift(); const maskCharIsOptional = maskParts[0] === '?'; // remove '?' from the mask parts if (maskCharIsOptional) { 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 const maskPattern = maskPatterns[maskChar.toUpperCase()]; // if there is no pattern configured for // this particular mask character, assume // the mask character is a placeholder / formatting // value that must be added to the string if (maskPattern !== undefined) { let check = false; // mask pattern can be either a function if (typeof maskPattern === 'function') { @@ -51,20 +61,28 @@ function maskString(value, mask, maskPatterns) { // it can bee added to the final masked value if (check) { maskedValue += unmaskedChar; unmaskedChar = null; } // the character did not pass the mask check, // however, the mask was optional, we can skip it else if (!check && maskCharIsOptional) { } // 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; if (unmaskedChar === maskChar) { unmaskedChar = null; } } } // no masking character could be found, @@ -79,8 +97,10 @@ function maskString(value, mask, maskPatterns) { } return maskedValue; }; // now you can do maskString('+410767007070', '+YXX? YX XXX XX XX', { X: /0-9/, Y: /[1-9]/ }) // or maskString('+3800767007070', '+YXX? YX XXX XX XX', { X: /0-9/, Y: /[1-9]/ }) // returns +380 76 700 70 70 -
maximkott created this gist
Mar 21, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,86 @@ 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 mask 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 / formatting // value 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 could be 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; } // now you can do maskString('+410767007070', '+XX SX XXX XX XX', {X: /\d/, S: /[1-9]/}); // returns +41 76 700 70 70