AccountId toString() with isEmpty (#592)

* AccountId toString() with isEmpty

* isEmpty is real isEmpty check
This commit is contained in:
Jaco Greeff
2019-01-16 10:05:46 +01:00
committed by GitHub
parent af3d4b25ed
commit 695e4f2cad
4 changed files with 19 additions and 9 deletions
+3 -2
View File
@@ -43,9 +43,9 @@ describe('AccountId', () => {
});
describe('encoding', () => {
const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string) =>
const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string, input: string = '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s') =>
it(`can encode ${to}`, () => {
const a = new AccountId('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s');
const a = new AccountId(input);
expect(a[to]()).toEqual(expected);
});
@@ -53,6 +53,7 @@ describe('AccountId', () => {
testEncode('toHex', '0x0102030405060708010203040506070801020304050607080102030405060708');
testEncode('toJSON', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s');
testEncode('toString', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s');
testEncode('toString', '-', '0x00');
testEncode('toU8a', Uint8Array.from([
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8
+11 -2
View File
@@ -41,9 +41,16 @@ export default class AccountId extends U8aFixed {
}
/**
* @description Returns true if the type wraps a non-encodable value
* @description Returns true if the type wraps an empty value
*/
get isEmpty (): boolean {
return this.length === 1 && this[0] === 0;
}
/**
* @description Returns true if the type wraps a non-valid value
*/
get isInvalid (): boolean {
return this.length !== 32;
}
@@ -58,6 +65,8 @@ export default class AccountId extends U8aFixed {
* @description Returns the string representation of the value
*/
toString (): string {
return AccountId.encode(this);
return this.isEmpty
? '-'
: AccountId.encode(this);
}
}