Use isChildClass & isBigInt from @polkadot/util (#1924)

* Use isChildClass & isBigInt from @polkadot/util

* Allow never

* Bumps

* Common 2.5.1

* Fix overrides

* Remove gh-pages script (in dev)
This commit is contained in:
Jaco Greeff
2020-02-22 17:02:03 +01:00
committed by GitHub
parent 2a16b04420
commit 196a0891b8
17 changed files with 92 additions and 115 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
"@polkadot/api": "1.4.0-beta.29",
"@polkadot/rpc-core": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util": "^2.5.1",
"bn.js": "^5.1.1",
"rxjs": "^6.5.4"
}
+3 -3
View File
@@ -32,13 +32,13 @@
"@polkadot/rpc-core": "1.4.0-beta.29",
"@polkadot/rpc-provider": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util-crypto": "^2.4.1",
"@polkadot/util": "^2.5.1",
"@polkadot/util-crypto": "^2.5.1",
"bn.js": "^5.1.1",
"memoizee": "^0.4.14",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1"
"@polkadot/keyring": "^2.5.1"
}
}
+4 -4
View File
@@ -28,18 +28,18 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"@polkadot/api-derive": "1.4.0-beta.29",
"@polkadot/keyring": "^2.4.1",
"@polkadot/keyring": "^2.5.1",
"@polkadot/metadata": "1.4.0-beta.29",
"@polkadot/rpc-core": "1.4.0-beta.29",
"@polkadot/rpc-provider": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util-crypto": "^2.4.1",
"@polkadot/util": "^2.5.1",
"@polkadot/util-crypto": "^2.5.1",
"bn.js": "^5.1.1",
"eventemitter3": "^4.0.0",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1"
"@polkadot/keyring": "^2.5.1"
}
}
+1 -1
View File
@@ -28,6 +28,6 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1"
"@polkadot/util": "^2.5.1"
}
}
+3 -3
View File
@@ -28,11 +28,11 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util-crypto": "^2.4.1",
"@polkadot/util": "^2.5.1",
"@polkadot/util-crypto": "^2.5.1",
"bn.js": "^5.1.1"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1"
"@polkadot/keyring": "^2.5.1"
}
}
+2 -2
View File
@@ -31,11 +31,11 @@
"@polkadot/metadata": "1.4.0-beta.29",
"@polkadot/rpc-provider": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util": "^2.5.1",
"memoizee": "^0.4.14",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1"
"@polkadot/keyring": "^2.5.1"
}
}
+4 -4
View File
@@ -30,16 +30,16 @@
"@polkadot/jsonrpc": "1.4.0-beta.29",
"@polkadot/metadata": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util-crypto": "^2.4.1",
"@polkadot/util": "^2.5.1",
"@polkadot/util-crypto": "^2.5.1",
"bn.js": "^5.1.1",
"eventemitter3": "^4.0.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.31"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1",
"@polkadot/keyring": "^2.5.1",
"mock-socket": "^9.0.3",
"nock": "^11.8.2"
"nock": "^12.0.1"
}
}
+1 -1
View File
@@ -41,7 +41,7 @@
"@polkadot/metadata": "1.4.0-beta.29",
"@polkadot/rpc-provider": "1.4.0-beta.29",
"@polkadot/types": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util": "^2.5.1",
"websocket": "^1.0.31",
"yargs": "^15.1.0"
},
+1 -9
View File
@@ -5,15 +5,7 @@
import { Constructor } from '@polkadot/types/types';
import UInt from '@polkadot/types/codec/UInt';
// See if a class is child of another class
// FIMXE This could go in util some day
/** @internal */
export function isChildClass (Parent: Constructor<any>, Child: Constructor<any>): boolean {
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
// eslint-disable-next-line no-prototype-builtins
return Parent === Child || Parent.isPrototypeOf(Child);
}
import { isChildClass } from '@polkadot/util';
/** @internal */
export function isCompactEncodable (Child: Constructor<any>): boolean {
+6 -6
View File
@@ -5,21 +5,22 @@
import { TypeDef, TypeDefInfo } from '@polkadot/types/create/types';
import { Constructor, Registry } from '@polkadot/types/types';
import { stringLowerFirst } from '@polkadot/util';
import { isChildClass, isCompactEncodable } from './class';
import { ClassOf, ClassOfUnsafe, getTypeDef } from '@polkadot/types/create';
import AbstractInt from '@polkadot/types/codec/AbstractInt';
import Compact from '@polkadot/types/codec/Compact';
import Enum from '@polkadot/types/codec/Enum';
import Option from '@polkadot/types/codec/Option';
import Struct from '@polkadot/types/codec/Struct';
import UInt from '@polkadot/types/codec/UInt';
import Vec from '@polkadot/types/codec/Vec';
import GenericAccountId from '@polkadot/types/generic/AccountId';
import GenericAddress from '@polkadot/types/generic/Address';
import Vote, { convictionNames as _voteConvictions } from '@polkadot/types/generic/Vote';
import Null from '@polkadot/types/primitive/Null';
import * as primitiveClasses from '@polkadot/types/primitive';
import { isChildClass, stringLowerFirst } from '@polkadot/util';
import { isCompactEncodable } from './class';
import { formatType } from './formatting';
import { setImports, TypeImports } from './imports';
@@ -96,7 +97,7 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
throw new Error(`Unhandled subtype in Vec, ${JSON.stringify(subDef)}`);
}
} else if (isChildClass(Enum, Clazz)) {
const e = new Clazz(registry) as Enum;
const e = new (Clazz as Constructor)(registry) as Enum;
if (e.isBasic) {
possibleTypes.push(arrayToStrType(e.defKeys), 'number');
@@ -106,7 +107,7 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
}
possibleTypes.push('Uint8Array');
} else if (isChildClass(AbstractInt as unknown as Constructor<any>, Clazz) || isChildClass(Compact, Clazz)) {
} else if (isChildClass(AbstractInt as unknown as Constructor<UInt>, Clazz) || isChildClass(Compact, Clazz)) {
possibleTypes.push('AnyNumber', 'Uint8Array');
} else if (isChildClass(GenericAddress, Clazz)) {
possibleTypes.push('Address', 'AccountId', 'AccountIndex', 'string', 'Uint8Array');
@@ -117,8 +118,7 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
} else if (isChildClass(Null, Clazz)) {
possibleTypes.push('null');
} else if (isChildClass(Struct, Clazz)) {
// TODO We don't really want any here, these should be expanded
const s = new Clazz(registry) as Struct;
const s = new (Clazz as Constructor)(registry) as Struct;
const obj = s.defKeys.map((key): string => `${key}?: any`).join('; ');
possibleTypes.push(`{ ${obj} }`, 'string', 'Uint8Array');
+3 -3
View File
@@ -28,15 +28,15 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"@polkadot/metadata": "1.4.0-beta.29",
"@polkadot/util": "^2.4.1",
"@polkadot/util-crypto": "^2.4.1",
"@polkadot/util": "^2.5.1",
"@polkadot/util-crypto": "^2.5.1",
"@types/bn.js": "^4.11.6",
"bn.js": "^5.1.1",
"memoizee": "^0.4.14",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1",
"@polkadot/keyring": "^2.5.1",
"@types/memoizee": "^0.4.3"
}
}
+1 -8
View File
@@ -15,11 +15,6 @@ export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
export const DEFAULT_UINT_BITS = 64;
// TODO Impot this from util once supported there
export function isBigInt (value: any): value is BigInt {
return typeof value === 'bigint';
}
/**
* @name AbstractInt
* @ignore
@@ -56,10 +51,8 @@ export default abstract class AbstractInt extends BN implements Codec {
return hexToBn(value, { isLe: false, isNegative }).toString();
} else if (isU8a(value)) {
return AbstractInt.decodeAbstracIntU8a(value, bitLength, isNegative);
} else if (isBigInt(value)) {
return value.toString();
} else if (isString(value)) {
return new BN(value, 10).toString();
return new BN(value.toString(), 10).toString();
}
return bnToBn(value).toString();
+3 -5
View File
@@ -5,11 +5,11 @@
import { AnyNumber, Codec, Constructor, ICompact, InterfaceTypes, Registry } from '../types';
import BN from 'bn.js';
import { compactAddLength, compactFromU8a, compactStripLength, compactToU8a, isBn, isNumber, isString } from '@polkadot/util';
import { compactAddLength, compactFromU8a, compactStripLength, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
import { typeToConstructor } from './utils';
import { UIntBitLength, isBigInt } from './AbstractInt';
import { UIntBitLength } from './AbstractInt';
import Base from './Base';
export interface CompactEncodable extends Codec {
@@ -60,9 +60,7 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
public static decodeCompact<T extends CompactEncodable> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): CompactEncodable {
if (value instanceof Compact) {
return new Type(registry, value.raw);
} else if (isBigInt(value)) {
return new Type(registry, value.toString());
} else if (isString(value) || isNumber(value) || isBn(value)) {
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
return new Type(registry, value);
}
+2 -8
View File
@@ -4,20 +4,14 @@
import { Codec, Constructor } from '../types';
import { isChildClass } from '@polkadot/util';
import Struct from '../codec/Struct';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import Unconstructable from '../primitive/Unconstructable';
import { TypeRegistry } from './registry';
// Copied from interfacesTs
function isChildClass (Parent: Constructor<any>, Child?: Constructor<any>): boolean {
return Child
// eslint-disable-next-line no-prototype-builtins
? Parent === Child || Parent.isPrototypeOf(Child)
: false;
}
describe('TypeRegistry', (): void => {
const registry = new TypeRegistry();
+1 -2
View File
@@ -5,11 +5,10 @@
import { AnyNumber, Registry } from '../types';
import BN from 'bn.js';
import { bnToBn, isBn, isNumber, isU8a, isHex } from '@polkadot/util';
import { bnToBn, isBn, isBigInt, isNumber, isU8a, isHex } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import { createType } from '../create';
import { isBigInt } from '../codec/AbstractInt';
import U32 from '../primitive/U32';
export const ENUMSET_SIZE = new BN(64);