Option allows null/undefined as empty (#230)
This commit is contained in:
@@ -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
|
||||
);
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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> } {
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user