Option instantiates from Option (#465)

This commit is contained in:
Jaco Greeff
2018-12-12 16:13:58 +01:00
committed by GitHub
parent 5488328dc6
commit 3c8b3d325e
2 changed files with 11 additions and 5 deletions
+6
View File
@@ -27,6 +27,12 @@ describe('Option', () => {
expect(new Option(Text, 'test').isNone).toBe(false);
});
it('converts an option to an option', () => {
expect(
new Option(Text, new Option(Text, 'hello')).toString()
).toEqual('hello');
});
testDecode('string (with)', 'foo', 'foo');
testDecode('string (without)', undefined, '');
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo');
+5 -5
View File
@@ -25,11 +25,11 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
static decodeOption<O> (Type: Constructor, value?: any): Codec {
if (isU8a(value)) {
if (value[0] === 0) {
return new Null();
}
return new Type(value.subarray(1));
return value[0] === 0
? new Null()
: new Type(value.subarray(1));
} else if (value instanceof Option) {
return Option.decodeOption(Type, value.value);
}
return isNull(value) || isUndefined(value) || value instanceof Null