Source: app/util/charToHex.js

/**
 * Copyright (C) 2007  Richard Ishida ishida@w3.org
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version as long as you point to
 * http://rishida.net/ in your code.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. http://www.gnu.org/licenses/gpl.html
 *
 * Source: https://r12a.github.io/apps/conversion/conversionfunctions.js
 */
function dec2hex(textString) {
  return (textString + 0).toString(16);
}

/**
 * This snippet parse character strings into hexadecimal
 *
 * Used in the detectEmoji method
 *
 * @param textString
 * @param type
 * @returns {string}
 */
export default function(textString) {
  // converts a string of characters to code points, separated by space
  // textString: string, the string to convert
  let haut = 0;
  let CPstring = '';
  let afterEscape = false;
  for (let i = 0; i < textString.length; i++) {
    const b = textString.charCodeAt(i);
    if (b < 0 || b > 0xffff) {
      CPstring += `Error in convertChar2CP: byte out of range ${dec2hex(b)}!`;
    }
    if (haut !== 0) {
      if (b >= 0xdc00 && b <= 0xdfff) {
        if (afterEscape) {
          CPstring += ' ';
        }
        // eslint-disable-next-line no-bitwise
        CPstring += dec2hex(0x10000 + ((haut - 0xd800) << 10) + (b - 0xdc00));
        haut = 0;
      } else {
        CPstring += `Error in convertChar2CP: surrogate out of range ${dec2hex(haut)}!`;
        haut = 0;
      }
    }
    if (b >= 0xd800 && b <= 0xdbff) {
      haut = b;
    } else if (b <= 127) {
      CPstring += textString.charAt(i);
      afterEscape = false;
    }
  }
  return CPstring;
}