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 { HexString } from '../types';
import { hexHasPrefix } from './hasPrefix';
/**
@@ -17,7 +19,7 @@ import { hexHasPrefix } from './hasPrefix';
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
* ```
*/
export function hexAddPrefix (value?: string | null): string {
export function hexAddPrefix (value?: string | null): HexString {
if (value && hexHasPrefix(value)) {
return 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';
import { hexAddPrefix } from './addPrefix';
import { hexStripPrefix } from './stripPrefix';
@@ -20,7 +22,7 @@ import { hexStripPrefix } from './stripPrefix';
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
* ```
*/
export function hexFixLength (value: string, bitLength = -1, withPadding = false): string {
export function hexFixLength (value: string, bitLength = -1, withPadding = false): HexString {
const strLength = Math.ceil(bitLength / 4);
const hexLength = strLength + 2;
+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';
import { isHex } from '../is/hex';
/**
@@ -17,6 +19,6 @@ import { isHex } from '../is/hex';
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
* ```
*/
export function hexHasPrefix (value?: string | null): boolean {
export function hexHasPrefix (value?: string | null): value is HexString {
return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x');
}
+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';
import { assert } from '../assert';
import { isHex } from '../is/hex';
import { hexStripPrefix } from './stripPrefix';
@@ -20,7 +22,7 @@ import { hexStripPrefix } from './stripPrefix';
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
* ```
*/
export function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
export function hexToU8a (_value?: HexString | string | null, bitLength = -1): Uint8Array {
if (!_value) {
return new Uint8Array();
}