Add u8a-only strict variants (#1503)

This commit is contained in:
Jaco
2022-05-25 10:02:01 +03:00
committed by GitHub
parent 2698f6779f
commit fbf973dc2a
20 changed files with 105 additions and 37 deletions
+9 -1
View File
@@ -33,8 +33,16 @@ export function isAscii (value?: U8aLike | null): boolean {
if (value) {
return isStringIn && !isHex(value)
? value.toString().split('').every(isAsciiChar)
: u8aToU8a(value).every(isAsciiByte);
: isAsciiStrict(u8aToU8a(value));
}
return isStringIn;
}
/**
* @name isAsciiStrict
* @description A strict version of [[isAscii]], accepting only Uint8Array inputs
*/
export function isAsciiStrict (u8a: Uint8Array): boolean {
return u8a.every(isAsciiByte);
}
+1 -1
View File
@@ -17,7 +17,7 @@
*/
export function isError (value: unknown): value is Error {
return (
((value && (value as Record<string, unknown>).constructor) === Error) ||
((value && (value as Error).constructor) === Error) ||
value instanceof Error
);
}
+2 -2
View File
@@ -5,7 +5,7 @@
* @summary Type checking utilities
*/
export { isArray } from './array';
export { isAscii } from './ascii';
export { isAscii, isAsciiStrict } from './ascii';
export { isBigInt } from './bigInt';
export { isBn } from './bn';
export { isBuffer } from './buffer';
@@ -30,5 +30,5 @@ export { isToBigInt } from './toBigInt';
export { isToBn } from './toBn';
export { isU8a } from './u8a';
export { isUndefined } from './undefined';
export { isUtf8 } from './utf8';
export { isUtf8, isUtf8Strict } from './utf8';
export { isWasm } from './wasm';
+10
View File
@@ -10,6 +10,16 @@ describe('isInstanceOf', (): void => {
).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)
+3 -3
View File
@@ -16,9 +16,9 @@
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function isInstanceOf (value: unknown, clazz: Function): boolean {
export function isInstanceOf (value: unknown, Clazz: Function): boolean {
return (
((value && (value as Record<string, unknown>).constructor) === clazz) ||
value instanceof Uint8Array
((value && (value as Record<string, unknown>).constructor) === Clazz) ||
value instanceof Clazz
);
}
+1 -1
View File
@@ -19,7 +19,7 @@ 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 Record<string, unknown>).constructor) === Uint8Array) ||
((value && (value as Uint8Array).constructor) === Uint8Array) ||
value instanceof Uint8Array
);
}
+8 -1
View File
@@ -19,7 +19,14 @@ export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | str
return isString(value);
}
const u8a = u8aToU8a(value);
return isUtf8Strict(u8aToU8a(value));
}
/**
* @name isUtf8Strict
* @description A strict version of [[isUtf8]], acception only Uint8Array inpouts
*/
export function isUtf8Strict (u8a: Uint8Array): boolean {
const len = u8a.length;
let i = 0;