This commit is contained in:
Jaco
2021-11-18 09:57:26 +02:00
committed by GitHub
parent 9abcc72143
commit 66866e1a8e
7 changed files with 66 additions and 8 deletions
+2 -1
View File
@@ -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
+22
View File
@@ -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);
});
});
+27
View File
@@ -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)
);
}
+6 -5
View File
@@ -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)
);
}
+1
View File
@@ -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';
+6
View File
@@ -15,4 +15,10 @@ describe('isObject', (): void => {
isObject('notAnObject')
).toEqual(false);
});
it('returns false on null', (): void => {
expect(
isObject(null)
).toEqual(false);
});
});
+2 -2
View File
@@ -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';
}