Adjust isHex with template return (#1103)

* Adjust isHex with template return

* Unneeded assignment

* CHANGELOG

* typo

* Additional HexString types

* Additional HexString inputs

* Use U8aLike as applicable

* typo

* Unneeded type

* HexString outputs

* Export types
This commit is contained in:
Jaco
2021-09-08 14:38:53 +02:00
committed by GitHub
parent 6b9b7fa7f5
commit 9fc57a23e8
71 changed files with 233 additions and 122 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { U8aLike } from '../types';
import { u8aToU8a } from '../u8a/toU8a';
import { isString } from './string';
@@ -12,7 +14,7 @@ const FORMAT = [9, 10, 13];
* @description
* Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
*/
export function isAscii (value?: number[] | Buffer | Uint8Array | string | null): boolean {
export function isAscii (value?: U8aLike | null): boolean {
return value
? !u8aToU8a(value).some((byte) => (byte >= 127) || (byte < 32 && !FORMAT.includes(byte)))
: isString(value);
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
/**
@@ -18,7 +20,7 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
* isHex('0x1234', 8); // => false
* ```
*/
export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is string {
export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is HexString {
return (typeof value === 'string' && (value === '0x' || HEX_REGEX.test(value)))
? bitLength === -1
? ((value.length % 2 === 0) || ignoreLength)
+3 -1
View File
@@ -3,6 +3,8 @@
// Adapted from https://github.com/JulienPalard/is_utf8/blob/master/is_utf8.c
import type { HexString } from '../types';
import { u8aToU8a } from '../u8a/toU8a';
import { isString } from './string';
@@ -12,7 +14,7 @@ import { isString } from './string';
* @description
* Checks to see if the input string or Uint8Array is valid Utf8
*/
export function isUtf8 (value?: number[] | Buffer | Uint8Array | string | null): boolean {
export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | string | null): boolean {
if (!value) {
return isString(value);
}