function encode(str) {
  const length = str.length;
  let resPos = -1;

  // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
  // takes up the equivalent space of 3 UTF-8 characters to encode it properly. However, Array's
  // have an auto expanding length and 1.5x should be just the right balance for most uses.
  const resultArray = new Uint8Array(length * 3);
  let i = 0;

  while (i !== length) {
    let point = str.charCodeAt(i);
    i += 1;

    if (point >= 0xd800 && point <= 0xdbff) {
      if (i === length) {
        resultArray[(resPos += 1)] = 0xef; // 0b11101111
        resultArray[(resPos += 1)] = 0xbf; // 0b10111111
        resultArray[(resPos += 1)] = 0xbd; // 0b10111101
        break;
      }
      // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
      const nextCode = str.charCodeAt(i);

      if (nextCode >= 0xdc00 && nextCode <= 0xdfff) {
        point = (point - 0xd800) * 0x400 + nextCode - 0xdc00 + 0x10000;
        i += 1;

        if (point > 0xffff) {
          // point > 65535
          resultArray[(resPos += 1)] = (0x1e /*0b11110*/ << 3) | (point >>> 18);
          resultArray[(resPos += 1)] =
            (0x2 /*0b10*/ << 6) | ((point >>> 12) & 0x3f); // 0b00111111
          resultArray[(resPos += 1)] =
            (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f); // 0b00111111
          resultArray[(resPos += 1)] = (0x2 /*0b10*/ << 6) | (point & 0x3f); // 0b00111111
          continue;
        }
      } else {
        resultArray[(resPos += 1)] = 0xef; // 0b11101111
        resultArray[(resPos += 1)] = 0xbf; // 0b10111111
        resultArray[(resPos += 1)] = 0xbd; // 0b10111101
        continue;
      }
    }

    if (point <= 0x007f) {
      resultArray[(resPos += 1)] = (0x0 /*0b0*/ << 7) | point;
    } else if (point <= 0x07ff) {
      resultArray[(resPos += 1)] = (0x6 /*0b110*/ << 5) | (point >>> 6);
      resultArray[(resPos += 1)] = (0x2 /*0b10*/ << 6) | (point & 0x3f); // 0b00111111
    } else {
      resultArray[(resPos += 1)] = (0xe /*0b1110*/ << 4) | (point >>> 12);
      resultArray[(resPos += 1)] = (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f); // 0b00111111
      resultArray[(resPos += 1)] = (0x2 /*0b10*/ << 6) | (point & 0x3f); // 0b00111111
    }
  }

  return resultArray.subarray(0, resPos + 1);
}
/**
 * Encode non regular ASCII characters in string to array of chars
 *
 * @private
 * @param {string} str Encode a string into a Uint8 array
 * @returns {Uint8Array}
 */
