Small utility adjustments (#1185)

This commit is contained in:
Jaco
2021-11-15 07:16:21 +02:00
committed by GitHub
parent 5ab0b173b6
commit cf0aab479a
4 changed files with 19 additions and 20 deletions
+7 -10
View File
@@ -9,11 +9,6 @@ import { isString } from './string';
const FORMAT = [9, 10, 13];
/** @internal */
function getCharCode0 (s: string): number {
return s.charCodeAt(0);
}
/** @internal */
function isAsciiByte (b: number): boolean {
return (b < 127) && (
@@ -22,6 +17,10 @@ function isAsciiByte (b: number): boolean {
);
}
function isAsciiChar (s: string): boolean {
return isAsciiByte(s.charCodeAt(0));
}
/**
* @name isAscii
* @summary Tests if the input is printable ASCII
@@ -32,11 +31,9 @@ export function isAscii (value?: U8aLike | null): boolean {
const isStringIn = isString(value);
if (value) {
return (
isStringIn && !isHex(value)
? value.toString().split('').map(getCharCode0)
: u8aToU8a(value)
).every(isAsciiByte);
return isStringIn && !isHex(value)
? value.toString().split('').every(isAsciiChar)
: u8aToU8a(value).every(isAsciiByte);
}
return isStringIn;
+2 -2
View File
@@ -1,12 +1,12 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
const re = /(Development|Local Testnet)$/;
const REGEX_DEV = /(Development|Local Testnet)$/;
export function isTestChain (chain?: string | null): boolean {
if (!chain) {
return false;
}
return !!re.test(chain.toString());
return !!REGEX_DEV.test(chain.toString());
}
+7 -5
View File
@@ -1,10 +1,6 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
function isByteZero (v: number): boolean {
return !v;
}
/**
* @name u8aEmpty
* @summary Tests for a `Uint8Array` for emptyness
@@ -12,5 +8,11 @@ function isByteZero (v: number): boolean {
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
*/
export function u8aEmpty (value: Uint8Array): boolean {
return value.length === 0 || value.every(isByteZero);
for (let i = 0; i < value.length; i++) {
if (value[i]) {
return false;
}
}
return true;
}
+3 -3
View File
@@ -5,14 +5,14 @@ import type { HexString } from '../types';
import { u8aToU8a } from './toU8a';
interface Constructor<T extends Uint32Array | Uint16Array | Uint8Array> {
interface Constructor<T extends Uint32Array | Uint16Array > {
BYTES_PER_ELEMENT: number;
new(...args: unknown[]): T;
}
// Creates a Uint8Array, ensuring that the alignment is correct
function createArray <T extends Uint32Array | Uint16Array | Uint8Array> (Clazz: Constructor<T>, value: Uint8Array): T {
// Creates a Uint{16, 32}Array, ensuring that the alignment is correct
function createArray <T extends Uint32Array | Uint16Array> (Clazz: Constructor<T>, value: Uint8Array): T {
// The byteOffset needs to match the size of the data, i.e. for Uint32 it needs to be 4
// NOTE: DataView doesn't have this limitation, but getters are slower
const align = Clazz.BYTES_PER_ELEMENT;