Skip to content

Instantly share code, notes, and snippets.

@maximkott
Last active April 15, 2024 20:43
Show Gist options
  • Save maximkott/31dfdd37d4c0fee499de453faca17c83 to your computer and use it in GitHub Desktop.
Save maximkott/31dfdd37d4c0fee499de453faca17c83 to your computer and use it in GitHub Desktop.

Revisions

  1. Maxim Kott revised this gist Jan 10, 2023. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions mask.js
    Original file line number Diff line number Diff line change
    @@ -100,7 +100,7 @@ export const maskString = (
    };

    // 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
    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
  2. maximkott revised this gist Aug 5, 2020. 1 changed file with 34 additions and 14 deletions.
    48 changes: 34 additions & 14 deletions mask.js
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,52 @@
    function maskString(value, mask, maskPatterns) {
    value = value || ''
    export const maskString = (
    value: any,
    mask: string,
    maskPatterns: Record<string, RegExp | ((char: string) => boolean)>
    ) => {
    value = value || '';
    mask = mask || '';
    maskPatterns = maskPatterns || {};

    var maskedValue = '';
    let maskedValue = '';
    // array representation of string under test
    var valueParts = value.split('');
    const valueParts = value.split('');
    // array representation of the mask
    var maskParts = mask.split('');
    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
    var unmaskedChar = valueParts.shift()
    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
    var maskChar = maskParts.shift();
    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
    var maskPattern = maskPatterns[maskChar.toUpperCase()];
    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) {
    var check = false;
    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;
    }

    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', '+XX SX XXX XX XX', {X: /\d/, S: /[1-9]/});
    // returns +41 76 700 70 70
    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
  3. maximkott created this gist Mar 21, 2017.
    86 changes: 86 additions & 0 deletions mask.js
    Original 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