Encode non regular ASCII characters in string to array of chars
@private
@param {string} str Encode a string into a Uint8 array
@returns {Uint8Array}
Encode non regular ASCII characters in string to array of chars @private @param { string } str Encode a string into a Uint8 array @returns { Uint8Array }
function encode(str) {
  const length = str.length;
  let resPos = -1;
  const resultArray = new Uint8Array(length * 3);
  let i = 0;
  while (i !== length) {
    let point = str.charCodeAt(i);
    i += 1;
    if (point >= 0xd800 && point <= 0xdbff) {
      if (i === length) {
        resultArray[(resPos += 1)] = 0xef; 
        resultArray[(resPos += 1)] = 0xbf; 
        resultArray[(resPos += 1)] = 0xbd; 
        break;
      }
      const nextCode = str.charCodeAt(i);
      if (nextCode >= 0xdc00 && nextCode <= 0xdfff) {
        point = (point - 0xd800) * 0x400 + nextCode - 0xdc00 + 0x10000;
        i += 1;
        if (point > 0xffff) {
          resultArray[(resPos += 1)] = (0x1e  << 3) | (point >>> 18);
          resultArray[(resPos += 1)] =
            (0x2  << 6) | ((point >>> 12) & 0x3f); 
          resultArray[(resPos += 1)] =
            (0x2  << 6) | ((point >>> 6) & 0x3f); 
          resultArray[(resPos += 1)] = (0x2  << 6) | (point & 0x3f); 
          continue;
        }
      } else {
        resultArray[(resPos += 1)] = 0xef; 
        resultArray[(resPos += 1)] = 0xbf; 
        resultArray[(resPos += 1)] = 0xbd; 
        continue;
      }
    }
    if (point <= 0x007f) {
      resultArray[(resPos += 1)] = (0x0  << 7) | point;
    } else if (point <= 0x07ff) {
      resultArray[(resPos += 1)] = (0x6  << 5) | (point >>> 6);
      resultArray[(resPos += 1)] = (0x2  << 6) | (point & 0x3f); 
    } else {
      resultArray[(resPos += 1)] = (0xe  << 4) | (point >>> 12);
      resultArray[(resPos += 1)] = (0x2  << 6) | ((point >>> 6) & 0x3f); 
      resultArray[(resPos += 1)] = (0x2  << 6) | (point & 0x3f); 
    }
  }
  return resultArray.subarray(0, resPos + 1);
}
function encode ( str ) { const length = str . length ; let resPos = - 1 ; const resultArray = new Uint8Array ( length * 3 ) ; let i = 0 ; while ( i !== length ) { let point = str . charCodeAt ( i ) ; i += 1 ; if ( point >= 0xd800 && point <= 0xdbff ) { if ( i === length ) { resultArray [ ( resPos += 1 ) ] = 0xef ; resultArray [ ( resPos += 1 ) ] = 0xbf ; resultArray [ ( resPos += 1 ) ] = 0xbd ; break ; } const nextCode = str . charCodeAt ( i ) ; if ( nextCode >= 0xdc00 && nextCode <= 0xdfff ) { point = ( point - 0xd800 ) * 0x400 + nextCode - 0xdc00 + 0x10000 ; i += 1 ; if ( point > 0xffff ) { resultArray [ ( resPos += 1 ) ] = ( 0x1e << 3 ) | ( point >>> 18 ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( ( point >>> 12 ) & 0x3f ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( ( point >>> 6 ) & 0x3f ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( point & 0x3f ) ; continue ; } } else { resultArray [ ( resPos += 1 ) ] = 0xef ; resultArray [ ( resPos += 1 ) ] = 0xbf ; resultArray [ ( resPos += 1 ) ] = 0xbd ; continue ; } } if ( point <= 0x007f ) { resultArray [ ( resPos += 1 ) ] = ( 0x0 << 7 ) | point ; } else if ( point <= 0x07ff ) { resultArray [ ( resPos += 1 ) ] = ( 0x6 << 5 ) | ( point >>> 6 ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( point & 0x3f ) ; } else { resultArray [ ( resPos += 1 ) ] = ( 0xe << 4 ) | ( point >>> 12 ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( ( point >>> 6 ) & 0x3f ) ; resultArray [ ( resPos += 1 ) ] = ( 0x2 << 6 ) | ( point & 0x3f ) ; } } return resultArray . subarray ( 0 , resPos + 1 ) ; }
Encode non regular ASCII characters in string to array of chars
 @private
 @param {string} str Encode a string into a Uint8 array
 @returns {Uint8Array}
Encode non regular ASCII characters in string to array of chars @private @param { string } str Encode a string into a Uint8 array @returns { Uint8Array }
// The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16 // takes up the equivalent space of 3 UTF-8 characters to encode it properly. However, Array's // have an auto expanding length and 1.5x should be just the right balance for most uses. // 0b11101111 // 0b10111111 // 0b10111101 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae // point > 65535 /*0b11110*/ /*0b10*/ // 0b00111111 /*0b10*/ // 0b00111111 /*0b10*/ // 0b00111111 // 0b11101111 // 0b10111111 // 0b10111101 /*0b0*/ /*0b110*/ /*0b10*/ // 0b00111111 /*0b1110*/ /*0b10*/ // 0b00111111 /*0b10*/ // 0b00111111