Add isChildClass (#544)

* Add isChildClass

* isChildClass is return

* Cleanup doc example
This commit is contained in:
Jaco Greeff
2020-02-22 13:26:12 +01:00
committed by GitHub
parent 20b46a65fa
commit 91c4c407a9
5 changed files with 65 additions and 1 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
# 2.5.1-beta.x
- Add `isBigInt` to allow `BigInt` checks. Also add support in `bnToBn`
- Add `isBigInt(value)` to allow `BigInt` checks. Also add support in `bnToBn`, and anything that uses this conversion for numbers
- Add `isChildClass(Parent, Child?)` to check if a class extends the parent
# 2.4.1 Feb 18, 2020
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { isChildClass } from '.';
describe('isChildClass', (): void => {
it('returns true when a Child value', (): void => {
expect(
isChildClass(BN, class Test extends BN {})
).toEqual(true);
});
it('returns false on non-Child values', (): void => {
expect(
isChildClass(BN, Uint8Array)
).toEqual(false);
});
it('returns false on undefined', (): void => {
expect(
isChildClass(BN)
).toEqual(false);
});
});
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Constructor } from '../types';
/**
* @name isChildClass
* @summary Tests if the child extends the parent Class
* @description
* Checks to see if the child Class extends the parent Class
* @example
* <BR>
*
* ```javascript
* import { isChildClass } from '@polkadot/util';
*
* console.log('isChildClass', isChildClass(BN, BN); // => true
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function isChildClass <T = any> (Parent: Constructor<T>, Child?: Constructor | null): Child is Constructor<T> {
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
return Child
// eslint-disable-next-line no-prototype-builtins
? Parent === Child || Parent.isPrototypeOf(Child)
: false;
}
+1
View File
@@ -9,6 +9,7 @@ export { default as isBigInt } from './bigInt';
export { default as isBn } from './bn';
export { default as isBuffer } from './buffer';
export { default as isBoolean } from './boolean';
export { default as isChildClass } from './childClass';
export { default as isError } from './error';
export { default as isFunction } from './function';
export { default as isHex } from './hex';
+6
View File
@@ -4,6 +4,12 @@
import BN from 'bn.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface Constructor<T = any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(...value: any[]): T;
}
export interface ToBn {
toBn: () => BN;
}