Add isClass helper (#1720)

* Add isClass helper

* add isOnFunction helper
This commit is contained in:
Jaco
2023-02-03 09:59:18 +02:00
committed by GitHub
parent e5c2f8f529
commit e29003f51e
11 changed files with 413 additions and 222 deletions
+4 -2
View File
@@ -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;
}
+35
View File
@@ -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);
});
});
+12
View File
@@ -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');
+6
View File
@@ -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) &&
+1
View File
@@ -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';
+4
View File
@@ -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 {