extensions/amp-inputmask/0.1/inputmask-custom-alias.js
/**
 * Installs an alias used by amp-inputmask that fixes a problem where
 * the user copy-pastes a value into a field with an inputmask that has a
 * literal prefix. e.g.
 * <paste> +1(234)567-8910 -> +1(123)456-7891
 * @param {!Object} Inputmask
 */
Installs an alias used by amp-inputmask that fixes a problem where
the user copy-pastes a value into a field with an inputmask that has a
literal prefix.
Installs an alias used by amp - inputmask that fixes a problem where the user copy - pastes a value into a field with an inputmask that has a literal prefix .
function factory(Inputmask) {
  Inputmask.extendAliases(dict({
    'custom': {
      'prefixes': [],
      'mask': function(opts) {
        const customMask = opts['customMask'];
        opts['prefixes'] = getPrefixSubsets(customMask);
        return customMask;
      },
      'onBeforeMask': function(value, opts) {
        const processedValue = value.replace(/^0{1,2}/, '').replace(/[\s]/g, '');
        const prefixes = opts['prefixes'];
        return removePrefix(processedValue, prefixes);
      },
    },
  }));
  const prefixRe = /^([^\*\[\]a\?9\\]+)[\*\[\]a\?9\\]/i;
  function getPrefixSubsets(mask) {
    const masks = (typeof mask == 'string' ? [mask] : mask);
    const prefixes = {};
    for (let i = 0; i < masks.length; i++) {
      const prefix = getMaskPrefix(masks[i]);
      if (prefix.length == 0) {
        continue;
      }
      const stack = [prefix];
      while (stack.length) {
        const prefix = stack.pop();
        prefixes[prefix] = true;
        if (prefix.length > 1) {
          stack.push(prefix.slice(1));
          stack.push(prefix.slice(0, -1));
        }
      }
    }
    return Object.keys(prefixes);
  }
  function getMaskPrefix(mask) {
    const processedMask = mask.replace(/[\s]/g, '');
    const match = prefixRe.exec(processedMask);
    const prefix = (match && match[1]) || '';
    return prefix;
  }
  function removePrefix(value, prefixes) {
    const longestPrefix = prefixes
        .filter(prefix => value.indexOf(prefix) == 0)
        .sort((a, b) => b.length - a.length)[0];
    if (longestPrefix) {
      return value.slice(longestPrefix.length);
    } else {
      return value;
    }
  }
}
function factory ( Inputmask ) { Inputmask . extendAliases ( dict ( { 'custom' : { 'prefixes' : [ ] , 'mask' : function ( opts ) { const customMask = opts [ 'customMask' ] ; opts [ 'prefixes' ] = getPrefixSubsets ( customMask ) ; return customMask ; } , 'onBeforeMask' : function ( value , opts ) { const processedValue = value . replace ( / ^0{1,2} / , '' ) . replace ( / [\s] / g , '' ) ; const prefixes = opts [ 'prefixes' ] ; return removePrefix ( processedValue , prefixes ) ; } , } , } ) ) ; const prefixRe = / ^([^\*\[\]a\?9\\]+)[\*\[\]a\?9\\] / i ; function getPrefixSubsets ( mask ) { const masks = ( typeof mask == 'string' ? [ mask ] : mask ) ; const prefixes = { } ; for ( let i = 0 ; i < masks . length ; i ++ ) { const prefix = getMaskPrefix ( masks [ i ] ) ; if ( prefix . length == 0 ) { continue ; } const stack = [ prefix ] ; while ( stack . length ) { const prefix = stack . pop ( ) ; prefixes [ prefix ] = true ; if ( prefix . length > 1 ) { stack . push ( prefix . slice ( 1 ) ) ; stack . push ( prefix . slice ( 0 , - 1 ) ) ; } } } return Object . keys ( prefixes ) ; } function getMaskPrefix ( mask ) { const processedMask = mask . replace ( / [\s] / g , '' ) ; const match = prefixRe . exec ( processedMask ) ; const prefix = ( match && match [ 1 ] ) || '' ; return prefix ; } function removePrefix ( value , prefixes ) { const longestPrefix = prefixes . filter ( prefix => value . indexOf ( prefix ) == 0 ) . sort ( ( a , b ) => b . length - a . length ) [ 0 ] ; if ( longestPrefix ) { return value . slice ( longestPrefix . length ) ; } else { return value ; } } }
Installs an alias used by amp-inputmask that fixes a problem where
 the user copy-pastes a value into a field with an inputmask that has a
 literal prefix.
Installs an alias used by amp - inputmask that fixes a problem where the user copy - pastes a value into a field with an inputmask that has a literal prefix .
/**
       * @param {!JsonObject} opts
       * @return {!Object}
       */ /**
       * @param {string} value
       * @param {!JsonObject} opts
       * @return {string}
       */ /**
   * A prefix is defined as non-mask characters at the beginning of a mask
   * definition.
   */ /**
   * Gets a map of substrings of the mask prefixes.
   * e.g. +1(000)000-0000" -> ["+1(", "+1", "+", "1(", ...]
   * @param {!Array<string>|string} mask
   * @return {!Array<string>}
   */ /**
   * Gets any literal non-variable mask characters from the
   * beginning of the mask string
   * e.g. "+1(000)000-0000" -> "+1("
   * @param {string} mask
   * @return {string}
   */ /**
   * Remove a mask prefix from the input value
   * @param {string} value
   * @param {!Array<string>} prefixes
   * @return {string}
   */