45 encode(plainText:
string =
""):
string {
47 const sanitizedText = this.sanitize(plainText);
50 const pairs:
string[] = [];
51 for (
const ch of sanitizedText) {
52 pairs.push(poly.encode(ch).replace(/\s/g,
""));
56 const coords: number[] = pairs.join(
"").split(
"").map(Number);
59 const half = Math.ceil(coords.length / 2);
60 const rearranged = coords.slice(0, half).concat(coords.slice(half));
63 const output:
string[] = [];
64 for (let i = 0; i < rearranged.length; i += 2) {
65 const code = String(rearranged[i]) + String(rearranged[i + 1]);
66 output.push(poly.decode(code));
69 return output.join(
"");
79 decode(cipherText:
string =
""):
string {
81 const sanitizedText = this.sanitize(cipherText);
84 const pairs:
string[] = [];
85 for (
const ch of sanitizedText) {
86 pairs.push(poly.encode(ch).replace(/\s/g,
""));
90 const coords: number[] = pairs.join(
"").split(
"").map(Number);
93 const half = Math.floor(coords.length / 2);
94 const firstHalf = coords.slice(0, half);
95 const secondHalf = coords.slice(half);
98 const output:
string[] = [];
99 for (let i = 0; i < firstHalf.length; i++) {
100 const code = String(firstHalf[i]) + String(secondHalf[i]);
101 output.push(poly.decode(code));
104 return output.join(
"");