Add isClass helper (#1720)
* Add isClass helper * add isOnFunction helper
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
import type { Constructor } from '../types';
|
||||
|
||||
import { isClass } from './class';
|
||||
|
||||
/**
|
||||
* @name isChildClass
|
||||
* @summary Tests if the child extends the parent Class
|
||||
@@ -20,8 +22,8 @@ import type { Constructor } from '../types';
|
||||
*/
|
||||
export function isChildClass <P extends Constructor> (Parent: P, Child?: Constructor | null | unknown): Child is P {
|
||||
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
|
||||
return Child
|
||||
return isClass(Child) && isClass(Parent)
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
? Parent === Child || Parent.isPrototypeOf(Child as Constructor)
|
||||
? Parent === Child || Parent.isPrototypeOf(Child)
|
||||
: false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BN } from '../bn';
|
||||
import { isClass } from '.';
|
||||
|
||||
describe('isClass', (): void => {
|
||||
it('returns false on a pure object', (): void => {
|
||||
expect(isClass({})).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on instance values', (): void => {
|
||||
expect(
|
||||
isClass(new BN(1))
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true when a class value', (): void => {
|
||||
expect(
|
||||
isClass(BN)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on extends', (): void => {
|
||||
expect(
|
||||
isClass(class Test extends BN {})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on built-ins', (): void => {
|
||||
expect(
|
||||
isClass(Uint8Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2023 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Constructor } from '../types';
|
||||
|
||||
import { isOnFunction } from './helpers';
|
||||
|
||||
/**
|
||||
* @name isClass
|
||||
* Tests if the supplied agrument is a Class
|
||||
*/
|
||||
export const isClass: <T extends Constructor> (value?: unknown) => value is T = /*#__PURE__*/ isOnFunction('isPrototypeOf', 'hasOwnProperty');
|
||||
@@ -10,6 +10,12 @@ export function isOn <T> (...fns: (keyof T)[]): (value?: unknown) => value is T
|
||||
fns.every((f) => isFunction((value as T)[f]));
|
||||
}
|
||||
|
||||
export function isOnFunction <T> (...fns: (keyof T)[]): (value?: unknown) => value is T {
|
||||
return (value?: unknown): value is T =>
|
||||
isFunction(value) &&
|
||||
fns.every((f) => isFunction((value as T)[f]));
|
||||
}
|
||||
|
||||
export function isOnObject <T> (...fns: (keyof T)[]): (value?: unknown) => value is T {
|
||||
return (value?: unknown): value is T =>
|
||||
isObject(value) &&
|
||||
|
||||
@@ -11,6 +11,7 @@ export { isBn } from './bn';
|
||||
export { isBuffer } from './buffer';
|
||||
export { isBoolean } from './boolean';
|
||||
export { isChildClass } from './childClass';
|
||||
export { isClass } from './class';
|
||||
export { isCodec } from './codec';
|
||||
export { isCompact } from './compact';
|
||||
export { isError } from './error';
|
||||
|
||||
@@ -7,6 +7,10 @@ import type { BN } from './bn/bn';
|
||||
export interface Constructor<T = any> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
new(...value: any[]): T;
|
||||
|
||||
hasOwnProperty (prop: string): boolean;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
isPrototypeOf (other: any): boolean;
|
||||
}
|
||||
|
||||
export interface ToBigInt {
|
||||
|
||||
Reference in New Issue
Block a user