mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-28 17:57:57 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* @name isArray
|
||||
* @summary Tests for a Array instance.
|
||||
*/
|
||||
export declare function isArray<T>(value?: unknown): value is T[];
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @name isArray
|
||||
* @summary Tests for a Array instance.
|
||||
*/
|
||||
export function isArray(value) {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
import type { U8aLike } from '../types.js';
|
||||
/**
|
||||
* @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 declare function isAscii(value?: U8aLike | null): boolean;
|
||||
@@ -0,0 +1,40 @@
|
||||
import { u8aToU8a } from '../u8a/toU8a.js';
|
||||
import { isHex } from './hex.js';
|
||||
import { isString } from './string.js';
|
||||
/** @internal */
|
||||
function isAsciiStr(str) {
|
||||
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) {
|
||||
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) {
|
||||
return isString(value)
|
||||
? isHex(value)
|
||||
? isAsciiBytes(u8aToU8a(value))
|
||||
: isAsciiStr(value)
|
||||
: value
|
||||
? isAsciiBytes(value)
|
||||
: false;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isBigInt(value: unknown): value is bigint;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return typeof value === 'bigint';
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
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 declare function isBn(value: unknown): value is BN;
|
||||
@@ -0,0 +1,19 @@
|
||||
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) {
|
||||
return BN.isBN(value);
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isBoolean(value: unknown): value is boolean;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return typeof value === 'boolean';
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { BufferObject } from '../types.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 declare function isBuffer<T = BufferObject>(value: unknown): value is T;
|
||||
@@ -0,0 +1,21 @@
|
||||
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(value) {
|
||||
// we do check a function first, since it is slightly faster than isBuffer itself
|
||||
return hasBuffer && !!value && isFunction(value.readDoubleLE) && xglobal.Buffer.isBuffer(value);
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
import type { Class } from '../types.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 declare function isChildClass<P extends Class>(Parent: P, Child?: unknown): Child is P;
|
||||
@@ -0,0 +1,23 @@
|
||||
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(Parent, Child) {
|
||||
// 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;
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import type { Class } from '../types.js';
|
||||
/**
|
||||
* @name isClass
|
||||
* Tests if the supplied argument is a Class
|
||||
*/
|
||||
export declare const isClass: <T extends Class>(value?: unknown) => value is T;
|
||||
@@ -0,0 +1,6 @@
|
||||
import { isOnFunction } from './helpers.js';
|
||||
/**
|
||||
* @name isClass
|
||||
* Tests if the supplied argument is a Class
|
||||
*/
|
||||
export const isClass = /*#__PURE__*/ isOnFunction('isPrototypeOf', 'hasOwnProperty');
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
import type { HexString } from '../types.js';
|
||||
interface Registry {
|
||||
get: (...params: never[]) => any;
|
||||
}
|
||||
interface Codec {
|
||||
readonly registry: Registry;
|
||||
toHex(...params: never[]): HexString;
|
||||
toHuman(...params: never[]): unknown;
|
||||
toU8a: (...params: never[]) => Uint8Array;
|
||||
}
|
||||
export declare function isCodec<T extends Codec = Codec>(value?: unknown): value is T;
|
||||
export {};
|
||||
@@ -0,0 +1,6 @@
|
||||
import { isOnObject } from './helpers.js';
|
||||
const checkCodec = /*#__PURE__*/ isOnObject('toHex', 'toHuman', 'toU8a');
|
||||
const checkRegistry = /*#__PURE__*/ isOnObject('get');
|
||||
export function isCodec(value) {
|
||||
return checkCodec(value) && checkRegistry(value.registry);
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
interface Compact<T> {
|
||||
toBigInt(): bigint;
|
||||
toBn(): BN;
|
||||
toNumber(): number;
|
||||
unwrap(): T;
|
||||
}
|
||||
/**
|
||||
* @name isCompact
|
||||
* @summary Tests for SCALE-Compact-like object instance.
|
||||
*/
|
||||
export declare const isCompact: <T>(value?: unknown) => value is Compact<T>;
|
||||
export {};
|
||||
@@ -0,0 +1,6 @@
|
||||
import { isOnObject } from './helpers.js';
|
||||
/**
|
||||
* @name isCompact
|
||||
* @summary Tests for SCALE-Compact-like object instance.
|
||||
*/
|
||||
export const isCompact = /*#__PURE__*/ isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isError(value: unknown): value is Error;
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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) {
|
||||
return (((value && value.constructor) === Error) ||
|
||||
value instanceof Error);
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
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 declare function isFunction(value: unknown): value is FnType;
|
||||
export {};
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export declare function isOn<T>(...fns: (keyof T)[]): (value?: unknown) => value is T;
|
||||
export declare function isOnFunction<T>(...fns: (keyof T)[]): (value?: unknown) => value is T;
|
||||
export declare function isOnObject<T>(...fns: (keyof T)[]): (value?: unknown) => value is T;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { isFunction } from './function.js';
|
||||
import { isObject } from './object.js';
|
||||
export function isOn(...fns) {
|
||||
return (value) => (isObject(value) || isFunction(value)) &&
|
||||
fns.every((f) => isFunction(value[f]));
|
||||
}
|
||||
export function isOnFunction(...fns) {
|
||||
return (value) => isFunction(value) &&
|
||||
fns.every((f) => isFunction(value[f]));
|
||||
}
|
||||
export function isOnObject(...fns) {
|
||||
return (value) => isObject(value) &&
|
||||
fns.every((f) => isFunction(value[f]));
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
import type { HexString } from '../types.js';
|
||||
export declare const REGEX_HEX_PREFIXED: RegExp;
|
||||
export declare const REGEX_HEX_NOPREFIX: RegExp;
|
||||
/**
|
||||
* @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 declare function isHex(value: unknown, bitLength?: number, ignoreLength?: boolean): value is HexString;
|
||||
@@ -0,0 +1,23 @@
|
||||
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, bitLength = -1, ignoreLength) {
|
||||
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))));
|
||||
}
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @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,33 @@
|
||||
/**
|
||||
* @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';
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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
|
||||
* ```
|
||||
*/
|
||||
export declare function isInstanceOf(value: unknown, Clazz: Function): boolean;
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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
|
||||
* ```
|
||||
*/
|
||||
export function isInstanceOf(value, Clazz) {
|
||||
return (((value && value.constructor) === Clazz) ||
|
||||
value instanceof Clazz);
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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 declare function isIp(value: string, type?: 'v4' | 'v6'): boolean;
|
||||
@@ -0,0 +1,41 @@
|
||||
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, type) {
|
||||
switch (type) {
|
||||
case 'v4': return v4exact.test(value);
|
||||
case 'v6': return v6exact.test(value);
|
||||
default: return v46Exact.test(value);
|
||||
}
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
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 declare function isJsonObject(value: unknown): value is ObjectIndexed;
|
||||
export {};
|
||||
@@ -0,0 +1,37 @@
|
||||
import { stringify } from '../stringify.js';
|
||||
/**
|
||||
* @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) {
|
||||
const str = typeof value !== 'string'
|
||||
? stringify(value)
|
||||
: value;
|
||||
try {
|
||||
const obj = JSON.parse(str);
|
||||
return typeof obj === 'object' && obj !== null;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isNull(value?: unknown): value is null;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return value === null;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isNumber(value: unknown): value is number;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return typeof value === 'number';
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
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 declare function isObject<T extends ObjectIndexed = ObjectIndexed>(value?: unknown): value is T;
|
||||
export {};
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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(value) {
|
||||
return !!value && typeof value === 'object';
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { Observable } from '../types.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 declare const isObservable: (value?: unknown) => value is Observable;
|
||||
@@ -0,0 +1,16 @@
|
||||
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('next');
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export declare const isPromise: (value?: unknown) => value is Promise<unknown>;
|
||||
@@ -0,0 +1,2 @@
|
||||
import { isOnObject } from './helpers.js';
|
||||
export const isPromise = /*#__PURE__*/ isOnObject('catch', 'then');
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @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 declare function isRiscV(bytes: unknown): bytes is Uint8Array;
|
||||
@@ -0,0 +1,17 @@
|
||||
import { u8aEq } from '../u8a/eq.js';
|
||||
import { isU8a } from './u8a.js';
|
||||
const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F'
|
||||
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) {
|
||||
if (isU8a(bytes)) {
|
||||
const start = bytes.subarray(0, 4);
|
||||
return u8aEq(start, PVM_MAGIC) || u8aEq(start, ELF_MAGIC);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
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 declare function isString(value: unknown): value is AnyString;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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) {
|
||||
return typeof value === 'string' || value instanceof String;
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export declare function isTestChain(chain?: string | null): boolean;
|
||||
@@ -0,0 +1,7 @@
|
||||
const REGEX_DEV = /(Development|Local Testnet)$/;
|
||||
export function isTestChain(chain) {
|
||||
if (!chain) {
|
||||
return false;
|
||||
}
|
||||
return !!REGEX_DEV.test(chain.toString());
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { ToBigInt } from '../types.js';
|
||||
export declare const isToBigInt: (value?: unknown) => value is ToBigInt;
|
||||
@@ -0,0 +1,2 @@
|
||||
import { isOn } from './helpers.js';
|
||||
export const isToBigInt = /*#__PURE__*/ isOn('toBigInt');
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { ToBn } from '../types.js';
|
||||
export declare const isToBn: (value?: unknown) => value is ToBn;
|
||||
@@ -0,0 +1,2 @@
|
||||
import { isOn } from './helpers.js';
|
||||
export const isToBn = /*#__PURE__*/ isOn('toBn');
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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 declare function isU8a(value?: unknown): value is Uint8Array;
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @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) {
|
||||
// here we defer the instanceof check which is actually slightly
|
||||
// slower than just checking the constrctor (direct instances)
|
||||
return (((value && value.constructor) === Uint8Array) ||
|
||||
value instanceof Uint8Array);
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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
|
||||
* ```
|
||||
*/
|
||||
export declare function isUndefined(value?: unknown): value is undefined;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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
|
||||
* ```
|
||||
*/
|
||||
export function isUndefined(value) {
|
||||
return value === undefined;
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @name isUtf8
|
||||
* @summary Tests if the input is valid Utf8
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is valid Utf8
|
||||
*/
|
||||
export declare function isUtf8(value?: number[] | Uint8Array | string | null): boolean;
|
||||
@@ -0,0 +1,197 @@
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @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 declare function isWasm(value?: unknown): value is Uint8Array;
|
||||
@@ -0,0 +1,12 @@
|
||||
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) {
|
||||
return isU8a(value) && u8aEq(value.subarray(0, 4), WASM_MAGIC);
|
||||
}
|
||||
Reference in New Issue
Block a user