64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
const MamiSRLE = (() => {
|
|
return {
|
|
encode: (input, cutoff) => {
|
|
let output = '',
|
|
last = '',
|
|
repeat = 0;
|
|
|
|
input = (input || '').toString();
|
|
cutoff = cutoff || 1
|
|
|
|
for(let i = 0; i <= input.length; ++i) {
|
|
const chr = input[i];
|
|
|
|
if(last === chr)
|
|
++repeat;
|
|
else {
|
|
if(repeat > cutoff)
|
|
for(const repChr in repeat.toString())
|
|
output += ')!@#$%^&*('[parseInt(repChr)];
|
|
else
|
|
output += last.repeat(repeat);
|
|
|
|
repeat = 0;
|
|
|
|
if(chr !== undefined) {
|
|
output += chr;
|
|
last = chr;
|
|
}
|
|
}
|
|
}
|
|
|
|
return output;
|
|
},
|
|
|
|
decode: input => {
|
|
let output = '',
|
|
repeat = '',
|
|
chr;
|
|
|
|
input = (input || '').toString().split('').reverse();
|
|
|
|
for(;;) {
|
|
const chr = input.pop(), num = ')!@#$%^&*('.indexOf(chr);
|
|
|
|
if(num >= 0)
|
|
repeat += num;
|
|
else {
|
|
if(repeat) {
|
|
output += output.slice(-1).repeat(parseInt(repeat));
|
|
repeat = '';
|
|
}
|
|
|
|
if(chr === undefined)
|
|
break;
|
|
|
|
output += chr;
|
|
}
|
|
}
|
|
|
|
return output;
|
|
},
|
|
};
|
|
})();
|