mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-10-29 21:59:48 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
commit
ec06da0ebc
687 files changed
+48096
No files matched your search
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isArray } from './index.js';
|
||||
|
||||
describe('isArray', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isArray()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on non-array', (): void => {
|
||||
expect(isArray(123)).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when Array is found', (): void => {
|
||||
expect(isArray([1, 2, 3])).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isArray
|
||||
* @summary Tests for a Array instance.
|
||||
*/
|
||||
export function isArray <T> (value?: unknown): value is T[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a } from '../string/index.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { isAscii } from './index.js';
|
||||
|
||||
describe('isAscii', (): void => {
|
||||
it('returns true for an ASCII string', (): void => {
|
||||
expect(isAscii('Hello World! Testing')).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false for an ASCII string (with formatters)', (): void => {
|
||||
expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on an non-ascii string', (): void => {
|
||||
expect(isAscii('你好,世界')).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true for ASCII bytes', (): void => {
|
||||
expect(isAscii(stringToU8a('Hello World! Testing'))).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true for empty string inputs', (): void => {
|
||||
expect(isAscii('')).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true for empty U8a inputs', (): void => {
|
||||
expect(isAscii(new Uint8Array())).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false for empty undefined inputs', (): void => {
|
||||
expect(isAscii()).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false for non-printable characters', (): void => {
|
||||
expect(isAscii(new Uint8Array([5]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false for hex input, non-ascii', (): void => {
|
||||
expect(isAscii('0x010203')).toEqual(false);
|
||||
});
|
||||
|
||||
perf('isAscii (str)', 2_000_000, [['Hello World! Testing']], isAscii);
|
||||
perf('isAscii (u8a)', 200_000, [[[stringToU8a('Hello World! Testing')]]], isAscii);
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString, U8aLike } from '../types.js';
|
||||
|
||||
import { u8aToU8a } from '../u8a/toU8a.js';
|
||||
import { isHex } from './hex.js';
|
||||
import { isString } from './string.js';
|
||||
|
||||
/** @internal */
|
||||
function isAsciiStr (str: AnyString): boolean {
|
||||
for (let i = 0, count = str.length; i < count; i++) {
|
||||
const b = str.charCodeAt(i);
|
||||
|
||||
// check is inlined here, it is faster than making a call
|
||||
if (b < 32 || b > 126) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function isAsciiBytes (u8a: Uint8Array | number[]): boolean {
|
||||
for (let i = 0, count = u8a.length; i < count; i++) {
|
||||
const b = u8a[i] | 0;
|
||||
|
||||
// check is inlined here, it is faster than making a call
|
||||
if (b < 32 || b > 126) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name isAscii
|
||||
* @summary Tests if the input is printable ASCII
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
|
||||
*/
|
||||
export function isAscii (value?: U8aLike | null): boolean {
|
||||
return isString(value)
|
||||
? isHex(value)
|
||||
? isAsciiBytes(u8aToU8a(value))
|
||||
: isAsciiStr(value)
|
||||
: value
|
||||
? isAsciiBytes(value)
|
||||
: false;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isBigInt } from './index.js';
|
||||
|
||||
describe('isBigInt', (): void => {
|
||||
it('returns true with a ...n value', (): void => {
|
||||
expect(
|
||||
isBigInt(123456n)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true with a BigInt(...) value', (): void => {
|
||||
expect(
|
||||
isBigInt(BigInt(123456))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BigInt values', (): void => {
|
||||
expect(
|
||||
isBigInt(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isBigInt
|
||||
* @summary Tests for a `BigInt` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BigInt`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isBigInt } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isBigInt', isBigInt(123_456n)); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isBigInt (value: unknown): value is bigint {
|
||||
return typeof value === 'bigint';
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { isBn } from './index.js';
|
||||
|
||||
describe('isBN', (): void => {
|
||||
it('returns true when a BN value', (): void => {
|
||||
expect(
|
||||
isBn(new BN(123))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', (): void => {
|
||||
expect(
|
||||
isBn(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BN } from '../bn/bn.js';
|
||||
|
||||
/**
|
||||
* @name isBn
|
||||
* @summary Tests for a `BN` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { isBn } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isBn', isBn(new BN(1))); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isBn (value: unknown): value is BN {
|
||||
return BN.isBN(value);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isBoolean } from './index.js';
|
||||
|
||||
describe('isBoolean', (): void => {
|
||||
it('returns true on false', (): void => {
|
||||
expect(
|
||||
isBoolean(false)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on true', (): void => {
|
||||
expect(
|
||||
isBoolean(true)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid booleans', (): void => {
|
||||
expect(
|
||||
isBoolean('notABool')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isBoolean
|
||||
* @summary Tests for a boolean value.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript boolean.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isBoolean } from '@pezkuwi/util';
|
||||
*
|
||||
* isBoolean(false); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isBoolean (value: unknown): value is boolean {
|
||||
return typeof value === 'boolean';
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isBuffer } from './index.js';
|
||||
|
||||
describe('isBuffer', (): void => {
|
||||
it('returns true when a Buffer value', (): void => {
|
||||
expect(
|
||||
isBuffer(Buffer.from([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values (number)', (): void => {
|
||||
expect(
|
||||
isBuffer(123)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values (null)', (): void => {
|
||||
expect(
|
||||
isBuffer(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BufferClass, BufferObject } from '../types.js';
|
||||
|
||||
import { xglobal } from '@pezkuwi/x-global';
|
||||
|
||||
import { hasBuffer } from '../has.js';
|
||||
import { isFunction } from './function.js';
|
||||
|
||||
/**
|
||||
* @name isBuffer
|
||||
* @summary Tests for a `Buffer` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Buffer`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isBuffer } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isBuffer', isBuffer(Buffer.from([]))); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isBuffer <T = BufferObject> (value: unknown): value is T {
|
||||
// we do check a function first, since it is slightly faster than isBuffer itself
|
||||
return hasBuffer && !!value && isFunction((value as unknown as BufferObject).readDoubleLE) && (xglobal.Buffer as unknown as BufferClass).isBuffer(value);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { isChildClass } from './index.js';
|
||||
|
||||
describe('isChildClass', (): void => {
|
||||
it('returns true when a Child value', (): void => {
|
||||
expect(
|
||||
isChildClass(BN, class Test extends BN {})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Child values', (): void => {
|
||||
expect(
|
||||
isChildClass(BN, Uint8Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on undefined', (): void => {
|
||||
expect(
|
||||
isChildClass(BN)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('does TS magic, determining type', (): void => {
|
||||
const T = (class extends BN {}) as unknown;
|
||||
|
||||
expect(
|
||||
isChildClass(BN, T) &&
|
||||
new T(12345).toNumber()
|
||||
).toEqual(12345);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Class } from '../types.js';
|
||||
|
||||
import { isClass } from './class.js';
|
||||
|
||||
/**
|
||||
* @name isChildClass
|
||||
* @summary Tests if the child extends the parent Class
|
||||
* @description
|
||||
* Checks to see if the child Class extends the parent Class
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isChildClass } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isChildClass', isChildClass(BN, BN); // => true
|
||||
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isChildClass <P extends Class> (Parent: P, Child?: unknown): Child is P {
|
||||
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
|
||||
return isClass(Child) && isClass(Parent)
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
? Parent === Child || Parent.isPrototypeOf(Child)
|
||||
: false;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { isClass } from './index.js';
|
||||
|
||||
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-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Class } from '../types.js';
|
||||
|
||||
import { isOnFunction } from './helpers.js';
|
||||
|
||||
/**
|
||||
* @name isClass
|
||||
* Tests if the supplied argument is a Class
|
||||
*/
|
||||
export const isClass: <T extends Class> (value?: unknown) => value is T = /*#__PURE__*/ isOnFunction('isPrototypeOf', 'hasOwnProperty');
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perfCmp } from '../test/index.js';
|
||||
import { isCodec } from './index.js';
|
||||
|
||||
const VALID = {
|
||||
registry: {
|
||||
get: () => null
|
||||
},
|
||||
toHex: () => '0x',
|
||||
toHuman: () => '0x',
|
||||
toU8a: () => new Uint8Array()
|
||||
};
|
||||
|
||||
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(VALID)
|
||||
).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);
|
||||
});
|
||||
|
||||
perfCmp('isCodec', ['isCodec', 'instanceof'], 500_000, [[VALID]], (v: unknown, isSecond) =>
|
||||
isSecond
|
||||
? !(v instanceof String)
|
||||
: isCodec(v)
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
import { isOnObject } from './helpers.js';
|
||||
|
||||
interface Registry {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
get: (...params: never[]) => any;
|
||||
}
|
||||
|
||||
interface Codec {
|
||||
readonly registry: Registry;
|
||||
|
||||
toHex (...params: never[]): HexString;
|
||||
toHuman (...params: never[]): unknown;
|
||||
toU8a: (...params: never[]) => Uint8Array;
|
||||
}
|
||||
|
||||
const checkCodec = /*#__PURE__*/ isOnObject<Codec>('toHex', 'toHuman', 'toU8a');
|
||||
const checkRegistry = /*#__PURE__*/ isOnObject<Registry>('get');
|
||||
|
||||
export function isCodec <T extends Codec = Codec> (value?: unknown): value is T {
|
||||
return checkCodec(value) && checkRegistry(value.registry);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { isCompact } from './index.js';
|
||||
|
||||
describe('isCompact', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isCompact()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on non-compact value', (): void => {
|
||||
expect(isCompact(123)).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when compact-like signature is found', (): void => {
|
||||
expect(isCompact({
|
||||
toBigInt: () => BigInt(1),
|
||||
toBn: () => new BN(1),
|
||||
toNumber: () => 1,
|
||||
unwrap: () => new BN(1)
|
||||
})).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn.js';
|
||||
|
||||
import { isOnObject } from './helpers.js';
|
||||
|
||||
interface Compact<T> {
|
||||
toBigInt (): bigint;
|
||||
toBn (): BN;
|
||||
toNumber (): number;
|
||||
unwrap (): T;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name isCompact
|
||||
* @summary Tests for SCALE-Compact-like object instance.
|
||||
*/
|
||||
export const isCompact: <T> (value?: unknown) => value is Compact<T> = /*#__PURE__*/ isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isError } from './index.js';
|
||||
|
||||
describe('isError', (): void => {
|
||||
it('returns true when an Error value', (): void => {
|
||||
expect(
|
||||
isError(new Error('testing'))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Error values', (): void => {
|
||||
expect(
|
||||
isError(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isError
|
||||
* @summary Tests for a `Error` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Error`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isError } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isError', isError(new Error('message'))); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isError (value: unknown): value is Error {
|
||||
return (
|
||||
((value && (value as Error).constructor) === Error) ||
|
||||
value instanceof Error
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isFunction } from './index.js';
|
||||
|
||||
describe('isFunction', (): void => {
|
||||
it('returns true on valid functions', (): void => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid functions', (): void => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
type FnType = Function;
|
||||
|
||||
/**
|
||||
* @name isFunction
|
||||
* @summary Tests for a `function`.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript function.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isFunction } from '@pezkuwi/util';
|
||||
*
|
||||
* isFunction(() => false); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isFunction (value: unknown): value is FnType {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isFunction } from './function.js';
|
||||
import { isObject } from './object.js';
|
||||
|
||||
export function isOn <T> (...fns: (keyof T)[]): (value?: unknown) => value is T {
|
||||
return (value?: unknown): value is T =>
|
||||
(isObject(value) || isFunction(value)) &&
|
||||
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) &&
|
||||
fns.every((f) => isFunction((value as T)[f]));
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { isHex } from './index.js';
|
||||
|
||||
describe('isHex', (): void => {
|
||||
const test = '1234abcd';
|
||||
const ptest = `0x${'1234567890abcdef'.repeat(8192)}`;
|
||||
|
||||
it('returns true on 0x hex values', (): void => {
|
||||
expect(
|
||||
isHex('0x')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on hex values', (): void => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on hex values with String', (): void => {
|
||||
expect(
|
||||
isHex(String(`0x${test}`))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on hex values (non % 2)', (): void => {
|
||||
expect(
|
||||
isHex(`0x${test}0`)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', (): void => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('return false on hex values unprefixed', (): void => {
|
||||
expect(
|
||||
isHex(test)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-string values', (): void => {
|
||||
expect(
|
||||
isHex(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true when valid hex and bitLength matches', (): void => {
|
||||
expect(
|
||||
isHex('0x1234', 16)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true when valid hex and bitLength does not match', (): void => {
|
||||
expect(
|
||||
isHex('0x1234', 8)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('does ignore lengths as required', (): void => {
|
||||
expect(
|
||||
isHex('0x123')
|
||||
).toEqual(false);
|
||||
expect(
|
||||
isHex('0x123', -1, true)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
perf(`isHex (${(ptest.length - 2) / 2} bytes)`, 1000, [[ptest]], isHex);
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types.js';
|
||||
|
||||
export const REGEX_HEX_PREFIXED = /^0x[\da-fA-F]+$/;
|
||||
|
||||
export const REGEX_HEX_NOPREFIX = /^[\da-fA-F]+$/;
|
||||
|
||||
/**
|
||||
* @name isHex
|
||||
* @summary Tests for a hex string.
|
||||
* @description
|
||||
* Checks to see if the input value is a `0x` prefixed hex string. Optionally (`bitLength` !== -1) checks to see if the bitLength is correct.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isHex } from '@pezkuwi/util';
|
||||
*
|
||||
* isHex('0x1234'); // => true
|
||||
* isHex('0x1234', 8); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isHex (value: unknown, bitLength = -1, ignoreLength?: boolean): value is HexString {
|
||||
return (
|
||||
typeof value === 'string' && (
|
||||
value === '0x' ||
|
||||
REGEX_HEX_PREFIXED.test(value)
|
||||
)
|
||||
) && (
|
||||
bitLength === -1
|
||||
? (ignoreLength || (value.length % 2 === 0))
|
||||
: (value.length === (2 + Math.ceil(bitLength / 4)))
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Type checking utilities
|
||||
*/
|
||||
export { isArray } from './array.js';
|
||||
export { isAscii } from './ascii.js';
|
||||
export { isBigInt } from './bigInt.js';
|
||||
export { isBn } from './bn.js';
|
||||
export { isBoolean } from './boolean.js';
|
||||
export { isBuffer } from './buffer.js';
|
||||
export { isChildClass } from './childClass.js';
|
||||
export { isClass } from './class.js';
|
||||
export { isCodec } from './codec.js';
|
||||
export { isCompact } from './compact.js';
|
||||
export { isError } from './error.js';
|
||||
export { isFunction } from './function.js';
|
||||
export { isHex } from './hex.js';
|
||||
export { isInstanceOf } from './instanceOf.js';
|
||||
export { isIp } from './ip.js';
|
||||
export { isJsonObject } from './jsonObject.js';
|
||||
export { isNull } from './null.js';
|
||||
export { isNumber } from './number.js';
|
||||
export { isObject } from './object.js';
|
||||
export { isObservable } from './observable.js';
|
||||
export { isPromise } from './promise.js';
|
||||
export { isRiscV } from './riscv.js';
|
||||
export { isString } from './string.js';
|
||||
export { isTestChain } from './testChain.js';
|
||||
export { isToBigInt } from './toBigInt.js';
|
||||
export { isToBn } from './toBn.js';
|
||||
export { isU8a } from './u8a.js';
|
||||
export { isUndefined } from './undefined.js';
|
||||
export { isUtf8 } from './utf8.js';
|
||||
export { isWasm } from './wasm.js';
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isInstanceOf } from './index.js';
|
||||
|
||||
describe('isInstanceOf', (): void => {
|
||||
it('returns true on real instances', (): void => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on real instances (inherited)', (): void => {
|
||||
class Test extends Array {
|
||||
// default
|
||||
}
|
||||
|
||||
expect(
|
||||
isInstanceOf(new Test(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', (): void => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-instances', (): void => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when class not specified', (): void => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on wrong class type', (): void => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), String)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isInstanceOf
|
||||
* @summary Tests for a instance of a class.
|
||||
* @description
|
||||
* Checks to see if the input value is an instance of the test class.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isInstanceOf } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function isInstanceOf (value: unknown, Clazz: Function): boolean {
|
||||
return (
|
||||
((value && (value as Record<string, unknown>).constructor) === Clazz) ||
|
||||
value instanceof Clazz
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isIp } from './index.js';
|
||||
|
||||
describe('isIp', (): void => {
|
||||
it('returns false when non-ip found', (): void => {
|
||||
expect(
|
||||
isIp('not.an.ip.1')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on IPv4 addresses', (): void => {
|
||||
expect(
|
||||
isIp('192.168.0.1')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on IPv4 addresses (IPV4 type)', (): void => {
|
||||
expect(
|
||||
isIp('192.168.0.1', 'v4')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on IPv4 addresses (IPv6 type)', (): void => {
|
||||
expect(
|
||||
isIp('192.168.0.1', 'v6')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on IPv6 addresses', (): void => {
|
||||
expect(
|
||||
isIp('1:2:3:4:5:6:7:8')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on IPv6 addresses (IPv6 type)', (): void => {
|
||||
expect(
|
||||
isIp('1:2:3:4:5:6:7:8', 'v6')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on IPv6 addresses (IPv4 type)', (): void => {
|
||||
expect(
|
||||
isIp('1:2:3:4:5:6:7:8', 'v4')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// The regex patterns below were copied as-is from the ip-regex package 5.0.0,
|
||||
// https://github.com/sindresorhus/ip-regex/blob/a2a44dfa7f776528158c2a5ff9d8a1be435ec1b9/index.js#L1
|
||||
//
|
||||
// MIT License
|
||||
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
//
|
||||
// Changes made:
|
||||
// - boundary support option has been dropped
|
||||
// - only exact matching is used (isIp always passed exact: true)
|
||||
// - the newest ip-regex is ESM-only, we compile to all platforms
|
||||
const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}';
|
||||
const v6s = '[a-fA-F\\d]{1,4}';
|
||||
const v6 = `
|
||||
(?:
|
||||
(?:${v6s}:){7}(?:${v6s}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
|
||||
(?:${v6s}:){6}(?:${v4}|:${v6s}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
|
||||
(?:${v6s}:){5}(?::${v4}|(?::${v6s}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
|
||||
(?:${v6s}:){4}(?:(?::${v6s}){0,1}:${v4}|(?::${v6s}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
|
||||
(?:${v6s}:){3}(?:(?::${v6s}){0,2}:${v4}|(?::${v6s}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
|
||||
(?:${v6s}:){2}(?:(?::${v6s}){0,3}:${v4}|(?::${v6s}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
|
||||
(?:${v6s}:){1}(?:(?::${v6s}){0,4}:${v4}|(?::${v6s}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
|
||||
(?::(?:(?::${v6s}){0,5}:${v4}|(?::${v6s}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
|
||||
)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
|
||||
`.replace(/\s*\/\/.*$/gm, '').replace(/\n/g, '').trim();
|
||||
|
||||
const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
|
||||
const v4exact = new RegExp(`^${v4}$`);
|
||||
const v6exact = new RegExp(`^${v6}$`);
|
||||
|
||||
/**
|
||||
* @name isIp
|
||||
* @summary Tests if the value is a valid IP address
|
||||
* @description
|
||||
* Checks to see if the value is a valid IP address. Optionally check for either v4/v6
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isIp } from '@pezkuwi/util';
|
||||
*
|
||||
* isIp('192.168.0.1')); // => true
|
||||
* isIp('1:2:3:4:5:6:7:8'); // => true
|
||||
* isIp('192.168.0.1', 'v6')); // => false
|
||||
* isIp('1:2:3:4:5:6:7:8', 'v4'); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isIp (value: string, type?: 'v4' | 'v6'): boolean {
|
||||
switch (type) {
|
||||
case 'v4': return v4exact.test(value);
|
||||
case 'v6': return v6exact.test(value);
|
||||
default: return v46Exact.test(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isJsonObject } from './index.js';
|
||||
|
||||
const jsonObject = `{
|
||||
"Test": "1234",
|
||||
"NestedTest": {
|
||||
"Test": "5678"
|
||||
}
|
||||
}`;
|
||||
|
||||
describe('isJsonObject', (): void => {
|
||||
it('returns true on empty objects', (): void => {
|
||||
expect(
|
||||
isJsonObject({})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on JSON objects', (): void => {
|
||||
expect(
|
||||
isJsonObject(jsonObject)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on JSON parsable value typeof number', (): void => {
|
||||
expect(
|
||||
isJsonObject(1234)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on JSON parsable value null', (): void => {
|
||||
expect(
|
||||
isJsonObject(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on invalid objects', (): void => {
|
||||
expect(
|
||||
isJsonObject('notAnObject')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on invalid JSON', (): void => {
|
||||
expect(
|
||||
isJsonObject('{"abc", "def"}')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringify } from '../stringify.js';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ObjectIndexed = Record<string, any>;
|
||||
|
||||
/**
|
||||
* @name isJsonObject
|
||||
* @summary Tests for a valid JSON `object`.
|
||||
* @description
|
||||
* Checks to see if the input value is a valid JSON object.
|
||||
* It returns false if the input is JSON parsable, but not an Javascript object.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isJsonObject } from '@pezkuwi/util';
|
||||
*
|
||||
* isJsonObject({}); // => true
|
||||
* isJsonObject({
|
||||
* "Test": "1234",
|
||||
* "NestedTest": {
|
||||
* "Test": "5678"
|
||||
* }
|
||||
* }); // => true
|
||||
* isJsonObject(1234); // JSON parsable, but not an object => false
|
||||
* isJsonObject(null); // JSON parsable, but not an object => false
|
||||
* isJsonObject('not an object'); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isJsonObject (value: unknown): value is ObjectIndexed {
|
||||
const str = typeof value !== 'string'
|
||||
? stringify(value)
|
||||
: value;
|
||||
|
||||
try {
|
||||
const obj = JSON.parse(str) as unknown;
|
||||
|
||||
return typeof obj === 'object' && obj !== null;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isNull } from './index.js';
|
||||
|
||||
describe('isNull', (): void => {
|
||||
it('returns true when a null value', (): void => {
|
||||
expect(
|
||||
isNull(null)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-null values', (): void => {
|
||||
expect(
|
||||
isNull()
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isNull
|
||||
* @summary Tests for a `null` values.
|
||||
* @description
|
||||
* Checks to see if the input value is `null`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isNull } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isNull', isNull(null)); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isNull (value?: unknown): value is null {
|
||||
return value === null;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isNumber } from './index.js';
|
||||
|
||||
describe('isNumber', (): void => {
|
||||
it('returns true on valid numbers', (): void => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', (): void => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isNumber
|
||||
* @summary Tests for a JavaScript number.
|
||||
* @description
|
||||
* Checks to see if the input value is a valid number.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isNumber } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isNumber', isNumber(1234)); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isNumber (value: unknown): value is number {
|
||||
return typeof value === 'number';
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isObject } from './index.js';
|
||||
|
||||
describe('isObject', (): void => {
|
||||
it('returns true on valid objects', (): void => {
|
||||
expect(
|
||||
isObject({})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid objects', (): void => {
|
||||
expect(
|
||||
isObject('notAnObject')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on null', (): void => {
|
||||
expect(
|
||||
isObject(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on bigint', (): void => {
|
||||
expect(
|
||||
isObject(123n)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ObjectIndexed = Record<string, any>;
|
||||
|
||||
/**
|
||||
* @name isObject
|
||||
* @summary Tests for an `object`.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript object.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isObject } from '@pezkuwi/util';
|
||||
*
|
||||
* isObject({}); // => true
|
||||
* isObject('something'); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isObject <T extends ObjectIndexed = ObjectIndexed> (value?: unknown): value is T {
|
||||
return !!value && typeof value === 'object';
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isObservable } from './index.js';
|
||||
|
||||
describe('isObservable', (): void => {
|
||||
it('returns true on valid observables', (): void => {
|
||||
expect(
|
||||
isObservable({ next: (): boolean => true })
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid objects', (): void => {
|
||||
expect(
|
||||
isObservable('notAnObservable')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on invalid next functions', (): void => {
|
||||
expect(
|
||||
isObservable({ next: true })
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from '../types.js';
|
||||
|
||||
import { isOn } from './helpers.js';
|
||||
|
||||
/**
|
||||
* @name isBObservable
|
||||
* @summary Tests for a `Observable` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isObservable } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isObservable', isObservable(...));
|
||||
* ```
|
||||
*/
|
||||
export const isObservable = /*#__PURE__*/ isOn<Observable>('next');
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isPromise } from './index.js';
|
||||
|
||||
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-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isOnObject } from './helpers.js';
|
||||
|
||||
export const isPromise = /*#__PURE__*/ isOnObject<Promise<unknown>>('catch', 'then');
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isRiscV } from './index.js';
|
||||
|
||||
describe('isRiscV', (): void => {
|
||||
it('is false on non-elf/non-pvm header', (): void => {
|
||||
expect(isRiscV(new Uint8Array([0, 97, 115, 109, 1, 2, 3]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on partial-elf/pvm header', (): void => {
|
||||
expect(isRiscV(new Uint8Array([0x7f, 0x45, 0x4c]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on empty byte before elf/pvm header', (): void => {
|
||||
expect(
|
||||
isRiscV(
|
||||
new Uint8Array([0x00, 0x7f, 0x45, 0x4c, 0x46, 0xff, 0x00, 0x42, 0x23])
|
||||
)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when a elf header is found', (): void => {
|
||||
expect(
|
||||
isRiscV(new Uint8Array([0x7f, 0x45, 0x4c, 0x46, 0xff, 0x00, 0x42, 0x23]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('is true when a pvm header is found', (): void => {
|
||||
expect(
|
||||
isRiscV(new Uint8Array([0x50, 0x56, 0x4d, 0x00, 0xff, 0x00, 0x42, 0x23]))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aEq } from '../u8a/eq.js';
|
||||
import { isU8a } from './u8a.js';
|
||||
|
||||
// general elf header
|
||||
const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F'
|
||||
|
||||
// contract-specific PVM header
|
||||
const PVM_MAGIC = new Uint8Array([0x50, 0x56, 0x4d, 0x00]); // 'P', 'V', 'M', 0x00
|
||||
|
||||
/**
|
||||
* @name isRiscV
|
||||
* @summary Tests if the input has a RISC-V header
|
||||
* @description
|
||||
* Checks to see if the input Uint8Array contains a valid RISC-V header
|
||||
*/
|
||||
export function isRiscV (bytes: unknown): bytes is Uint8Array {
|
||||
if (isU8a(bytes)) {
|
||||
const start = bytes.subarray(0, 4);
|
||||
|
||||
return u8aEq(start, PVM_MAGIC) || u8aEq(start, ELF_MAGIC);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isString } from './index.js';
|
||||
|
||||
describe('isString', (): void => {
|
||||
it('returns true on valid strings', (): void => {
|
||||
expect(
|
||||
isString('123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on empty strings', (): void => {
|
||||
expect(
|
||||
isString('')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on String object', (): void => {
|
||||
expect(
|
||||
isString(String('foo'))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', (): void => {
|
||||
expect(
|
||||
isString(2)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString } from '../types.js';
|
||||
|
||||
/**
|
||||
* @name isString
|
||||
* @summary Tests for a string.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript string.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isString } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isString', isString('test')); // => true
|
||||
* ```
|
||||
*/
|
||||
export function isString (value: unknown): value is AnyString {
|
||||
return typeof value === 'string' || value instanceof String;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isTestChain } from './index.js';
|
||||
|
||||
describe('isTestChain', (): void => {
|
||||
it('enables test environment when chain specification matches text of dev or loc(al)', (): void => {
|
||||
const validTestModeChainSpecsWithDev = ['Development'];
|
||||
const validTestModeChainSpecsWithLoc = ['Local Testnet'];
|
||||
|
||||
validTestModeChainSpecsWithDev.concat(validTestModeChainSpecsWithLoc).forEach((s): void => {
|
||||
expect(isTestChain(s)).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('disables keyring test mode when chain specification is not a test mode or undefined or number type', (): void => {
|
||||
const invalidTestModeChainSpecs = ['dev', 'local', 'development', 'PoC-1 Testnet', 'Staging Testnet', 'future PoC-2 Testnet', 'a pocadot?', undefined];
|
||||
|
||||
invalidTestModeChainSpecs.forEach((s): void => {
|
||||
expect(isTestChain(s)).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const REGEX_DEV = /(Development|Local Testnet)$/;
|
||||
|
||||
export function isTestChain (chain?: string | null): boolean {
|
||||
if (!chain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!REGEX_DEV.test(chain.toString());
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isToBigInt } from './index.js';
|
||||
|
||||
describe('isToBigInt', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isToBigInt()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on non-compact value', (): void => {
|
||||
expect(isToBigInt(123)).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when compact-like signature is found', (): void => {
|
||||
expect(isToBigInt({
|
||||
toBigInt: () => BigInt(1)
|
||||
})).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ToBigInt } from '../types.js';
|
||||
|
||||
import { isOn } from './helpers.js';
|
||||
|
||||
export const isToBigInt = /*#__PURE__*/ isOn<ToBigInt>('toBigInt');
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { isToBn } from './index.js';
|
||||
|
||||
describe('isToBn', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isToBn()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on non-compact value', (): void => {
|
||||
expect(isToBn(123)).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when compact-like signature is found', (): void => {
|
||||
expect(isToBn({
|
||||
toBigInt: () => BigInt(1),
|
||||
toBn: () => new BN(1)
|
||||
})).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ToBn } from '../types.js';
|
||||
|
||||
import { isOn } from './helpers.js';
|
||||
|
||||
export const isToBn = /*#__PURE__*/ isOn<ToBn>('toBn');
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { isU8a } from './index.js';
|
||||
|
||||
describe('isU8a', (): void => {
|
||||
it('returns false on undefined values', (): void => {
|
||||
expect(
|
||||
isU8a()
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on null values', (): void => {
|
||||
expect(
|
||||
isU8a(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on Array values', (): void => {
|
||||
expect(
|
||||
isU8a([1, 2, 3])
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on Buffer values', (): void => {
|
||||
// under Node, Buffer implements Uint8Array
|
||||
expect(
|
||||
isU8a(Buffer.from([1, 2, 3]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on Uint8Array values', (): void => {
|
||||
expect(
|
||||
isU8a(new Uint8Array())
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
perf('isU8a', 2_000_000, [[new Uint8Array()]], isU8a);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isU8a
|
||||
* @summary Tests for a `Uint8Array` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Uint8Array`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isUint8Array } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isU8a', isU8a([])); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isU8a (value?: unknown): value is Uint8Array {
|
||||
// here we defer the instanceof check which is actually slightly
|
||||
// slower than just checking the constrctor (direct instances)
|
||||
return (
|
||||
((value && (value as Uint8Array).constructor) === Uint8Array) ||
|
||||
value instanceof Uint8Array
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { isUndefined } from './index.js';
|
||||
|
||||
describe('isUndefined', (): void => {
|
||||
it('returns true on undefined values', (): void => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on defined values', (): void => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
perf('isUndefined', 10_000_000, [[null]], isUndefined);
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name isUndefined
|
||||
* @summary Tests for a `undefined` values.
|
||||
* @description
|
||||
* Checks to see if the input value is `undefined`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isUndefined } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('isUndefined', isUndefined(void(0))); // => true
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function isUndefined (value?: unknown): value is undefined {
|
||||
return value === undefined;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isUtf8 } from './index.js';
|
||||
|
||||
describe('isUtf8', (): void => {
|
||||
it('returns true for an ASCII string', (): void => {
|
||||
expect(isUtf8('Hello\tWorld!\n\rTesting')).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true for ASCII bytes', (): void => {
|
||||
expect(isUtf8(new Uint8Array([0x31, 0x32, 0x20, 10]))).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true for empty string inputs', (): void => {
|
||||
expect(isUtf8('')).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true for empty U8a inputs', (): void => {
|
||||
expect(isUtf8(new Uint8Array())).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false for empty undefined inputs', (): void => {
|
||||
expect(isUtf8()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is valid for ru', (): void => {
|
||||
expect(isUtf8('Приветствую, ми')).toEqual(true);
|
||||
});
|
||||
|
||||
it('is valid for zh', (): void => {
|
||||
expect(isUtf8('你好')).toEqual(true);
|
||||
});
|
||||
|
||||
it('is invalid for something random', (): void => {
|
||||
expect(isUtf8('0x7f07b1f87709608bee603bbc79a0dfc29cd315c1351a83aa31adf7458d7d3003')).toEqual(false);
|
||||
expect(isUtf8('0xc9ec51351beec59b52db06b3dd4685004013035df080358c319553ced597ca05')).toEqual(false);
|
||||
expect(isUtf8('0xd12cef06e90a05b63737d542aaee1389ff9f5c25e461b362180f025c3b2d5635')).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,207 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Adapted from https://github.com/JulienPalard/is_utf8/blob/master/is_utf8.c
|
||||
|
||||
import { u8aToU8a } from '../u8a/toU8a.js';
|
||||
import { isString } from './string.js';
|
||||
|
||||
/**
|
||||
* @name isUtf8
|
||||
* @summary Tests if the input is valid Utf8
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is valid Utf8
|
||||
*/
|
||||
export function isUtf8 (value?: number[] | Uint8Array | string | null): boolean {
|
||||
if (!value) {
|
||||
return isString(value);
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
const len = u8a.length;
|
||||
let i = 0;
|
||||
|
||||
while (i < len) {
|
||||
if (u8a[i] <= 0x7F) /* 00..7F */ {
|
||||
i += 1;
|
||||
} else if (u8a[i] >= 0xC2 && u8a[i] <= 0xDF) /* C2..DF 80..BF */ {
|
||||
if (i + 1 < len) /* Expect a 2nd byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte between C2 and DF, expecting a 2nd byte between 80 and BF";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte between C2 and DF, expecting a 2nd byte.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 2;
|
||||
} else if (u8a[i] === 0xE0) /* E0 A0..BF 80..BF */ {
|
||||
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||
if (u8a[i + 1] < 0xA0 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte of E0, expecting a 2nd byte between A0 and BF.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte of E0, expecting a 3nd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte of E0, expecting two following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 3;
|
||||
} else if (u8a[i] >= 0xE1 && u8a[i] <= 0xEC) /* E1..EC 80..BF 80..BF */ {
|
||||
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte between E1 and EC, expecting the 2nd byte between 80 and BF.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte between E1 and EC, expecting the 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte between E1 and EC, expecting two following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 3;
|
||||
} else if (u8a[i] === 0xED) /* ED 80..9F 80..BF */ {
|
||||
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x9F) {
|
||||
// *message = "After a first byte of ED, expecting 2nd byte between 80 and 9F.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte of ED, expecting 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte of ED, expecting two following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 3;
|
||||
} else if (u8a[i] >= 0xEE && u8a[i] <= 0xEF) /* EE..EF 80..BF 80..BF */ {
|
||||
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte between EE and EF, expecting 2nd byte between 80 and BF.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte between EE and EF, expecting 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte between EE and EF, two following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 3;
|
||||
} else if (u8a[i] === 0xF0) /* F0 90..BF 80..BF 80..BF */ {
|
||||
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||
if (u8a[i + 1] < 0x90 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte of F0, expecting 2nd byte between 90 and BF.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte of F0, expecting 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||
// *message = "After a first byte of F0, expecting 4th byte between 80 and BF.";
|
||||
// *faulty_bytes = 4;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte of F0, expecting three following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 4;
|
||||
} else if (u8a[i] >= 0xF1 && u8a[i] <= 0xF3) /* F1..F3 80..BF 80..BF 80..BF */ {
|
||||
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||
// *message = "After a first byte of F1, F2, or F3, expecting a 2nd byte between 80 and BF.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte of F1, F2, or F3, expecting a 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||
// *message = "After a first byte of F1, F2, or F3, expecting a 4th byte between 80 and BF.";
|
||||
// *faulty_bytes = 4;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte of F1, F2, or F3, expecting three following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 4;
|
||||
} else if (u8a[i] === 0xF4) /* F4 80..8F 80..BF 80..BF */ {
|
||||
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x8F) {
|
||||
// *message = "After a first byte of F4, expecting 2nd byte between 80 and 8F.";
|
||||
// *faulty_bytes = 2;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||
// *message = "After a first byte of F4, expecting 3rd byte between 80 and BF.";
|
||||
// *faulty_bytes = 3;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||
// *message = "After a first byte of F4, expecting 4th byte between 80 and BF.";
|
||||
// *faulty_bytes = 4;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// *message = "After a first byte of F4, expecting three following bytes.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
i += 4;
|
||||
} else {
|
||||
// *message = "Expecting bytes in the following ranges: 00..7F C2..F4.";
|
||||
// *faulty_bytes = 1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { isWasm } from './index.js';
|
||||
|
||||
describe('isWasm', (): void => {
|
||||
it('is false on no value', (): void => {
|
||||
expect(isWasm()).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false on non-wasm header', (): void => {
|
||||
expect(isWasm(new Uint8Array([1, 2, 3]))).toEqual(false);
|
||||
});
|
||||
|
||||
it('is true when a wasm header is found', (): void => {
|
||||
expect(isWasm(new Uint8Array([0, 97, 115, 109, 1, 2, 3]))).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aEq } from '../u8a/eq.js';
|
||||
import { isU8a } from './u8a.js';
|
||||
|
||||
const WASM_MAGIC = new Uint8Array([0, 97, 115, 109]); // \0asm
|
||||
|
||||
/**
|
||||
* @name isWasm
|
||||
* @summary Tests if the input has a WASM header
|
||||
* @description
|
||||
* Checks to see if the input Uint8Array contains a valid WASM header
|
||||
*/
|
||||
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