mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-27 18:38:00 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexAddPrefix
|
||||
* @summary Adds the `0x` prefix to string values.
|
||||
* @description
|
||||
* Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexAddPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
export declare function hexAddPrefix(value?: string | null): HexString;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { hexHasPrefix } from './hasPrefix.js';
|
||||
/**
|
||||
* @name hexAddPrefix
|
||||
* @summary Adds the `0x` prefix to string values.
|
||||
* @description
|
||||
* Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexAddPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
export function hexAddPrefix(value) {
|
||||
return value && hexHasPrefix(value)
|
||||
? value
|
||||
: `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexFixLength
|
||||
* @summary Shifts a hex string to a specific bitLength
|
||||
* @description
|
||||
* Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexFixLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('fixed', hexFixLength('0x12', 16)); // => 0x12
|
||||
* console.log('fixed', hexFixLength('0x12', 16, true)); // => 0x0012
|
||||
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
|
||||
* ```
|
||||
*/
|
||||
export declare function hexFixLength(value: string, bitLength?: number, withPadding?: boolean): HexString;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { hexAddPrefix } from './addPrefix.js';
|
||||
import { hexStripPrefix } from './stripPrefix.js';
|
||||
/**
|
||||
* @name hexFixLength
|
||||
* @summary Shifts a hex string to a specific bitLength
|
||||
* @description
|
||||
* Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexFixLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('fixed', hexFixLength('0x12', 16)); // => 0x12
|
||||
* console.log('fixed', hexFixLength('0x12', 16, true)); // => 0x0012
|
||||
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
|
||||
* ```
|
||||
*/
|
||||
export function hexFixLength(value, bitLength = -1, withPadding = false) {
|
||||
const strLength = Math.ceil(bitLength / 4);
|
||||
const hexLength = strLength + 2;
|
||||
return hexAddPrefix((bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength))
|
||||
? hexStripPrefix(value)
|
||||
: (value.length > hexLength)
|
||||
? hexStripPrefix(value).slice(-1 * strLength)
|
||||
: `${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength));
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexHasPrefix
|
||||
* @summary Tests for the existence of a `0x` prefix.
|
||||
* @description
|
||||
* Checks for a valid hex input value and if the start matched `0x`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexHasPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
export declare function hexHasPrefix(value?: string | null): value is HexString;
|
||||
@@ -0,0 +1,18 @@
|
||||
import { isHex } from '../is/hex.js';
|
||||
/**
|
||||
* @name hexHasPrefix
|
||||
* @summary Tests for the existence of a `0x` prefix.
|
||||
* @description
|
||||
* Checks for a valid hex input value and if the start matched `0x`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexHasPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
export function hexHasPrefix(value) {
|
||||
return !!value && isHex(value, -1);
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
export { hexAddPrefix } from './addPrefix.js';
|
||||
export { hexFixLength } from './fixLength.js';
|
||||
export { hexHasPrefix } from './hasPrefix.js';
|
||||
export { hexStripPrefix } from './stripPrefix.js';
|
||||
export { hexToBigInt } from './toBigInt.js';
|
||||
export { hexToBn } from './toBn.js';
|
||||
export { hexToNumber } from './toNumber.js';
|
||||
export { hexToString } from './toString.js';
|
||||
export { hexToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
export { hexAddPrefix } from './addPrefix.js';
|
||||
export { hexFixLength } from './fixLength.js';
|
||||
export { hexHasPrefix } from './hasPrefix.js';
|
||||
export { hexStripPrefix } from './stripPrefix.js';
|
||||
export { hexToBigInt } from './toBigInt.js';
|
||||
export { hexToBn } from './toBn.js';
|
||||
export { hexToNumber } from './toNumber.js';
|
||||
export { hexToString } from './toString.js';
|
||||
export { hexToU8a } from './toU8a.js';
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexStripPrefix
|
||||
* @summary Strips any leading `0x` prefix.
|
||||
* @description
|
||||
* Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexStripPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
* ```
|
||||
*/
|
||||
export declare function hexStripPrefix(value?: string | null): string;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { REGEX_HEX_NOPREFIX, REGEX_HEX_PREFIXED } from '../is/hex.js';
|
||||
/**
|
||||
* @name hexStripPrefix
|
||||
* @summary Strips any leading `0x` prefix.
|
||||
* @description
|
||||
* Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexStripPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
* ```
|
||||
*/
|
||||
export function hexStripPrefix(value) {
|
||||
if (!value || value === '0x') {
|
||||
return '';
|
||||
}
|
||||
else if (REGEX_HEX_PREFIXED.test(value)) {
|
||||
return value.substring(2);
|
||||
}
|
||||
else if (REGEX_HEX_NOPREFIX.test(value)) {
|
||||
return value;
|
||||
}
|
||||
throw new Error(`Expected hex value to convert, found '${value}'`);
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import type { ToBnOptions } from '../types.js';
|
||||
/**
|
||||
* @name hexToBigInt
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
export declare function hexToBigInt(value?: string | null, { isLe, isNegative }?: ToBnOptions): bigint;
|
||||
@@ -0,0 +1,12 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { u8aToBigInt } from '../u8a/toBigInt.js';
|
||||
import { hexToU8a } from './toU8a.js';
|
||||
/**
|
||||
* @name hexToBigInt
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
export function hexToBigInt(value, { isLe = false, isNegative = false } = {}) {
|
||||
return !value || value === '0x'
|
||||
? BigInt(0)
|
||||
: u8aToBigInt(hexToU8a(value), { isLe, isNegative });
|
||||
}
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
import type { ToBnOptions } from '../types.js';
|
||||
import { BN } from '../bn/bn.js';
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @param _value The value to convert
|
||||
* @param _options Options to pass while converting
|
||||
* @param _options.isLe Convert using Little Endian
|
||||
* @param _options.isNegative Convert using two's complement
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToBn(value?: string | null, { isLe, isNegative }?: ToBnOptions): BN;
|
||||
@@ -0,0 +1,32 @@
|
||||
import { BN } from '../bn/bn.js';
|
||||
import { hexStripPrefix } from './stripPrefix.js';
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @param _value The value to convert
|
||||
* @param _options Options to pass while converting
|
||||
* @param _options.isLe Convert using Little Endian
|
||||
* @param _options.isNegative Convert using two's complement
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
export function hexToBn(value, { isLe = false, isNegative = false } = {}) {
|
||||
if (!value || value === '0x') {
|
||||
return new BN(0);
|
||||
}
|
||||
const stripped = hexStripPrefix(value);
|
||||
const bn = new BN(stripped, 16, isLe ? 'le' : 'be');
|
||||
// fromTwos takes as parameter the number of bits, which is the hex length
|
||||
// multiplied by 4 (2 bytes being 8 bits)
|
||||
return isNegative
|
||||
? bn.fromTwos(stripped.length * 4)
|
||||
: bn;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexToNumber
|
||||
* @summary Creates a Number value from a Buffer object.
|
||||
* @description
|
||||
* `null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToNumber } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToNumber('0x1234'); // => 0x1234
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToNumber(value?: string | null): number;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { hexToBn } from './toBn.js';
|
||||
/**
|
||||
* @name hexToNumber
|
||||
* @summary Creates a Number value from a Buffer object.
|
||||
* @description
|
||||
* `null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToNumber } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToNumber('0x1234'); // => 0x1234
|
||||
* ```
|
||||
*/
|
||||
export function hexToNumber(value) {
|
||||
return value
|
||||
? hexToBn(value).toNumber()
|
||||
: NaN;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToString } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x68656c6c6f'); // hello
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToString(_value?: string | null): string;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { u8aToString } from '../u8a/toString.js';
|
||||
import { hexToU8a } from './toU8a.js';
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToString } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x68656c6c6f'); // hello
|
||||
* ```
|
||||
*/
|
||||
export function hexToString(_value) {
|
||||
return u8aToString(hexToU8a(_value));
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToU8a(value?: string | null, bitLength?: number): Uint8Array;
|
||||
@@ -0,0 +1,54 @@
|
||||
const CHR = '0123456789abcdef';
|
||||
const U8 = new Uint8Array(256);
|
||||
const U16 = new Uint8Array(256 * 256);
|
||||
for (let i = 0, count = CHR.length; i < count; i++) {
|
||||
U8[CHR[i].charCodeAt(0) | 0] = i | 0;
|
||||
if (i > 9) {
|
||||
U8[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const s = i << 8;
|
||||
for (let j = 0; j < 256; j++) {
|
||||
U16[s | j] = (U8[i] << 4) | U8[j];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a(value, bitLength = -1) {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
let s = value.startsWith('0x')
|
||||
? 2
|
||||
: 0;
|
||||
const decLength = Math.ceil((value.length - s) / 2);
|
||||
const endLength = Math.ceil(bitLength === -1
|
||||
? decLength
|
||||
: bitLength / 8);
|
||||
const result = new Uint8Array(endLength);
|
||||
const offset = endLength > decLength
|
||||
? endLength - decLength
|
||||
: 0;
|
||||
for (let i = offset; i < endLength; i++, s += 2) {
|
||||
// The big factor here is actually the string lookups. If we do
|
||||
// HEX_TO_U16[value.substring()] we get an 10x slowdown. In the
|
||||
// same vein using charCodeAt (as opposed to value[s] or value.charAt(s)) is
|
||||
// also the faster operation by at least 2x with the character map above
|
||||
result[i] = U16[(value.charCodeAt(s) << 8) | value.charCodeAt(s + 1)];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToU8a(_value?: string | null, bitLength?: number): Uint8Array;
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a(_value, bitLength = -1) {
|
||||
if (!_value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
const value = _value.startsWith('0x')
|
||||
? _value.substring(2)
|
||||
: _value;
|
||||
const buf = Buffer.from(value, 'hex');
|
||||
const valLength = value.length / 2;
|
||||
const resultLength = Math.ceil(bitLength === -1
|
||||
? valLength
|
||||
: bitLength / 8);
|
||||
if (resultLength === valLength) {
|
||||
return Uint8Array.from(buf);
|
||||
}
|
||||
const offset = resultLength > valLength
|
||||
? resultLength - valLength
|
||||
: 0;
|
||||
if (offset) {
|
||||
const u8a = new Uint8Array(resultLength);
|
||||
u8a.set(buf, offset);
|
||||
return u8a;
|
||||
}
|
||||
return Uint8Array.from(buf.subarray(0, resultLength));
|
||||
}
|
||||
Reference in New Issue
Block a user