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

Method performEncode

src/Encoder/MorseCode.js:129–182  ·  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

127 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
128 */
129 performEncode (content) {
130 const representation = this.getSettingValue('representation')
131
132 let shortMark = '.'
133 let longerMark = '-'
134 let spaceMark = '/'
135
136 if (representation === 'code') {
137 shortMark = this.getSettingValue('shortMark').getString()
138 longerMark = this.getSettingValue('longerMark').getString()
139 spaceMark = this.getSettingValue('spaceMark').getString()
140 }
141
142 let string = content.toLowerCase().getChars()
143 // Encode each character
144 .map(char => {
145 const code = MorseCodeEncoder.encodeCharacter(
146 char, shortMark, longerMark, spaceMark)
147
148 if (code === null) {
149 throw new InvalidInputError(
150 `Char '${char}' is not defined in morse code`)
151 }
152 return code
153 })
154 // Glue it back together
155 .join(' ')
156
157 if (representation === 'timing') {
158 // Translate to timing representation
159 const signalOnMark = this.getSettingValue('signalOnMark').getString()
160 const signalOffMark = this.getSettingValue('signalOffMark').getString()
161
162 string = string
163 .split('')
164 .map(symbol => {
165 switch (symbol) {
166 // Dit
167 case '.':
168 return signalOnMark
169 // Dah
170 case '-':
171 return signalOnMark.repeat(3)
172 // Letter and word space
173 default:
174 return signalOffMark
175 }
176 })
177 // Glue together and add symbol space
178 .join(signalOffMark)
179 }
180
181 return string
182 }
183
184 /**
185 * Performs decode on given content.

Callers

nothing calls this directly

Calls 7

getSettingValueMethod · 0.80
getStringMethod · 0.80
joinMethod · 0.80
getCharsMethod · 0.80
toLowerCaseMethod · 0.80
encodeCharacterMethod · 0.80
splitMethod · 0.80

Tested by

no test coverage detected