* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
(content)
| 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 | } |
nothing calls this directly
no test coverage detected