Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class JumpEncoder {
- constructor() {
- this.charBorder = 128;
- }
- Encode(text) {
- let i = 0;
- let result = "";
- let inBrackets = (shift) => i + shift < text.length && shift < this.charBorder;
- let hasThreeRepeats = (startIndex) => text.slice(startIndex, startIndex + 3) == text[startIndex].repeat(3);
- while (i < text.length) {
- let repeatsCount = 1;
- while (inBrackets(repeatsCount)
- && text[i] == text[i + repeatsCount])
- repeatsCount++;
- if (repeatsCount >= 3) {
- result += String.fromCharCode(repeatsCount - 3 + this.charBorder) + text[i];
- i += repeatsCount;
- }
- else {
- let nonRepeatsCount = repeatsCount;
- while (inBrackets(nonRepeatsCount) && !(hasThreeRepeats(i + nonRepeatsCount)))
- nonRepeatsCount++;
- result += String.fromCharCode(nonRepeatsCount);
- result += text.slice(i, i + nonRepeatsCount)
- i += nonRepeatsCount;
- }
- }
- return result;
- }
- Decode(text) {
- let i = 0;
- let result = "";
- while (i < text.length) {
- let currentcode = text.charCodeAt(i);
- if (currentcode >= this.charBorder) {
- let repeatsCount = currentcode + 3 - this.charBorder;
- result += text[i + 1].repeat(repeatsCount);
- i += 2;
- }
- else {
- result += text.slice(i + 1, i + currentcode + 1)
- i += text.charCodeAt(i) + 1;
- }
- }
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement