v1
- by bvaughn 9/27/202100
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const strings = [
  "🟩 Container",
  "Lorem ipsum dolor sit amet",
  "consectetur adipiscing elit",
  "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
  "Ut enim ad minim veniam",
  "quis nostrud 💜exercitation ullamco"
];
delete caserun single casemove downdrag and drop case


ready



function utfDecodeString(array) {
  let string = '';
  for (let i = 0; i < array.length; i++) {
    const char = array[i];
    string += String.fromCodePoint(char);
  }
  return string;
}

function utfEncodeString(string) {
  const encoded = new Array(string.length);
  for (let i = 0; i < string.length; i++) {
    encoded[i] = string.codePointAt(i);
  }
  return encoded;
}

for (let i = 0; i < strings.length; i++) {
  const string = strings[i];
  const encoded = utfEncodeString(string);
  const decoded = utfDecodeString(encoded);
}
delete caserun single casemove upmove downdrag and drop case


ready



// Credit to https://stackoverflow.com/questions/4877326/how-can-i-tell-if-a-string-contains-multibyte-characters-in-javascript

function surrogatePairToCodePoint(charCode1, charCode2) {
  return ((charCode1 & 0x3FF) << 10) + (charCode2 & 0x3FF) + 0x10000;
}

// Read string in character by character and create an array of code points
function utfEncodeString(string) {
  const codePoints = [];
  let i = 0;
  let charCode;
  while (i < string.length) {
    charCode = string.charCodeAt(i);
    if ((charCode & 0xF800) == 0xD800) {
      codePoints.push(surrogatePairToCodePoint(charCode, string.charCodeAt(++i)));
    } else {
      codePoints.push(charCode);
    }
    ++i;
  }
  return codePoints;
}

function utfDecodeString(array) {
	
  let string = '';
  for (let i = 0; i < array.length; i++) {
    const char = array[i];
    string += String.fromCodePoint(char);
  }
  
  return string;
}

for (let i = 0; i < strings.length; i++) {
  const string = strings[i];
  const encoded = utfEncodeString(string);
  const decoded = utfDecodeString(encoded);
}
delete caserun single casemove updrag and drop case


ready



const encodedStringCache = new Map();

// Credit to https://stackoverflow.com/questions/4877326/how-can-i-tell-if-a-string-contains-multibyte-characters-in-javascript

function surrogatePairToCodePoint(charCode1, charCode2) {
  return ((charCode1 & 0x3FF) << 10) + (charCode2 & 0x3FF) + 0x10000;
}

// Read string in character by character and create an array of code points
function utfEncodeString(string) {
  const cached = encodedStringCache.get(string);
  if (cached !== undefined) {
    return cached;
  }
  
  const codePoints = [];
  let i = 0;
  let charCode;
  while (i < string.length) {
    charCode = string.charCodeAt(i);
    if ((charCode & 0xF800) == 0xD800) {
      codePoints.push(surrogatePairToCodePoint(charCode, string.charCodeAt(++i)));
    } else {
      codePoints.push(charCode);
    }
    ++i;
  }
  
  encodedStringCache.set(string, codePoints);

  return codePoints;
}

function utfDecodeString(array) {
  let string = '';
  for (let i = 0; i < array.length; i++) {
    const char = array[i];
    string += String.fromCodePoint(char);
  }
  return string;
}

for (let i = 0; i < strings.length; i++) {
  const string = strings[i];
  const encoded = utfEncodeString(string);
  const decoded = utfDecodeString(encoded);
}
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN