Additional Node-specific u8a utils (#1177)

* Additional Node-specific u8a utils

* toHexBuffer
This commit is contained in:
Jaco
2021-11-13 11:38:07 +02:00
committed by GitHub
parent 445e0f7ea6
commit d9231d26aa
5 changed files with 165 additions and 0 deletions
@@ -0,0 +1,30 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aConcat } from './concatBuffer';
describe('u8aConcat', (): void => {
it('concatenates arrays', (): void => {
expect(
u8aConcat(
new Uint8Array([1, 2, 3, 4]),
new Uint8Array([5, 6]),
new Uint8Array([7, 8, 9])
)
).toEqual(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
);
});
it('concatenates arrays & hex values', (): void => {
expect(
u8aConcat(
new Uint8Array([1, 2, 3, 4]),
'0x0506',
'0x070809'
)
).toEqual(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
);
});
});
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { U8aLike } from '../types';
import { u8aToU8a } from './toU8a';
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@polkadot/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: U8aLike[]): Uint8Array {
const u8as = new Array<Uint8Array>(list.length);
for (let i = 0; i < list.length; i++) {
u8as[i] = u8aToU8a(list[i]);
}
return Uint8Array.from(Buffer.concat(u8as));
}
+60
View File
@@ -0,0 +1,60 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aToHex } from './toHexBuffer';
describe('u8aToHex', (): void => {
it('returns empty as 0x', (): void => {
expect(
u8aToHex()
).toEqual('0x');
});
it('returns empty as "" (unprefixed)', (): void => {
expect(
u8aToHex(null, -1, false)
).toEqual('');
});
it('returns the hex value for the array', (): void => {
expect(
u8aToHex(
new Uint8Array([128, 0, 10])
)
).toEqual('0x80000a');
});
it('returns the hex value for the array (unprefixed)', (): void => {
expect(
u8aToHex(
new Uint8Array([128, 0, 10]),
-1, false
)
).toEqual('80000a');
});
it('handles starting zeros correctly', (): void => {
expect(
u8aToHex(
new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0])
)
).toEqual('0x0001000000000000');
});
it('returns the hex value where allowed < max', (): void => {
expect(
u8aToHex(
new Uint8Array(Uint8Array.from([128, 0, 10, 11]), 64)
)
).toEqual('0x80000a0b');
});
it('returns the trimmed hex value where allowed >= max', (): void => {
expect(
u8aToHex(
new Uint8Array([128, 0, 10, 11, 12, 13]),
32
)
).toEqual('0x8000…0c0d');
});
});
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
/**
* @name u8aToHex
* @summary Creates a hex string from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string.
* @example
* <BR>
*
* ```javascript
* import { u8aToHex } from '@polkadot/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString {
const length = Math.ceil(bitLength / 8);
return `${isPrefixed ? '0x' : ''}${
!value || !value.length
? ''
: (length > 0 && value.length > length)
? `${Buffer.from(value.subarray(0, length / 2)).toString('hex')}…${Buffer.from(value.subarray(value.length - length / 2)).toString('hex')}`
: Buffer.from(value).toString('hex')
}` as HexString;
}