Small utility adjustments (#1185)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user