Cleanup Text from U8a (wrapper class) (#1642)

* Cleanup Text from U8a (wrapper class)

* U8a test
This commit is contained in:
Jaco Greeff
2019-12-05 12:54:52 +01:00
committed by GitHub
parent 97fbb4732d
commit 191abe4e08
2 changed files with 8 additions and 1 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ describe('Text', (): void => {
testDecode('string', 'foo', 'foo');
testDecode('Text', new Text(registry, 'foo'), 'foo');
testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo');
testDecode('U8a', new U8a(registry, Uint8Array.from([12, 102, 111, 111])), 'foo');
testDecode('U8a', new U8a(registry, Uint8Array.from([102, 111, 111])), 'foo'); // no length
testDecode('object with `toString()`', { toString (): string { return 'foo'; } }, 'foo');
testDecode('hex input value', new Text(registry, '0x12345678'), '0x12345678', 'toHex');
});
+7
View File
@@ -9,6 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import { createType } from '../codec/create';
import Compact from '../codec/Compact';
import U8a from '../codec/U8a';
/**
* @name Text
@@ -37,6 +38,12 @@ export default class Text extends String implements Codec {
return '';
}
// for U8a, the internal buffer does not have an internal length
// (the same applies in e.g. Bytes, where length is added at encoding-time)
if (value instanceof U8a) {
return u8aToString(value);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();