Allow EnumType to decode isHex (#611)

* Allow EnumType to decode isHex

* Consistently use string (not String/string mix)
This commit is contained in:
Jaco Greeff
2019-01-21 10:30:04 +01:00
committed by GitHub
parent 15721c16a6
commit cfd71db468
2 changed files with 15 additions and 2 deletions
@@ -35,6 +35,15 @@ describe('Struct', () => {
).toEqual('some text value');
});
it('decodes from hex', () => {
expect(
new EnumType(
{ Text, U32 },
'0x0134120000'
).value.toString()
).toEqual('4660'); // 0x1234 in decimal
});
it('decodes from a JSON input (mixed case)', () => {
expect(
new EnumType(
+6 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert, isNumber, isObject, isString, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import Base from './Base';
import Null from '../Null';
@@ -61,7 +61,11 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
} else if (isNumber(value)) {
return EnumType.createValue(def, value);
} else if (isString(value)) {
return EnumType.createViaJSON(def, value.toString());
const _str = value.toString();
return isHex(_str)
? EnumType.decodeViaValue(def, hexToU8a(_str))
: EnumType.createViaJSON(def, _str);
} else if (isObject(value)) {
const key = Object.keys(value)[0];