Allow EnumType to decode isHex (#611)
* Allow EnumType to decode isHex * Consistently use string (not String/string mix)
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user