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

Method performDecode

src/Encoder/MorseCode.js:190–247  ·  view source on GitHub ↗

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

(content)

Source from the content-addressed store, hash-verified

188 * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
189 */
190 performDecode (content) {
191 const representation = this.getSettingValue('representation')
192 let string = content.getString()
193
194 let shortMark = '.'
195 let longerMark = '-'
196 let spaceMark = '/'
197
198 if (representation === 'code') {
199 shortMark = this.getSettingValue('shortMark').getString()
200 longerMark = this.getSettingValue('longerMark').getString()
201 spaceMark = this.getSettingValue('spaceMark').getString()
202 }
203
204 if (representation === 'timing') {
205 // Interpret timing code
206 const fromMarks = [
207 this.getSettingValue('signalOnMark').getString(),
208 this.getSettingValue('signalOffMark').getString()]
209
210 string = MorseCodeEncoder.translateMarks(
211 string, fromMarks, ['=', '.'])
212
213 // Translate timing code to morse code
214 string = string
215 .replace(/===/g, '-')
216 .replace(/\.{7}/g, ' / ')
217 .replace(/\.{3}/g, ' ')
218 .replace(/\./g, '')
219 .replace(/=/g, '.')
220 }
221
222 // Translate morse code to string
223 string = string
224 // Split characters by space
225 .split(' ')
226 // Decode each character
227 .map(rawCode => {
228 if (rawCode === '') {
229 return null
230 }
231
232 const char = MorseCodeEncoder.decodeCode(
233 rawCode, shortMark, longerMark, spaceMark)
234
235 if (char === null) {
236 throw new InvalidInputError(
237 `Code '${rawCode}' is not defined in morse code`)
238 }
239 return char
240 })
241 // Leave out codes that are not defined
242 .filter(char => char !== null)
243 // Glue it back together
244 .join('')
245
246 return string
247 }

Callers

nothing calls this directly

Calls 6

getSettingValueMethod · 0.80
getStringMethod · 0.80
translateMarksMethod · 0.80
joinMethod · 0.80
splitMethod · 0.80
decodeCodeMethod · 0.80

Tested by

no test coverage detected