isPromise utility (#1308)
* isPromise utility * Adjust * Update packages/util/src/is/wasm.ts * Update packages/util/src/is/codec.ts * Adjust
This commit is contained in:
@@ -19,4 +19,22 @@ describe('isCodec', (): void => {
|
||||
})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('is true when invalid codec-like signature is found', (): void => {
|
||||
expect(
|
||||
isCodec({
|
||||
toHex: () => '0x',
|
||||
toU8a: () => new Uint8Array()
|
||||
})
|
||||
).toEqual(false);
|
||||
expect(
|
||||
isCodec({
|
||||
registry: {
|
||||
get: () => null
|
||||
},
|
||||
toHex: 1,
|
||||
toU8a: () => new Uint8Array()
|
||||
})
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { isFunction } from './function';
|
||||
import { isObject } from './object';
|
||||
import { isOnObject } from './helpers';
|
||||
|
||||
interface Registry {
|
||||
get: (...params: unknown[]) => unknown;
|
||||
@@ -17,11 +16,9 @@ interface Codec {
|
||||
toU8a: (isBare?: unknown) => Uint8Array;
|
||||
}
|
||||
|
||||
const checkCodec = isOnObject<Codec>('toHex', 'toU8a');
|
||||
const checkRegistry = isOnObject<Registry>('get');
|
||||
|
||||
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)
|
||||
);
|
||||
return checkCodec(value) && checkRegistry(value.registry);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
|
||||
import type { BN } from '../bn/bn';
|
||||
|
||||
import { isFunction } from './function';
|
||||
import { isObject } from './object';
|
||||
import { isOnObject } from './helpers';
|
||||
|
||||
interface Compact<T> {
|
||||
toBigInt (): bigint;
|
||||
@@ -13,16 +12,12 @@ interface Compact<T> {
|
||||
unwrap (): T;
|
||||
}
|
||||
|
||||
const checker = isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
|
||||
|
||||
/**
|
||||
* @name isCompact
|
||||
* @summary Tests for SCALE-Compact-like object instance.
|
||||
*/
|
||||
export function isCompact <T> (value?: unknown): value is Compact<T> {
|
||||
return (
|
||||
isObject<Compact<T>>(value) &&
|
||||
isFunction(value.toBigInt) &&
|
||||
isFunction(value.toBn) &&
|
||||
isFunction(value.toNumber) &&
|
||||
isFunction(value.unwrap)
|
||||
);
|
||||
return checker(value);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,14 @@
|
||||
import { isFunction } from './function';
|
||||
import { isObject } from './object';
|
||||
|
||||
export function isOn <T> (fn: keyof T): (value?: unknown) => value is T {
|
||||
export function isOn <T> (...fns: (keyof T)[]): (value?: unknown) => value is T {
|
||||
return (value?: unknown): value is T =>
|
||||
!!value && (isObject(value) || isFunction(value)) && isFunction((value as T)[fn]);
|
||||
(isObject(value) || 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) &&
|
||||
fns.every((f) => isFunction((value as T)[f]));
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ export { isNull } from './null';
|
||||
export { isNumber } from './number';
|
||||
export { isObject } from './object';
|
||||
export { isObservable } from './observable';
|
||||
export { isPromise } from './promise';
|
||||
export { isString } from './string';
|
||||
export { isTestChain } from './testChain';
|
||||
export { isToBigInt } from './toBigInt';
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isPromise } from '.';
|
||||
|
||||
describe('isPromise', (): void => {
|
||||
it('returns true on valid Promise (Promise-like)', (): void => {
|
||||
expect(
|
||||
isPromise(Promise.resolve(1))
|
||||
).toEqual(true);
|
||||
expect(
|
||||
isPromise({ catch: () => null, then: () => null })
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid objects', (): void => {
|
||||
expect(
|
||||
isPromise('notAnObservable')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on invalid catch/then functions', (): void => {
|
||||
expect(
|
||||
isPromise({ catch: () => null, then: true })
|
||||
).toEqual(false);
|
||||
expect(
|
||||
isPromise({ catch: true, then: () => null })
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isOnObject } from './helpers';
|
||||
|
||||
export const isPromise = isOnObject<Promise<unknown>>('catch', 'then');
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aEq } from '../u8a/eq';
|
||||
import { isU8a } from './u8a';
|
||||
|
||||
const WASM_MAGIC = new Uint8Array([0, 97, 115, 109]); // \0asm
|
||||
|
||||
@@ -11,6 +12,6 @@ const WASM_MAGIC = new Uint8Array([0, 97, 115, 109]); // \0asm
|
||||
* @description
|
||||
* Checks to see if the input Uint8Array contains a valid WASM header
|
||||
*/
|
||||
export function isWasm (value?: Uint8Array): boolean {
|
||||
return !!value && u8aEq(value.subarray(0, 4), WASM_MAGIC);
|
||||
export function isWasm (value?: unknown): value is Uint8Array {
|
||||
return isU8a(value) && u8aEq(value.subarray(0, 4), WASM_MAGIC);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user