u8a to u8a convenience function (#116)
* Convenience u8aToU8a conversion * u8aConcat take u8a | hex
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aToU8a = require('./toU8a');
|
||||
|
||||
/**
|
||||
@name u8aConcat
|
||||
@signature u8aConcat (...values: Array<Uint8Array>): Uint8Array
|
||||
@signature u8aConcat (...values: Array<Uint8Array | string>): Uint8Array
|
||||
@summary Creates a concatenated Uint8Array from the inputs.
|
||||
@description
|
||||
Concatenates the input arrays into a single `UInt8Array`.
|
||||
@@ -17,13 +19,13 @@
|
||||
new Uint8Array([4, 5, 6])
|
||||
); // [1, 2, 3, 4, 5, 6]
|
||||
*/
|
||||
module.exports = function u8aConcat (...list: Array<Uint8Array>): Uint8Array {
|
||||
module.exports = function u8aConcat (...list: Array<Uint8Array | string>): Uint8Array {
|
||||
const length = list.reduce((total, item) => total + item.length, 0);
|
||||
const result = new Uint8Array(length);
|
||||
let offset = 0;
|
||||
|
||||
return list.reduce((result, item) => {
|
||||
result.set(item, offset);
|
||||
result.set(u8aToU8a(item), offset);
|
||||
offset += item.length;
|
||||
|
||||
return result;
|
||||
|
||||
@@ -14,6 +14,7 @@ const u8aToBn = require('./toBn');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToHex = require('./toHex');
|
||||
const u8aToString = require('./toString');
|
||||
const u8aToU8a = require('./toU8a');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
|
||||
/**
|
||||
@@ -31,5 +32,6 @@ module.exports = {
|
||||
u8aToBuffer,
|
||||
u8aToHex,
|
||||
u8aToString,
|
||||
u8aToU8a,
|
||||
u8aToUtf8
|
||||
};
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isU8a = require('../is/u8a');
|
||||
const hexToU8a = require('../hex/toU8a');
|
||||
|
||||
/**
|
||||
@name u8aToU8a
|
||||
@signature u8aToU8a (value?: Uint8Array | string): Uint8Array
|
||||
@summary Creates a Uint8Array value from a Uint8Array bignumber or hex input.
|
||||
@description
|
||||
`null` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
||||
@example
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
|
||||
u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
||||
u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
||||
*/
|
||||
module.exports = function u8aToU8a (value?: Uint8Array | string): Uint8Array {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return new Uint8Array(0);
|
||||
}
|
||||
|
||||
return isU8a(value)
|
||||
// $FlowFixMe type is determined
|
||||
? value
|
||||
// $FlowFixMe type is determined
|
||||
: hexToU8a(value);
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToU8a } = require('./index');
|
||||
|
||||
describe('u8aToU8a', () => {
|
||||
it('returns an empty Uint8Array when null provided', () => {
|
||||
expect(
|
||||
u8aToU8a(null)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (hex)', () => {
|
||||
expect(
|
||||
u8aToU8a('0x80000a')
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (u8a)', () => {
|
||||
expect(
|
||||
u8aToU8a(new Uint8Array([128, 0, 10]))
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user