Skip to content

Commit

Permalink
Merge 5a3ae53 into 2c16932
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Oct 9, 2018
2 parents 2c16932 + 5a3ae53 commit e8bad04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/index.ts
Expand Up @@ -199,7 +199,7 @@ export default class Api implements ApiInterface {
// if we don't have a value, do not fill in the entry, it will be up to the
// caller to sort this out, either ignoring or having a cache for older values
result.push(
item
item && !item.value.isEmpty
? createType(type, item.value.value)
: undefined
);
Expand Down
13 changes: 10 additions & 3 deletions packages/types/src/codec/Option.spec.js
Expand Up @@ -5,25 +5,32 @@
import Option from './Option';
import Text from '../Text';

const testDecode = (type, input, expected) =>
const testDecode = (type, input, expected, testLength = true) =>
it(`can decode from ${type}`, () => {
const o = new Option(Text, input);
expect(o.isEmpty).toBe(!expected.length)

expect(o.toString()).toBe(expected);
expect(o.isEmpty).toBe(testLength && !expected.length);
});

const testEncode = (to, expected) =>
it(`can encode ${to}`, () => {
const e = new Option(Text, 'foo');

expect(e[to]()).toEqual(expected);
});

describe('Option', () => {
it('converts undefined/null to empty', () => {
expect(new Option(Text, undefined).isEmpty).toBe(true);
expect(new Option(Text, null).isEmpty).toBe(true);
expect(new Option(Text, 'test').isEmpty).toBe(false);
});

testDecode('string (with)', 'foo', 'foo');
testDecode('string (without)', undefined, '');
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo');
testDecode('Uint8Array (without)', Uint8Array.from([0]), '');
testDecode('Uint8Array (without)', Uint8Array.from([0]), '', false);

// testEncode('toHex', '0x010c666f6f'); // FIXME Add this
testEncode('toString', 'foo');
Expand Down
12 changes: 9 additions & 3 deletions packages/types/src/codec/Option.ts
Expand Up @@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import isNull from '@polkadot/util/is/null';
import isU8a from '@polkadot/util/is/u8a';
import isUndefined from '@polkadot/util/is/undefined';

Expand All @@ -19,14 +20,19 @@ export default class Option<T> extends Base<Base<T>> {
Option.decodeOption(Type, value)
);

this._isEmpty = isUndefined(value) || value.length <= 1;
this._isEmpty = isNull(value) || isUndefined(value);
}

static decodeOption<O> (Type: { new(value?: any): Base<O> }, value?: any): Base<O> {
if (isU8a(value)) {
return new Type(value.subarray(1));
}
return new Type(value);

return new Type(
isNull(value) || isUndefined(value)
? undefined
: value
);
}

static with<O> (Type: { new(value?: any): Base<O> }): { new(value?: any): Option<O> } {
Expand Down Expand Up @@ -56,7 +62,7 @@ export default class Option<T> extends Base<Base<T>> {
}

fromJSON (input: any): Option<T> {
this._isEmpty = isUndefined(input);
this._isEmpty = isNull(input) || isUndefined(input);

if (!this._isEmpty) {
this.raw.fromJSON(input);
Expand Down

0 comments on commit e8bad04

Please sign in to comment.