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, October 9, 2024 at 4:25:33 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);
  }
}