Advertisement
GangBoss

Untitled

Mar 23rd, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class JumpEncoder {
  3.     constructor() {
  4.         this.charBorder = 128;
  5.     }
  6.  
  7.     Encode(text) {
  8.         let i = 0;
  9.         let result = "";
  10.         let inBrackets = (shift) => i + shift < text.length && shift < this.charBorder;
  11.         let hasThreeRepeats = (startIndex) => text.slice(startIndex, startIndex + 3) == text[startIndex].repeat(3);
  12.  
  13.         while (i < text.length) {
  14.             let repeatsCount = 1;
  15.  
  16.             while (inBrackets(repeatsCount)
  17.                 && text[i] == text[i + repeatsCount])
  18.                 repeatsCount++;
  19.  
  20.             if (repeatsCount >= 3) {
  21.                 result += String.fromCharCode(repeatsCount - 3 + this.charBorder) + text[i];
  22.                 i += repeatsCount;
  23.             }
  24.  
  25.             else {
  26.                 let nonRepeatsCount = repeatsCount;
  27.                 while (inBrackets(nonRepeatsCount) && !(hasThreeRepeats(i + nonRepeatsCount)))
  28.                     nonRepeatsCount++;
  29.  
  30.                 result += String.fromCharCode(nonRepeatsCount);
  31.                 result += text.slice(i, i + nonRepeatsCount)
  32.                 i += nonRepeatsCount;
  33.             }
  34.         }
  35.  
  36.         return result;
  37.     }
  38.  
  39.     Decode(text) {
  40.         let i = 0;
  41.         let result = "";
  42.         while (i < text.length) {
  43.             let currentcode = text.charCodeAt(i);
  44.             if (currentcode >= this.charBorder) {
  45.                 let repeatsCount = currentcode + 3 - this.charBorder;
  46.                 result += text[i + 1].repeat(repeatsCount);
  47.                 i += 2;
  48.             }
  49.             else {
  50.                 result += text.slice(i + 1, i + currentcode + 1)
  51.                 i += text.charCodeAt(i) + 1;
  52.             }
  53.         }
  54.         return result
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement