Ensure Option<None> of different type evaluates to None (#4647)

This commit is contained in:
Jaco
2022-03-11 08:09:21 +02:00
committed by GitHub
parent e8cdbf9e80
commit 6c33e98553
2 changed files with 11 additions and 1 deletions
@@ -43,6 +43,16 @@ describe('Option', (): void => {
});
});
it('can convert between different Some/None', (): void => {
const def = '{ "foo":"Text", "zar":"Text" }';
const none = new Option(registry, def, null);
const some = new Option(registry, def, new Option(registry, def, { foo: 'a', zar: 'b' }));
expect(new Option(registry, def, none).isNone).toBe(true);
expect(new Option(registry, def, some).isNone).toBe(false);
expect(new Option(registry, def, some).unwrap().toHuman()).toEqual({ foo: 'a', zar: 'b' });
});
it('correctly handles booleans', (): void => {
expect(new Option(registry, bool).isNone).toBe(true);
expect(new Option(registry, bool, true).isSome).toBe(true);
+1 -1
View File
@@ -25,7 +25,7 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
value = value.value;
}
if (isNull(value) || isUndefined(value) || value === '0x') {
if (isNull(value) || isUndefined(value) || value === '0x' || value instanceof None) {
return new None(registry);
} else if (value instanceof Type) {
// don't re-create, use as it (which also caters for derived types)