From 15b22dab1ca89582970ac3ffca3bdd280ac868c4 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 15 Feb 2019 10:52:00 +0100 Subject: [PATCH] Allow indexOf to work with Codec eq (#699) * Allow indexOf to work with Codec eq * Convert to type before check * Swap compare (in case of empty/invalid values) --- packages/api/package.json | 2 +- packages/rpc-core/package.json | 2 +- packages/rpc-provider/package.json | 6 ++--- packages/type-extrinsics/package.json | 2 +- packages/type-storage/package.json | 6 ++--- packages/types/package.json | 4 ++-- packages/types/src/codec/Vector.spec.ts | 9 ++++++++ packages/types/src/codec/Vector.ts | 18 +++++++++++++++ yarn.lock | 30 ++++++++++++------------- 9 files changed, 53 insertions(+), 26 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index 11d2a82c72..f0a230afa6 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -36,7 +36,7 @@ "@polkadot/rpc-rx": "^0.43.3", "@polkadot/storage": "^0.43.3", "@polkadot/types": "^0.43.3", - "@polkadot/util-crypto": "^0.34.9", + "@polkadot/util-crypto": "^0.34.10", "@types/rx": "^4.1.1", "rxjs": "^6.3.3" } diff --git a/packages/rpc-core/package.json b/packages/rpc-core/package.json index a852d50db3..dc3df382eb 100644 --- a/packages/rpc-core/package.json +++ b/packages/rpc-core/package.json @@ -33,6 +33,6 @@ "@polkadot/jsonrpc": "^0.43.3", "@polkadot/rpc-provider": "^0.43.3", "@polkadot/types": "^0.43.3", - "@polkadot/util": "^0.34.9" + "@polkadot/util": "^0.34.10" } } diff --git a/packages/rpc-provider/package.json b/packages/rpc-provider/package.json index 33fda9cfd1..a9e0b54c5c 100644 --- a/packages/rpc-provider/package.json +++ b/packages/rpc-provider/package.json @@ -30,10 +30,10 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-provider#readme", "dependencies": { "@babel/runtime": "^7.2.0", - "@polkadot/keyring": "^0.34.9", + "@polkadot/keyring": "^0.34.10", "@polkadot/storage": "^0.43.3", - "@polkadot/util": "^0.34.9", - "@polkadot/util-crypto": "^0.34.9", + "@polkadot/util": "^0.34.10", + "@polkadot/util-crypto": "^0.34.10", "@types/nock": "^9.3.0", "eventemitter3": "^3.1.0", "isomorphic-fetch": "^2.2.1", diff --git a/packages/type-extrinsics/package.json b/packages/type-extrinsics/package.json index be579b466c..5f769c1157 100644 --- a/packages/type-extrinsics/package.json +++ b/packages/type-extrinsics/package.json @@ -12,6 +12,6 @@ "dependencies": { "@babel/runtime": "^7.2.0", "@polkadot/types": "^0.43.3", - "@polkadot/util": "^0.34.9" + "@polkadot/util": "^0.34.10" } } diff --git a/packages/type-storage/package.json b/packages/type-storage/package.json index c3468971b9..63de672e01 100644 --- a/packages/type-storage/package.json +++ b/packages/type-storage/package.json @@ -30,9 +30,9 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-storage#readme", "dependencies": { "@babel/runtime": "^7.2.0", - "@polkadot/keyring": "^0.34.9", + "@polkadot/keyring": "^0.34.10", "@polkadot/types": "^0.43.3", - "@polkadot/util": "^0.34.9", - "@polkadot/util-crypto": "^0.34.9" + "@polkadot/util": "^0.34.10", + "@polkadot/util-crypto": "^0.34.10" } } diff --git a/packages/types/package.json b/packages/types/package.json index 5e30983424..59e01bb784 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -30,8 +30,8 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme", "dependencies": { "@babel/runtime": "^7.2.0", - "@polkadot/keyring": "^0.34.9", - "@polkadot/util": "^0.34.9", + "@polkadot/keyring": "^0.34.10", + "@polkadot/util": "^0.34.10", "core-js": "^2.6.2" } } diff --git a/packages/types/src/codec/Vector.spec.ts b/packages/types/src/codec/Vector.spec.ts index 66ac4ed114..1ecb01c0ca 100644 --- a/packages/types/src/codec/Vector.spec.ts +++ b/packages/types/src/codec/Vector.spec.ts @@ -127,5 +127,14 @@ describe('Vector', () => { it('compares against codec + primitive types', () => { expect(vec.eq(['123', new Text('456')])).toBe(true); }); + + it('finds the index of an value', () => { + const myId = '5GfWeNe33QeyzVMV2uENuvSXLVfVXCiuLvfEP6WCDHADZhen'; + const vec = new Vector(AccountId, [ + '5H4787dXL43BaA6PvTwUTyA38JBbfC9r8QyK3ay8Nozc2Ta3', '5GeJHN5EcUGPoa5pUwYkXjymoDVN1DJHsBR4UGX4XRAwKBVk', '5Dn8F1SUX6SoLt1BTfKEPL5VY9wMvG1A6tEJTSCHpLsinThm', '5DPW1n4q2THUjKGj3QKkcqdh6oxY1bmLsSQ7t8FuiuNtRoUc', '5E8k3u7iRysqPPYsBK1UgetCquZznwRZxryTzynkDTzg3vNy', '5DteAmjoppNJ8rrBugKctCXM82UVWZSCHeU8pqFwQBbyAWyB', '5GfWeNe33QeyzVMV2uENuvSXLVfVXCiuLvfEP6WCDHADZhen' + ]); + + expect(vec.indexOf(myId)).toEqual(6); + }); }); }); diff --git a/packages/types/src/codec/Vector.ts b/packages/types/src/codec/Vector.ts index 94c7a77749..4ba5d6195e 100644 --- a/packages/types/src/codec/Vector.ts +++ b/packages/types/src/codec/Vector.ts @@ -81,6 +81,24 @@ export default class Vector< return compareArray(this, other); } + /** + * @description Finds the index of the value in the array + */ + indexOf (_other?: any): number { + // convert type first, this removes overhead from the eq + const other = _other instanceof this._Type + ? _other + : new this._Type(_other); + + for (let i = 0; i < this.length; i++) { + if (other.eq(this[i])) { + return i; + } + } + + return -1; + } + /** * @description Converts the Object to an standard JavaScript Array */ diff --git a/yarn.lock b/yarn.lock index 0e7d6b02cc..b0473fe8a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1338,14 +1338,14 @@ typedoc-plugin-no-inherit "^1.1.2" typescript "^3.2.2" -"@polkadot/keyring@^0.34.9": - version "0.34.9" - resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.34.9.tgz#54c5e9c503d7ea2d3176ebc744bd50e37f4ed99f" - integrity sha512-kQP+Ciawttz1egWPcCb7RXjZKW3IsGcPao8iau/beJFKAz32oOxem1BVA0ToiynYj9y7ivbJCs/E0lf+5iY0Ng== +"@polkadot/keyring@^0.34.10": + version "0.34.10" + resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.34.10.tgz#e7f4dfca7b5eedd0062e2f17e2d0f5b6ca650a19" + integrity sha512-Ctq/qLJKLocMIFRInA1f7KgyLS4RWRf3UxzVPZgSkLB0rId2gWKF/BIqFSpbWAUfwN7FxMRZ0HxcUlTRQMj48A== dependencies: "@babel/runtime" "^7.2.0" - "@polkadot/util" "^0.34.9" - "@polkadot/util-crypto" "^0.34.9" + "@polkadot/util" "^0.34.10" + "@polkadot/util-crypto" "^0.34.10" "@types/bs58" "^4.0.0" bs58 "^4.0.1" @@ -1354,14 +1354,14 @@ resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.52.tgz#6fb09e494d244dad4b3a9ca581bfe13beec66565" integrity sha512-sij1O0x4CY51A394RYD4/aQwDPwIxIeTOpYI4AZgdF/vq5nvF14b4XFq9vAcSnblaIosf0sYKoz1f3dkN3QqLw== -"@polkadot/util-crypto@^0.34.9": - version "0.34.9" - resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.34.9.tgz#bcdf53d062c79c277b79cd167b6a14d42432a7ba" - integrity sha512-XXLJCSks5/14Hc6nZnVyKLLVElDJ+c454kOFt7xjeYpBSohBSSeGx1G9uFoPsAILDC4vuDD+ZtO2fQtzNGa9aQ== +"@polkadot/util-crypto@^0.34.10": + version "0.34.10" + resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.34.10.tgz#84587b9c0f627f096c6bd6a12ad3f3d8b6dc230a" + integrity sha512-DShVvijGa/prI2JEBuytczBCV1HKpx2rhC1CZwbsbWjBUtK5GumIC6aMGQGdEPltGZ9qz6gS2YiBWrl31fLG/Q== dependencies: "@babel/runtime" "^7.2.0" "@parity/schnorrkel-js" "^0.1.2" - "@polkadot/util" "^0.34.9" + "@polkadot/util" "^0.34.10" "@types/bip39" "^2.4.1" "@types/webassembly-js-api" "^0.0.2" bip39 "^2.5.0" @@ -1370,10 +1370,10 @@ tweetnacl "^1.0.0" xxhashjs "^0.2.2" -"@polkadot/util@^0.34.9": - version "0.34.9" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.34.9.tgz#919dc57de5441358a45f1c4f2fcf2cf0773285ab" - integrity sha512-kyGwG9apL8QBseIMVkP4BanyJboekF9hGLvyNhpka0f9aynEUs/TxRpExo5KRL4r9DTEKrDA8TDDBs3uQ1veHg== +"@polkadot/util@^0.34.10": + version "0.34.10" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.34.10.tgz#b1d4d683c92974a32f861e67349d7109ece451c2" + integrity sha512-nNegFolqS7OiiXoxe7LK1bq8x/gDcPYCHFTyoMub5ZgEEn0uemy3g7YRoXF9QA9BYc3E7pRTXYpLqAqzSvBn+Q== dependencies: "@babel/runtime" "^7.2.0" "@types/bn.js" "^4.11.3"