@pezkuwi/util 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/package.json +49 -0
- package/src/array/chunk.spec.ts +29 -0
- package/src/array/chunk.ts +35 -0
- package/src/array/filter.spec.ts +20 -0
- package/src/array/filter.ts +24 -0
- package/src/array/flatten.spec.ts +33 -0
- package/src/array/flatten.ts +51 -0
- package/src/array/index.ts +14 -0
- package/src/array/range.spec.ts +29 -0
- package/src/array/range.ts +31 -0
- package/src/array/shuffle.spec.ts +39 -0
- package/src/array/shuffle.ts +27 -0
- package/src/array/unzip.spec.ts +14 -0
- package/src/array/unzip.ts +18 -0
- package/src/array/zip.spec.ts +26 -0
- package/src/array/zip.ts +17 -0
- package/src/assert.spec.ts +40 -0
- package/src/assert.ts +50 -0
- package/src/bi/consts.ts +112 -0
- package/src/bi/helpers.ts +23 -0
- package/src/bi/index.ts +16 -0
- package/src/bi/max.spec.ts +32 -0
- package/src/bi/min.spec.ts +26 -0
- package/src/bi/min.ts +16 -0
- package/src/bi/sqrt.spec.ts +59 -0
- package/src/bi/sqrt.ts +43 -0
- package/src/bi/toBigInt.spec.ts +69 -0
- package/src/bi/toBigInt.ts +33 -0
- package/src/bi/toHex.spec.ts +56 -0
- package/src/bi/toHex.ts +16 -0
- package/src/bi/toU8a.spec.ts +99 -0
- package/src/bi/toU8a.ts +72 -0
- package/src/bn/bn.ts +6 -0
- package/src/bn/consts.spec.ts +38 -0
- package/src/bn/consts.ts +112 -0
- package/src/bn/fromHex.spec.ts +15 -0
- package/src/bn/fromHex.ts +4 -0
- package/src/bn/index.ts +18 -0
- package/src/bn/max.spec.ts +32 -0
- package/src/bn/min.spec.ts +26 -0
- package/src/bn/min.ts +36 -0
- package/src/bn/sqrt.spec.ts +37 -0
- package/src/bn/sqrt.ts +50 -0
- package/src/bn/toBn.spec.ts +53 -0
- package/src/bn/toBn.ts +43 -0
- package/src/bn/toHex.spec.ts +50 -0
- package/src/bn/toHex.ts +27 -0
- package/src/bn/toU8a.spec.ts +60 -0
- package/src/bn/toU8a.ts +45 -0
- package/src/buffer/index.ts +8 -0
- package/src/buffer/toU8a.spec.ts +20 -0
- package/src/buffer/toU8a.ts +20 -0
- package/src/bundle.ts +35 -0
- package/src/compact/addLength.spec.ts +14 -0
- package/src/compact/addLength.ts +24 -0
- package/src/compact/defaults.ts +7 -0
- package/src/compact/fromU8a.spec.ts +84 -0
- package/src/compact/fromU8a.ts +109 -0
- package/src/compact/index.ts +28 -0
- package/src/compact/stripLength.spec.ts +17 -0
- package/src/compact/stripLength.ts +26 -0
- package/src/compact/toU8a.spec.ts +59 -0
- package/src/compact/toU8a.ts +53 -0
- package/src/compact/types.ts +4 -0
- package/src/detectPackage.spec.ts +110 -0
- package/src/detectPackage.ts +138 -0
- package/src/extractTime.spec.ts +21 -0
- package/src/extractTime.ts +71 -0
- package/src/float/index.ts +4 -0
- package/src/float/toU8a.spec.ts +52 -0
- package/src/float/toU8a.ts +30 -0
- package/src/format/formatBalance.spec.ts +296 -0
- package/src/format/formatBalance.ts +177 -0
- package/src/format/formatDate.spec.ts +17 -0
- package/src/format/formatDate.ts +22 -0
- package/src/format/formatDecimal.spec.ts +28 -0
- package/src/format/formatDecimal.ts +22 -0
- package/src/format/formatElapsed.spec.ts +44 -0
- package/src/format/formatElapsed.ts +35 -0
- package/src/format/formatNumber.spec.ts +60 -0
- package/src/format/formatNumber.ts +29 -0
- package/src/format/getSeparator.spec.ts +53 -0
- package/src/format/getSeparator.ts +15 -0
- package/src/format/index.ts +9 -0
- package/src/format/si.ts +52 -0
- package/src/has.spec.ts +12 -0
- package/src/has.ts +38 -0
- package/src/hex/addPrefix.spec.ts +32 -0
- package/src/hex/addPrefix.ts +26 -0
- package/src/hex/fixLength.spec.ts +38 -0
- package/src/hex/fixLength.ts +36 -0
- package/src/hex/hasPrefix.spec.ts +32 -0
- package/src/hex/hasPrefix.ts +24 -0
- package/src/hex/index.ts +16 -0
- package/src/hex/stripPrefix.spec.ts +44 -0
- package/src/hex/stripPrefix.ts +30 -0
- package/src/hex/toBigInt.spec.ts +74 -0
- package/src/hex/toBigInt.ts +19 -0
- package/src/hex/toBn.spec.ts +74 -0
- package/src/hex/toBn.ts +40 -0
- package/src/hex/toNumber.spec.ts +20 -0
- package/src/hex/toNumber.ts +24 -0
- package/src/hex/toString.spec.ts +20 -0
- package/src/hex/toString.ts +25 -0
- package/src/hex/toU8a.spec.ts +76 -0
- package/src/hex/toU8a.ts +68 -0
- package/src/hex/toU8aBuffer.spec.ts +38 -0
- package/src/hex/toU8aBuffer.ts +52 -0
- package/src/index.spec.ts +12 -0
- package/src/index.ts +6 -0
- package/src/is/array.spec.ts +20 -0
- package/src/is/array.ts +10 -0
- package/src/is/ascii.spec.ts +49 -0
- package/src/is/ascii.ts +52 -0
- package/src/is/bigInt.spec.ts +26 -0
- package/src/is/bigInt.ts +20 -0
- package/src/is/bn.spec.ts +21 -0
- package/src/is/bn.ts +23 -0
- package/src/is/boolean.spec.ts +26 -0
- package/src/is/boolean.ts +20 -0
- package/src/is/buffer.spec.ts +26 -0
- package/src/is/buffer.ts +28 -0
- package/src/is/childClass.spec.ts +36 -0
- package/src/is/childClass.ts +29 -0
- package/src/is/class.spec.ts +37 -0
- package/src/is/class.ts +12 -0
- package/src/is/codec.spec.ts +52 -0
- package/src/is/codec.ts +26 -0
- package/src/is/compact.spec.ts +27 -0
- package/src/is/compact.ts +19 -0
- package/src/is/error.spec.ts +20 -0
- package/src/is/error.ts +23 -0
- package/src/is/function.spec.ts +20 -0
- package/src/is/function.ts +23 -0
- package/src/is/helpers.ts +23 -0
- package/src/is/hex.spec.ts +77 -0
- package/src/is/hex.ts +36 -0
- package/src/is/index.ts +36 -0
- package/src/is/instanceOf.spec.ts +48 -0
- package/src/is/instanceOf.ts +24 -0
- package/src/is/ip.spec.ts +50 -0
- package/src/is/ip.ts +56 -0
- package/src/is/jsonObject.spec.ts +51 -0
- package/src/is/jsonObject.ts +45 -0
- package/src/is/null.spec.ts +20 -0
- package/src/is/null.ts +20 -0
- package/src/is/number.spec.ts +20 -0
- package/src/is/number.ts +20 -0
- package/src/is/object.spec.ts +32 -0
- package/src/is/object.ts +24 -0
- package/src/is/observable.spec.ts +26 -0
- package/src/is/observable.ts +22 -0
- package/src/is/promise.spec.ts +32 -0
- package/src/is/promise.ts +6 -0
- package/src/is/riscv.spec.ts +36 -0
- package/src/is/riscv.ts +27 -0
- package/src/is/string.spec.ts +32 -0
- package/src/is/string.ts +22 -0
- package/src/is/testChain.spec.ts +25 -0
- package/src/is/testChain.ts +12 -0
- package/src/is/toBigInt.spec.ts +22 -0
- package/src/is/toBigInt.ts +8 -0
- package/src/is/toBn.spec.ts +25 -0
- package/src/is/toBn.ts +8 -0
- package/src/is/u8a.spec.ts +42 -0
- package/src/is/u8a.ts +25 -0
- package/src/is/undefined.spec.ts +23 -0
- package/src/is/undefined.ts +21 -0
- package/src/is/utf8.spec.ts +42 -0
- package/src/is/utf8.ts +207 -0
- package/src/is/wasm.spec.ts +20 -0
- package/src/is/wasm.ts +17 -0
- package/src/lazy.spec.ts +71 -0
- package/src/lazy.ts +63 -0
- package/src/logger.spec.ts +188 -0
- package/src/logger.ts +160 -0
- package/src/memoize.spec.ts +79 -0
- package/src/memoize.ts +49 -0
- package/src/mod.ts +4 -0
- package/src/nextTick.ts +26 -0
- package/src/noop.ts +16 -0
- package/src/number/index.ts +9 -0
- package/src/number/toHex.spec.ts +44 -0
- package/src/number/toHex.ts +27 -0
- package/src/number/toU8a.spec.ts +50 -0
- package/src/number/toU8a.ts +25 -0
- package/src/object/clear.spec.ts +14 -0
- package/src/object/clear.ts +16 -0
- package/src/object/copy.spec.ts +17 -0
- package/src/object/copy.ts +12 -0
- package/src/object/entries.spec.ts +20 -0
- package/src/object/entries.ts +12 -0
- package/src/object/index.ts +10 -0
- package/src/object/keys.spec.ts +19 -0
- package/src/object/keys.ts +10 -0
- package/src/object/property.spec.ts +149 -0
- package/src/object/property.ts +39 -0
- package/src/object/spread.spec.ts +36 -0
- package/src/object/spread.ts +38 -0
- package/src/object/values.spec.ts +19 -0
- package/src/object/values.ts +10 -0
- package/src/packageDetect.ts +13 -0
- package/src/packageInfo.ts +6 -0
- package/src/promisify.spec.ts +50 -0
- package/src/promisify.ts +30 -0
- package/src/string/camelCase.spec.ts +119 -0
- package/src/string/camelCase.ts +79 -0
- package/src/string/index.ts +12 -0
- package/src/string/lowerFirst.spec.ts +41 -0
- package/src/string/lowerFirst.ts +46 -0
- package/src/string/pascalCase.spec.ts +26 -0
- package/src/string/shorten.spec.ts +38 -0
- package/src/string/shorten.ts +24 -0
- package/src/string/toHex.spec.ts +26 -0
- package/src/string/toHex.ts +27 -0
- package/src/string/toU8a.spec.ts +31 -0
- package/src/string/toU8a.ts +28 -0
- package/src/string/upperFirst.spec.ts +41 -0
- package/src/stringify.spec.ts +33 -0
- package/src/stringify.ts +20 -0
- package/src/test/index.ts +4 -0
- package/src/test/performance.ts +92 -0
- package/src/types.ts +100 -0
- package/src/u8a/cmp.spec.ts +62 -0
- package/src/u8a/cmp.ts +50 -0
- package/src/u8a/concat.spec.ts +42 -0
- package/src/u8a/concat.ts +60 -0
- package/src/u8a/concatBuffer.ts +34 -0
- package/src/u8a/empty.spec.ts +46 -0
- package/src/u8a/empty.ts +22 -0
- package/src/u8a/eq.spec.ts +113 -0
- package/src/u8a/eq.ts +46 -0
- package/src/u8a/fixLength.spec.ts +34 -0
- package/src/u8a/fixLength.ts +34 -0
- package/src/u8a/index.ts +22 -0
- package/src/u8a/sorted.spec.ts +20 -0
- package/src/u8a/sorted.ts +22 -0
- package/src/u8a/toBigInt.spec.ts +106 -0
- package/src/u8a/toBigInt.ts +100 -0
- package/src/u8a/toBn.spec.ts +106 -0
- package/src/u8a/toBn.ts +101 -0
- package/src/u8a/toBuffer.spec.ts +20 -0
- package/src/u8a/toBuffer.ts +28 -0
- package/src/u8a/toFloat.spec.ts +86 -0
- package/src/u8a/toFloat.ts +26 -0
- package/src/u8a/toHex.spec.ts +94 -0
- package/src/u8a/toHex.ts +68 -0
- package/src/u8a/toHexBuffer.spec.ts +63 -0
- package/src/u8a/toHexBuffer.ts +30 -0
- package/src/u8a/toNumber.spec.ts +47 -0
- package/src/u8a/toNumber.ts +80 -0
- package/src/u8a/toString.spec.ts +36 -0
- package/src/u8a/toString.ts +26 -0
- package/src/u8a/toU8a.spec.ts +63 -0
- package/src/u8a/toU8a.ts +46 -0
- package/src/u8a/wrap.spec.ts +139 -0
- package/src/u8a/wrap.ts +65 -0
- package/tsconfig.build.json +19 -0
- package/tsconfig.spec.json +17 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ToBnOptions } from '../types.js';
|
|
5
|
+
|
|
6
|
+
import { BigInt } from '@pezkuwi/x-bigint';
|
|
7
|
+
|
|
8
|
+
import { _1n } from '../bi/consts.js';
|
|
9
|
+
|
|
10
|
+
const U8_MAX = BigInt(256);
|
|
11
|
+
const U16_MAX = BigInt(256 * 256);
|
|
12
|
+
const U64_MAX = BigInt('0x10000000000000000');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @name u8aToBigInt
|
|
16
|
+
* @summary Creates a BigInt from a Uint8Array object.
|
|
17
|
+
*/
|
|
18
|
+
export function u8aToBigInt (value: Uint8Array, { isLe = true, isNegative = false }: ToBnOptions = {}): bigint {
|
|
19
|
+
// slice + reverse is expensive, however SCALE is LE by default so this is the path
|
|
20
|
+
// we are most interested in (the BE is added for the sake of being comprehensive)
|
|
21
|
+
if (!isLe) {
|
|
22
|
+
value = value.slice().reverse();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const count = value.length;
|
|
26
|
+
|
|
27
|
+
if (isNegative && count && (value[count - 1] & 0x80)) {
|
|
28
|
+
switch (count) {
|
|
29
|
+
case 0:
|
|
30
|
+
return BigInt(0);
|
|
31
|
+
|
|
32
|
+
case 1:
|
|
33
|
+
return BigInt(((value[0] ^ 0x0000_00ff) * -1) - 1);
|
|
34
|
+
|
|
35
|
+
case 2:
|
|
36
|
+
return BigInt((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
|
|
37
|
+
|
|
38
|
+
case 4:
|
|
39
|
+
return BigInt((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const dvI = new DataView(value.buffer, value.byteOffset);
|
|
43
|
+
|
|
44
|
+
if (count === 8) {
|
|
45
|
+
return dvI.getBigInt64(0, true);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let result = BigInt(0);
|
|
49
|
+
const mod = count % 2;
|
|
50
|
+
|
|
51
|
+
for (let i = count - 2; i >= mod; i -= 2) {
|
|
52
|
+
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true) ^ 0xffff);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (mod) {
|
|
56
|
+
result = (result * U8_MAX) + BigInt(value[0] ^ 0xff);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (result * -_1n) - _1n;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
switch (count) {
|
|
63
|
+
case 0:
|
|
64
|
+
return BigInt(0);
|
|
65
|
+
|
|
66
|
+
case 1:
|
|
67
|
+
return BigInt(value[0]);
|
|
68
|
+
|
|
69
|
+
case 2:
|
|
70
|
+
return BigInt(value[0] + (value[1] << 8));
|
|
71
|
+
|
|
72
|
+
case 4:
|
|
73
|
+
return BigInt(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const dvI = new DataView(value.buffer, value.byteOffset);
|
|
77
|
+
|
|
78
|
+
switch (count) {
|
|
79
|
+
case 8:
|
|
80
|
+
return dvI.getBigUint64(0, true);
|
|
81
|
+
|
|
82
|
+
case 16:
|
|
83
|
+
return (dvI.getBigUint64(8, true) * U64_MAX) + dvI.getBigUint64(0, true);
|
|
84
|
+
|
|
85
|
+
default: {
|
|
86
|
+
let result = BigInt(0);
|
|
87
|
+
const mod = count % 2;
|
|
88
|
+
|
|
89
|
+
for (let i = count - 2; i >= mod; i -= 2) {
|
|
90
|
+
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (mod) {
|
|
94
|
+
result = (result * U8_MAX) + BigInt(value[0]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { TESTS } from '../bi/toU8a.spec.js';
|
|
7
|
+
import { perf } from '../test/index.js';
|
|
8
|
+
import { u8aToBn } from './index.js';
|
|
9
|
+
|
|
10
|
+
// test-cases are the same as in u8aToBigInt
|
|
11
|
+
describe('u8aToBn', (): void => {
|
|
12
|
+
it('converts little-endian by default', (): void => {
|
|
13
|
+
expect(
|
|
14
|
+
u8aToBn(
|
|
15
|
+
new Uint8Array([0x12, 0x34])
|
|
16
|
+
).toString(16)
|
|
17
|
+
).toBe('3412');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('empty creation', (): void => {
|
|
21
|
+
it('handles unsigned (le)', (): void => {
|
|
22
|
+
expect(
|
|
23
|
+
u8aToBn(
|
|
24
|
+
new Uint8Array(),
|
|
25
|
+
{ isLe: true }
|
|
26
|
+
).toString(16)
|
|
27
|
+
).toBe('0');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('handles signed (le)', (): void => {
|
|
31
|
+
expect(
|
|
32
|
+
u8aToBn(
|
|
33
|
+
new Uint8Array(),
|
|
34
|
+
{ isLe: true, isNegative: true }
|
|
35
|
+
).toString(16)
|
|
36
|
+
).toBe('0');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('handles unsigned (be)', (): void => {
|
|
40
|
+
expect(
|
|
41
|
+
u8aToBn(
|
|
42
|
+
new Uint8Array(),
|
|
43
|
+
{ isLe: false }
|
|
44
|
+
).toString(16)
|
|
45
|
+
).toBe('0');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('handles signed (be)', (): void => {
|
|
49
|
+
expect(
|
|
50
|
+
u8aToBn(
|
|
51
|
+
new Uint8Array(),
|
|
52
|
+
{ isLe: false, isNegative: true }
|
|
53
|
+
).toString(16)
|
|
54
|
+
).toBe('0');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('handles overflows correctly (little-endian)', (): void => {
|
|
59
|
+
expect(
|
|
60
|
+
u8aToBn(
|
|
61
|
+
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]),
|
|
62
|
+
{ isLe: true }
|
|
63
|
+
).toNumber()
|
|
64
|
+
).toBe(256);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('length tests', (): void => {
|
|
68
|
+
[true, false].forEach((isLe) => {
|
|
69
|
+
for (let i = 1; i < 32; i++) {
|
|
70
|
+
const tu8a = [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
|
|
71
|
+
const tstr = tu8a.map((n) => n.toString(16));
|
|
72
|
+
|
|
73
|
+
it(`converts values with bitLength=${i * 8}, isLe=${isLe}`, (): void => {
|
|
74
|
+
expect(
|
|
75
|
+
u8aToBn(
|
|
76
|
+
new Uint8Array(tu8a.slice(0, i)),
|
|
77
|
+
{ isLe }
|
|
78
|
+
).toString(16)
|
|
79
|
+
).toBe(
|
|
80
|
+
isLe
|
|
81
|
+
? tstr.slice(0, i).reverse().join('')
|
|
82
|
+
: tstr.slice(0, i).join('')
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('conversion tests', (): void => {
|
|
90
|
+
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
|
91
|
+
it(`#${i}: creates ${strval} (bitLength=${numarr.length * 8}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
|
92
|
+
expect(
|
|
93
|
+
u8aToBn(
|
|
94
|
+
new Uint8Array(numarr),
|
|
95
|
+
{ isLe, isNegative }
|
|
96
|
+
).toString()
|
|
97
|
+
).toBe(strval);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
perf('u8aToBn (i32)', 750_000, [[new Uint8Array([0x9c, 0x9c, 0x9c, 0x9c])]], (v: Uint8Array) => u8aToBn(v, { isNegative: true }));
|
|
103
|
+
perf('u8aToBn (u32)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
|
104
|
+
perf('u8aToBn (u64)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
|
105
|
+
perf('u8aToBn (u128)', 750_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
|
106
|
+
});
|
package/src/u8a/toBn.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ToBnOptions } from '../types.js';
|
|
5
|
+
|
|
6
|
+
import { BN } from '../bn/bn.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @name u8aToBn
|
|
10
|
+
* @summary Creates a BN from a Uint8Array object.
|
|
11
|
+
* @description
|
|
12
|
+
* `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
|
|
13
|
+
* @param value The value to convert
|
|
14
|
+
* @param options Options to pass while converting
|
|
15
|
+
* @param options.isLe Convert using Little Endian (default)
|
|
16
|
+
* @param options.isNegative Convert using two's complement
|
|
17
|
+
* @example
|
|
18
|
+
* <BR>
|
|
19
|
+
*
|
|
20
|
+
* ```javascript
|
|
21
|
+
* import { u8aToBn } from '@pezkuwi/util';
|
|
22
|
+
*
|
|
23
|
+
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function u8aToBn (value: Uint8Array, { isLe = true, isNegative = false }: ToBnOptions = {}): BN {
|
|
27
|
+
// slice + reverse is expensive, however SCALE is LE by default so this is the path
|
|
28
|
+
// we are most interested in (the BE is added for the sake of being comprehensive)
|
|
29
|
+
if (!isLe) {
|
|
30
|
+
value = value.slice().reverse();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const count = value.length;
|
|
34
|
+
|
|
35
|
+
// shortcut for <= u48 values - in this case the manual conversion
|
|
36
|
+
// here seems to be more efficient than passing the full array
|
|
37
|
+
if (isNegative && count && (value[count - 1] & 0x80)) {
|
|
38
|
+
// Most common case i{8, 16, 32} default LE SCALE-encoded
|
|
39
|
+
// For <= 32, we also optimize the xor to a single op
|
|
40
|
+
switch (count) {
|
|
41
|
+
case 0:
|
|
42
|
+
return new BN(0);
|
|
43
|
+
|
|
44
|
+
case 1:
|
|
45
|
+
return new BN(((value[0] ^ 0x0000_00ff) * -1) - 1);
|
|
46
|
+
|
|
47
|
+
case 2:
|
|
48
|
+
return new BN((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
|
|
49
|
+
|
|
50
|
+
case 3:
|
|
51
|
+
return new BN((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 0x00ff_ffff) * -1) - 1);
|
|
52
|
+
|
|
53
|
+
case 4:
|
|
54
|
+
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
|
|
55
|
+
// 32-bit, in the case where the top-most bit is set this yields a negative value
|
|
56
|
+
return new BN((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
|
|
57
|
+
|
|
58
|
+
case 5:
|
|
59
|
+
return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00)) * -1) - 1);
|
|
60
|
+
|
|
61
|
+
case 6:
|
|
62
|
+
return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00)) * -1) - 1);
|
|
63
|
+
|
|
64
|
+
default:
|
|
65
|
+
return new BN(value, 'le').fromTwos(count * 8);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Most common case - u{8, 16, 32} default LE SCALE-encoded
|
|
70
|
+
//
|
|
71
|
+
// There are some slight benefits in unrolling this specific loop,
|
|
72
|
+
// however it comes with diminishing returns since here the actual
|
|
73
|
+
// `new BN` does seem to take up the bulk of the time
|
|
74
|
+
switch (count) {
|
|
75
|
+
case 0:
|
|
76
|
+
return new BN(0);
|
|
77
|
+
|
|
78
|
+
case 1:
|
|
79
|
+
return new BN(value[0]);
|
|
80
|
+
|
|
81
|
+
case 2:
|
|
82
|
+
return new BN(value[0] + (value[1] << 8));
|
|
83
|
+
|
|
84
|
+
case 3:
|
|
85
|
+
return new BN(value[0] + (value[1] << 8) + (value[2] << 16));
|
|
86
|
+
|
|
87
|
+
case 4:
|
|
88
|
+
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
|
|
89
|
+
// 32-bit, in the case where the top-most bit is set this yields a negative value
|
|
90
|
+
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
|
|
91
|
+
|
|
92
|
+
case 5:
|
|
93
|
+
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00));
|
|
94
|
+
|
|
95
|
+
case 6:
|
|
96
|
+
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00));
|
|
97
|
+
|
|
98
|
+
default:
|
|
99
|
+
return new BN(value, 'le');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { u8aToBuffer } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('u8aToBuffer', (): void => {
|
|
9
|
+
it('returns [] when the buffer is null', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
u8aToBuffer(null)
|
|
12
|
+
).toEqual(Buffer.from([]));
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns the Buffer value for the Uint8Array', (): void => {
|
|
16
|
+
expect(
|
|
17
|
+
u8aToBuffer(new Uint8Array([128, 0, 10]))
|
|
18
|
+
).toEqual(Buffer.from([128, 0, 10]));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { BufferClass, BufferObject } from '../types.js';
|
|
5
|
+
|
|
6
|
+
import { xglobal } from '@pezkuwi/x-global';
|
|
7
|
+
|
|
8
|
+
import { hasBuffer } from '../has.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @name u8aToBuffer
|
|
12
|
+
* @summary Creates a Buffer object from a hex string.
|
|
13
|
+
* @description
|
|
14
|
+
* `null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
|
|
15
|
+
* @example
|
|
16
|
+
* <BR>
|
|
17
|
+
*
|
|
18
|
+
* ```javascript
|
|
19
|
+
* import { u8aToBuffer } from '@pezkuwi/util';
|
|
20
|
+
*
|
|
21
|
+
* console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3])));
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function u8aToBuffer <T = BufferObject> (value?: Uint8Array | null): T {
|
|
25
|
+
return hasBuffer
|
|
26
|
+
? (xglobal.Buffer as unknown as BufferClass).from(value || [])
|
|
27
|
+
: new Uint8Array(value || []) as T;
|
|
28
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { hexToU8a } from '../hex/index.js';
|
|
7
|
+
import { u8aToFloat } from './index.js';
|
|
8
|
+
|
|
9
|
+
describe('u8aToFloat', (): void => {
|
|
10
|
+
it('throws on invalid bitLength', (): void => {
|
|
11
|
+
expect(
|
|
12
|
+
() => u8aToFloat(hexToU8a('0x00000000'), { bitLength: 48 as 32 })
|
|
13
|
+
).toThrow();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('throws on invalid input length', (): void => {
|
|
17
|
+
expect(
|
|
18
|
+
() => u8aToFloat(new Uint8Array())
|
|
19
|
+
).toThrow();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('32-bit', (): void => {
|
|
23
|
+
it('correctly decodes +0.0', (): void => {
|
|
24
|
+
expect(
|
|
25
|
+
u8aToFloat(hexToU8a('0x00000000'))
|
|
26
|
+
).toEqual(+0.0);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('correctly decodes -0.0', (): void => {
|
|
30
|
+
expect(
|
|
31
|
+
u8aToFloat(hexToU8a('0x00000080'))
|
|
32
|
+
).toEqual(-0.0);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('correctly decodes -0.0 (BE)', (): void => {
|
|
36
|
+
expect(
|
|
37
|
+
u8aToFloat(hexToU8a('0x80000000'), { isLe: false })
|
|
38
|
+
).toEqual(-0.0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('decoded NaN', (): void => {
|
|
42
|
+
expect(
|
|
43
|
+
u8aToFloat(hexToU8a('0x0000c07f'))
|
|
44
|
+
).toEqual(Number.NaN);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// https://www.h-schmidt.net/FloatConverter/IEEE754.html
|
|
48
|
+
it('decodes a known number', (): void => {
|
|
49
|
+
expect(
|
|
50
|
+
u8aToFloat(hexToU8a('0x79e9f642')).toFixed(3)
|
|
51
|
+
).toEqual('123.456');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('decodes a known number (BE)', (): void => {
|
|
55
|
+
expect(
|
|
56
|
+
u8aToFloat(hexToU8a('0x42f6e979'), { isLe: false }).toFixed(3)
|
|
57
|
+
).toEqual('123.456');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('64-bit', (): void => {
|
|
62
|
+
it('correctly decodes +0.0', (): void => {
|
|
63
|
+
expect(
|
|
64
|
+
u8aToFloat(hexToU8a('0x0000000000000000'), { bitLength: 64 })
|
|
65
|
+
).toEqual(+0.0);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('correctly decodes -0.0', (): void => {
|
|
69
|
+
expect(
|
|
70
|
+
u8aToFloat(hexToU8a('0x0000000000000080'), { bitLength: 64 })
|
|
71
|
+
).toEqual(-0.0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('correctly decodes -0.0 (BE)', (): void => {
|
|
75
|
+
expect(
|
|
76
|
+
u8aToFloat(hexToU8a('0x8000000000000000'), { bitLength: 64, isLe: false })
|
|
77
|
+
).toEqual(-0.0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('correctly decodes NaN', (): void => {
|
|
81
|
+
expect(
|
|
82
|
+
u8aToFloat(hexToU8a('0x000000000000f87f'), { bitLength: 64 })
|
|
83
|
+
).toEqual(Number.NaN);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
interface Options {
|
|
5
|
+
bitLength?: 32 | 64;
|
|
6
|
+
isLe?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @name u8aToFloat
|
|
11
|
+
* @description Converts a Uint8Array value into the float (either 32 or 64-bit)
|
|
12
|
+
* representation.
|
|
13
|
+
*/
|
|
14
|
+
export function u8aToFloat (value: Uint8Array, { bitLength = 32, isLe = true }: Options = {}): number {
|
|
15
|
+
if (bitLength !== 32 && bitLength !== 64) {
|
|
16
|
+
throw new Error('Invalid bitLength provided, expected 32 or 64');
|
|
17
|
+
} else if (value.length < (bitLength / 8)) {
|
|
18
|
+
throw new Error(`Invalid input buffer provided, expected at least ${bitLength / 8} bytes, found ${value.length}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const dv = new DataView(value.buffer, value.byteOffset);
|
|
22
|
+
|
|
23
|
+
return bitLength === 32
|
|
24
|
+
? dv.getFloat32(0, isLe)
|
|
25
|
+
: dv.getFloat64(0, isLe);
|
|
26
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { perfCmp } from '../test/index.js';
|
|
7
|
+
import { u8aToHex } from './index.js';
|
|
8
|
+
import { u8aToHex as u8aToHexBuffer } from './toHexBuffer.js';
|
|
9
|
+
|
|
10
|
+
const ptest32k = new Uint8Array(32768);
|
|
11
|
+
const ptest256 = new Uint8Array(256);
|
|
12
|
+
|
|
13
|
+
for (let i = 0, count = ptest32k.length; i < count; i++) {
|
|
14
|
+
if (i < ptest256.length) {
|
|
15
|
+
ptest256[1] = i % 256;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
ptest32k[i] = i % 256;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('u8aToHex', (): void => {
|
|
22
|
+
it('returns empty as 0x', (): void => {
|
|
23
|
+
expect(
|
|
24
|
+
u8aToHex()
|
|
25
|
+
).toEqual('0x');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('returns empty as "" (unprefixed)', (): void => {
|
|
29
|
+
expect(
|
|
30
|
+
u8aToHex(null, -1, false)
|
|
31
|
+
).toEqual('');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('returns the hex value for the array', (): void => {
|
|
35
|
+
expect(
|
|
36
|
+
u8aToHex(
|
|
37
|
+
new Uint8Array([128, 0, 10])
|
|
38
|
+
)
|
|
39
|
+
).toEqual('0x80000a');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns the hex value for the array (unprefixed)', (): void => {
|
|
43
|
+
expect(
|
|
44
|
+
u8aToHex(
|
|
45
|
+
new Uint8Array([128, 0, 10]),
|
|
46
|
+
-1,
|
|
47
|
+
false
|
|
48
|
+
)
|
|
49
|
+
).toEqual('80000a');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('handles starting zeros correctly', (): void => {
|
|
53
|
+
expect(
|
|
54
|
+
u8aToHex(
|
|
55
|
+
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0])
|
|
56
|
+
)
|
|
57
|
+
).toEqual('0x0001000000000000');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('returns the hex value where allowed < max', (): void => {
|
|
61
|
+
expect(
|
|
62
|
+
u8aToHex(
|
|
63
|
+
new Uint8Array(Uint8Array.from([128, 0, 10, 11]), 64)
|
|
64
|
+
)
|
|
65
|
+
).toEqual('0x80000a0b');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('returns the trimmed hex value where allowed >= max', (): void => {
|
|
69
|
+
expect(
|
|
70
|
+
u8aToHex(
|
|
71
|
+
new Uint8Array([128, 0, 10, 11, 12, 13]),
|
|
72
|
+
32
|
|
73
|
+
)
|
|
74
|
+
).toEqual('0x8000…0c0d');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('converts known bytes to their correct values', (): void => {
|
|
78
|
+
expect(
|
|
79
|
+
// hello world
|
|
80
|
+
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]))
|
|
81
|
+
).toEqual('0x68656c6c6f20776f726c64');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
perfCmp('u8aToHex (32k)', ['u8aToHexBuffer', 'u8aToHex'], 1000, [[ptest32k]], (s: Uint8Array, isSecond) =>
|
|
85
|
+
isSecond
|
|
86
|
+
? u8aToHex(s)
|
|
87
|
+
: u8aToHexBuffer(s)
|
|
88
|
+
);
|
|
89
|
+
perfCmp('u8aToHex (128)', ['u8aToHexBuffer', 'u8aToHex'], 200_000, [[ptest256]], (s: Uint8Array, isSecond) =>
|
|
90
|
+
isSecond
|
|
91
|
+
? u8aToHex(s)
|
|
92
|
+
: u8aToHexBuffer(s)
|
|
93
|
+
);
|
|
94
|
+
});
|
package/src/u8a/toHex.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { HexString } from '../types.js';
|
|
5
|
+
|
|
6
|
+
const U8 = new Array<string>(256);
|
|
7
|
+
const U16 = new Array<string>(256 * 256);
|
|
8
|
+
|
|
9
|
+
for (let n = 0; n < 256; n++) {
|
|
10
|
+
U8[n] = n.toString(16).padStart(2, '0');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < 256; i++) {
|
|
14
|
+
const s = i << 8;
|
|
15
|
+
|
|
16
|
+
for (let j = 0; j < 256; j++) {
|
|
17
|
+
U16[s | j] = U8[i] + U8[j];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
function hex (value: Uint8Array, result: HexString): HexString {
|
|
23
|
+
const mod = (value.length % 2) | 0;
|
|
24
|
+
const length = (value.length - mod) | 0;
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < length; i += 2) {
|
|
27
|
+
result += U16[(value[i] << 8) | value[i + 1]];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (mod) {
|
|
31
|
+
result += U8[value[length] | 0];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @name u8aToHex
|
|
39
|
+
* @summary Creates a hex string from a Uint8Array object.
|
|
40
|
+
* @description
|
|
41
|
+
* `UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string.
|
|
42
|
+
* @example
|
|
43
|
+
* <BR>
|
|
44
|
+
*
|
|
45
|
+
* ```javascript
|
|
46
|
+
* import { u8aToHex } from '@pezkuwi/util';
|
|
47
|
+
*
|
|
48
|
+
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString {
|
|
52
|
+
// this is not 100% correct sinmce we support isPrefixed = false....
|
|
53
|
+
const empty = isPrefixed
|
|
54
|
+
? '0x'
|
|
55
|
+
: '' as HexString;
|
|
56
|
+
|
|
57
|
+
if (!value?.length) {
|
|
58
|
+
return empty;
|
|
59
|
+
} else if (bitLength > 0) {
|
|
60
|
+
const length = Math.ceil(bitLength / 8);
|
|
61
|
+
|
|
62
|
+
if (value.length > length) {
|
|
63
|
+
return `${hex(value.subarray(0, length / 2), empty)}…${hex(value.subarray(value.length - length / 2), '' as HexString)}`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return hex(value, empty);
|
|
68
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { u8aToHex } from './toHexBuffer.js';
|
|
7
|
+
|
|
8
|
+
describe('u8aToHex', (): void => {
|
|
9
|
+
it('returns empty as 0x', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
u8aToHex()
|
|
12
|
+
).toEqual('0x');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns empty as "" (unprefixed)', (): void => {
|
|
16
|
+
expect(
|
|
17
|
+
u8aToHex(null, -1, false)
|
|
18
|
+
).toEqual('');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('returns the hex value for the array', (): void => {
|
|
22
|
+
expect(
|
|
23
|
+
u8aToHex(
|
|
24
|
+
new Uint8Array([128, 0, 10])
|
|
25
|
+
)
|
|
26
|
+
).toEqual('0x80000a');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('returns the hex value for the array (unprefixed)', (): void => {
|
|
30
|
+
expect(
|
|
31
|
+
u8aToHex(
|
|
32
|
+
new Uint8Array([128, 0, 10]),
|
|
33
|
+
-1,
|
|
34
|
+
false
|
|
35
|
+
)
|
|
36
|
+
).toEqual('80000a');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('handles starting zeros correctly', (): void => {
|
|
40
|
+
expect(
|
|
41
|
+
u8aToHex(
|
|
42
|
+
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0])
|
|
43
|
+
)
|
|
44
|
+
).toEqual('0x0001000000000000');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns the hex value where allowed < max', (): void => {
|
|
48
|
+
expect(
|
|
49
|
+
u8aToHex(
|
|
50
|
+
new Uint8Array(Uint8Array.from([128, 0, 10, 11]), 64)
|
|
51
|
+
)
|
|
52
|
+
).toEqual('0x80000a0b');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('returns the trimmed hex value where allowed >= max', (): void => {
|
|
56
|
+
expect(
|
|
57
|
+
u8aToHex(
|
|
58
|
+
new Uint8Array([128, 0, 10, 11, 12, 13]),
|
|
59
|
+
32
|
|
60
|
+
)
|
|
61
|
+
).toEqual('0x8000…0c0d');
|
|
62
|
+
});
|
|
63
|
+
});
|