Skip to content

Commit

Permalink
fix: ignore null trytes during trytes to ascii conversion (iotaledger…
Browse files Browse the repository at this point in the history
…#480)

trytesToAscii() converts null trytes to non-breaking whitespaces. This (sometimes) lead to whitespaces at the end of ascii string.

This commit fixes the issue by ignoring null trytes during trytes to ascii conversion.
  • Loading branch information
Umair Sarfraz authored Jun 1, 2020
1 parent cf6b694 commit 5e2501c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
38 changes: 21 additions & 17 deletions packages/converter/src/ascii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import * as errors from './errors'

/**
* This method converts ASCII characters to [trytes](https://docs.iota.org/docs/getting-started/0.1/introduction/ternary).
*
*
* ## Related methods
*
*
* To convert trytes to ASCII characters, use the [`trytesToAscii()`]{@link #module_converter.trytesToAscii} method.
*
*
* @method asciiToTrytes
*
*
* @summary Converts ASCII characters to trytes.
*
*
* @memberof module:converter
*
* @param {string} input - ASCII input
*
*
* @example
* ```js
* let trytes = Converter.asciiToTrytes('Hello, where is my coffee?');
* ```
*
*
* @return {string} Trytes
*
*
* @throws {errors.INVALID_ASCII_CHARS}: Make sure that the `input` argument contains only valid ASCII characters.
*/
export const asciiToTrytes = (input: string): string => {
Expand All @@ -45,28 +45,28 @@ export const asciiToTrytes = (input: string): string => {

/**
* This method converts trytes to ASCII characters.
*
*
* Because each ASCII character is represented as 2 trytes, the given trytes must be of an even length.
*
* ## Related methods
*
*
* To convert ASCII characters to trytes, use the [`asciiToTrytes()`]{@link #module_converter.asciiToTrytes} method.
*
*
* @method trytesToAscii
*
*
* @summary Converts trytes to ASCII characters.
*
*
* @memberof module:converter
*
* @param {string} trytes - An even number of trytes
*
*
* @example
* ```js
* let message = Converter.trytesToAscii('IOTA');
* ```
*
*
* @return {string} ASCII characters
*
*
* @throws {errors.INVALID_TRYTES}: Make sure that the `trytes` argument contains only valid trytes (A-Z or 9).
* @throws {errors.INVALID_ODD_LENGTH}: Make sure that the `trytes` argument contains an even number of trytes.
*/
Expand All @@ -82,7 +82,11 @@ export const trytesToAscii = (trytes: string): string => {
let ascii = ''

for (let i = 0; i < trytes.length; i += 2) {
ascii += String.fromCharCode(TRYTE_ALPHABET.indexOf(trytes[i]) + TRYTE_ALPHABET.indexOf(trytes[i + 1]) * 27)
const charCode = TRYTE_ALPHABET.indexOf(trytes[i]) + TRYTE_ALPHABET.indexOf(trytes[i + 1]) * 27

if (charCode) {
ascii += String.fromCharCode(charCode)
}
}

return ascii
Expand Down
7 changes: 7 additions & 0 deletions packages/converter/test/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('trytesToAscii(trytes)', async assert => {
expected: 'IOTA',
})

assert({
given: 'tryte-encoded ascii with 9s',
should: 'convert to ascii ignoring zero/null char code',
actual: trytesToAscii('SBYBCCKB9999'),
expected: 'IOTA',
})

assert({
given: 'non-trytes',
should: 'throw error',
Expand Down

0 comments on commit 5e2501c

Please sign in to comment.