utf7

This encodes and decodes UTF-7 used in browsers and email parsers.

Created by: hackvertor
Installed 2 times

Category: Charsets

Created on: Tuesday, September 17, 2024 at 11:46:27 AM

Updated on: Monday, September 30, 2024 at 1:27:25 PM

This is a built in tag

Tag arguments
[
   {
      "type": "string",
      "help": "Provides a prefix",
      "defaultValue": "&"
   }
]
Code
class utf7 {
  encode(input, prefix) {
      return prefix + btoa(input.replaceAll(/(.)/g,"\x00$1"))
        .replaceAll(/=+$/g,"") + "-";
  }

  decode(input) {
     return input.replaceAll(/[+&]([a-zA-Z0-9]+)-/g, ($0, match) => atob(match).replaceAll(/[\x00&-]/g,""));
  }

  matches(input) {
    return /^[&+][a-zA-Z0-9]{3,}-/.exec(input);
  }
}