MCPcopy Create free account
hub / github.com/cryptii/cryptii / performEncode

Method performEncode

src/Encoder/BaudotCode.js:168–213  ·  view source on GitHub ↗

* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content

(content)

Source from the content-addressed store, hash-verified

166 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
167 */
168 performEncode (content) {
169 // Retrieve character set
170 const variant = this.getSettingValue('variant')
171 const characterSet = this.getVariantCharacterSet(variant)
172 const switchToFigures = characterSet.indexOf('FS') % 32
173 const switchToLetters = characterSet.indexOf('LS') % 32
174
175 const codePoints = content.getCodePoints()
176 const m = codePoints.length
177
178 const tape = []
179 let index, codePoint
180 let figureSet = false
181
182 // Go through code points
183 for (let i = 0; i < m; i++) {
184 codePoint = codePoints[i]
185 index = characterSet.indexOf(codePoint)
186
187 // If character was not found try to map the uppercase character
188 if (index === -1 && codePoint >= 97 && codePoint <= 122) {
189 index = characterSet.indexOf(codePoint - 32)
190 }
191
192 if (index >= 0 && index < 32) {
193 // This character is situated on the letter set
194 // Switch to letter set
195 if (figureSet) {
196 tape.push(switchToLetters)
197 figureSet = false
198 }
199 tape.push(index)
200 } else if (index >= 32) {
201 // This character is situated on the figure set
202 // Switch to figure set
203 if (!figureSet) {
204 tape.push(switchToFigures)
205 figureSet = true
206 }
207 tape.push(index - 32)
208 }
209 }
210
211 // Compose byte array from 5-bit tape
212 return ArrayUtil.resizeBitSizedArray(tape, 5, 8)
213 }
214
215 /**
216 * Performs decode on given content.

Callers

nothing calls this directly

Calls 5

getSettingValueMethod · 0.80
indexOfMethod · 0.80
getCodePointsMethod · 0.80
resizeBitSizedArrayMethod · 0.80

Tested by

no test coverage detected