isCodec (#1201)
This commit is contained in:
+2
-1
@@ -4,10 +4,11 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `stringPascalCase` utility
|
||||
- Add strongly typed `object{Entries, Keys, Values}` utilities
|
||||
- Add `{hex, u8a}ToBigInt` utilities
|
||||
- Add `bi*` utilities for `bigint` operations (mirror of `bn*`)
|
||||
- Add `stringPascalCase` utility
|
||||
- Add `isCodec` to check for Codec-like structures
|
||||
- Optimize for `hexToU8a` and `u8aToHex` functions
|
||||
- Optimize `u8aEq` to use `DataView` for compares
|
||||
- Space optimization for bip39 wordlists
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isCodec } from '.';
|
||||
|
||||
describe('isCodec', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isCodec()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when codec-like signature is found', (): void => {
|
||||
expect(
|
||||
isCodec({
|
||||
registry: {
|
||||
get: () => null
|
||||
},
|
||||
toHex: () => '0x',
|
||||
toU8a: () => new Uint8Array()
|
||||
})
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { isFunction } from './function';
|
||||
import { isObject } from './object';
|
||||
|
||||
interface Registry {
|
||||
get: (...params: unknown[]) => unknown;
|
||||
}
|
||||
|
||||
interface Codec {
|
||||
readonly registry: Registry;
|
||||
|
||||
toHex (isLe?: boolean): HexString;
|
||||
toU8a: (isBare?: unknown) => Uint8Array;
|
||||
}
|
||||
|
||||
export function isCodec <T extends Codec = Codec> (value?: unknown): value is T {
|
||||
return (
|
||||
isObject<T>(value) &&
|
||||
isFunction(value.toU8a) &&
|
||||
isObject<Registry>(value.registry) &&
|
||||
isFunction(value.registry.get)
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
import type { BN } from '../bn/bn';
|
||||
|
||||
import { isFunction } from './function';
|
||||
import { isObject } from './object';
|
||||
|
||||
interface Compact<T> {
|
||||
toBigInt (): bigint;
|
||||
@@ -18,10 +19,10 @@ interface Compact<T> {
|
||||
*/
|
||||
export function isCompact <T> (value?: unknown): value is Compact<T> {
|
||||
return (
|
||||
!!value &&
|
||||
isFunction((value as Compact<T>).toBigInt) &&
|
||||
isFunction((value as Compact<T>).toBn) &&
|
||||
isFunction((value as Compact<T>).toNumber) &&
|
||||
isFunction((value as Compact<T>).unwrap)
|
||||
isObject<Compact<T>>(value) &&
|
||||
isFunction(value.toBigInt) &&
|
||||
isFunction(value.toBn) &&
|
||||
isFunction(value.toNumber) &&
|
||||
isFunction(value.unwrap)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ export { isBn } from './bn';
|
||||
export { isBuffer } from './buffer';
|
||||
export { isBoolean } from './boolean';
|
||||
export { isChildClass } from './childClass';
|
||||
export { isCodec } from './codec';
|
||||
export { isCompact } from './compact';
|
||||
export { isError } from './error';
|
||||
export { isFunction } from './function';
|
||||
|
||||
@@ -15,4 +15,10 @@ describe('isObject', (): void => {
|
||||
isObject('notAnObject')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on null', (): void => {
|
||||
expect(
|
||||
isObject(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,6 +21,6 @@ interface ObjectIndexed {
|
||||
* isObject('something'); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isObject (value: unknown): value is ObjectIndexed {
|
||||
return typeof value === 'object';
|
||||
export function isObject <T extends ObjectIndexed = ObjectIndexed> (value?: unknown): value is T {
|
||||
return !!value && typeof value === 'object';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user