* Performs encode or decode on given content. * @param {Chain} content * @param {boolean} isEncode True for encoding, false for decoding * @return {number[]|string|Uint8Array|Chain} Resulting content
(content, isEncode)
| 78 | * @return {number[]|string|Uint8Array|Chain} Resulting content |
| 79 | */ |
| 80 | performTranslate (content, isEncode) { |
| 81 | const { shift, caseStrategy, includeForeignChars } = |
| 82 | this.getSettingValues() |
| 83 | |
| 84 | // Prepare alphabet(s) depending on chosen case strategy |
| 85 | let alphabet = this.getSettingValue('alphabet') |
| 86 | let uppercaseAlphabet |
| 87 | if (caseStrategy !== 'strict') { |
| 88 | alphabet = alphabet.toLowerCase() |
| 89 | uppercaseAlphabet = alphabet.toUpperCase() |
| 90 | } |
| 91 | |
| 92 | const m = alphabet.getLength() |
| 93 | const n = content.getLength() |
| 94 | const result = new Array(n) |
| 95 | |
| 96 | let codePoint, x, y, uppercase |
| 97 | let j = 0 |
| 98 | |
| 99 | // Go through each character in content |
| 100 | for (let i = 0; i < n; i++) { |
| 101 | codePoint = content.getCodePointAt(i) |
| 102 | |
| 103 | // Match alphabet character |
| 104 | x = alphabet.indexOfCodePoint(codePoint) |
| 105 | uppercase = false |
| 106 | |
| 107 | // Match uppercase alphabet character (depending on case strategy) |
| 108 | if (x === -1 && caseStrategy !== 'strict') { |
| 109 | x = uppercaseAlphabet.indexOfCodePoint(codePoint) |
| 110 | uppercase = true |
| 111 | } |
| 112 | |
| 113 | if (x === -1) { |
| 114 | // Character is not in the alphabet |
| 115 | if (includeForeignChars) { |
| 116 | result[j++] = codePoint |
| 117 | } |
| 118 | } else { |
| 119 | // Shift character |
| 120 | if (typeof shift !== 'bigint') { |
| 121 | y = MathUtil.mod(x + shift * (isEncode ? 1 : -1), m) |
| 122 | } else { |
| 123 | y = Number(MathUtil.mod( |
| 124 | BigInt(x) + shift * BigInt(isEncode ? 1 : -1), |
| 125 | BigInt(m) |
| 126 | )) |
| 127 | } |
| 128 | |
| 129 | // Translate index to character following the case strategy |
| 130 | if (caseStrategy === 'maintain' && uppercase) { |
| 131 | result[j++] = uppercaseAlphabet.getCodePointAt(y) |
| 132 | } else { |
| 133 | result[j++] = alphabet.getCodePointAt(y) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |
nothing calls this directly
no test coverage detected