From 91c4c407a9691ec47768129a6705d0cec0c1b5f2 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 22 Feb 2020 13:26:12 +0100 Subject: [PATCH] Add isChildClass (#544) * Add isChildClass * isChildClass is return * Cleanup doc example --- CHANGELOG.md | 3 ++- packages/util/src/is/childClass.spec.ts | 27 +++++++++++++++++++++++ packages/util/src/is/childClass.ts | 29 +++++++++++++++++++++++++ packages/util/src/is/index.ts | 1 + packages/util/src/types.ts | 6 +++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 packages/util/src/is/childClass.spec.ts create mode 100644 packages/util/src/is/childClass.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 359358e07..53613b0c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/util/src/is/childClass.spec.ts b/packages/util/src/is/childClass.spec.ts new file mode 100644 index 000000000..c62bcbb28 --- /dev/null +++ b/packages/util/src/is/childClass.spec.ts @@ -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); + }); +}); diff --git a/packages/util/src/is/childClass.ts b/packages/util/src/is/childClass.ts new file mode 100644 index 000000000..b947c69c8 --- /dev/null +++ b/packages/util/src/is/childClass.ts @@ -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 + *
+ * + * ```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 (Parent: Constructor, Child?: Constructor | null): Child is Constructor { + // 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; +} diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index 350fd7bb1..e84ff4be3 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -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'; diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index d4d20887a..13a7f25e8 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -4,6 +4,12 @@ import BN from 'bn.js'; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export interface Constructor { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + new(...value: any[]): T; +} + export interface ToBn { toBn: () => BN; }