unicodeEs6

This tag provides ES6 unicode escapes and decoding

Created by: hackvertor
Installed 1 times

Category: Convert

Created on: Sunday, September 22, 2024 at 12:17:44 PM

Updated on: Sunday, September 22, 2024 at 12:17:44 PM

This is a built in tag

Tag arguments
[]
Code
class unicodeEs6 {
  encode(input) {
     return input.split('').map(chr => "\\u{"+chr.codePointAt(0).toString(16)+"}").join('');
  }

  decode(input) {
    return input.replaceAll(/\\u[{]([0]*[a-fA-F0-9]{1,6})[}]/g,($0,hex) => String.fromCodePoint(parseInt(hex,16)));
  }

  matches(input) {
    return /^(?:\\u[{][0]*[a-fA-F0-9]{1,6}[}])+/.exec(input);
  }
}