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: Wednesday, November 20, 2024 at 5:32: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);
  }
}