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
+1 -1
View File
@@ -24,7 +24,7 @@
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@polkadot/dev": "^0.23.10",
"@polkadot/dev": "^0.23.11",
"@polkadot/ts": "^0.1.50"
}
}
+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);
}
}
+4 -4
View File
@@ -1293,10 +1293,10 @@
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b"
integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==
"@polkadot/dev@^0.23.10":
version "0.23.10"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.10.tgz#ab76f12475fd3149cdab819174d5395da33f16d4"
integrity sha512-W9l9IygZnWnvm6hjdjY9IWbnrxiZSpFlRKD7bnA4PRKA326TbmtUFye4XOBoNVAA5+W2gGZd8UyGjs0ezfDCaA==
"@polkadot/dev@^0.23.11":
version "0.23.11"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.11.tgz#0b8d31553037b17113e29ead710117192880aac0"
integrity sha512-C8GIU06+yXW5Gc3K4jntLYI97JgCO/Hr7JRGFUYwjuPGmluBVkFS+b8/QaIHEwUCTGBk53FawKQqX9ABEXK14A==
dependencies:
"@babel/cli" "^7.2.3"
"@babel/core" "^7.2.2"