Add u8aToHex (#73)

* Add u8aToHex

* Update doc

* Cleanup allocation

* Fix typo
This commit is contained in:
Jaco Greeff
2018-02-02 13:28:52 +01:00
committed by GitHub
parent aab2a6543c
commit 8488254a0c
5 changed files with 68 additions and 2 deletions
+19 -1
View File
@@ -7,6 +7,7 @@ Utility methods to convert to and from `Uint8Array` objects
- [u8aFromString](#u8afromstring) Creates a Uint8Array object from a string.
- [u8aFromUtf8](#u8afromutf8) Creates a Uint8Array object from a utf-8 string.
- [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string.
- [u8aToHex](#u8atohex) Creates a hex string from a Uint8Array object.
- [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object.
## u8aFromBuffer
@@ -84,6 +85,23 @@ import { u8aToBuffer } from '@polkadot/util';
console.log('Buffer', u8aToBuffer('0x123480001f'));
```
## u8aToHex
Creates a hex string from a Uint8Array object.
```js
u8aToHex (value?: UInt8Array): string
```
`UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string.
```js
import { u8aToHex\ } from '@polkadot/util';
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
```
## u8aToUtf8
Creates a utf-8 string from a Uint8Array object.
@@ -96,7 +114,7 @@ u8aToUtf8 (value?: UInt8Array): string
`UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
```js
import { u8aToString } from '@polkadot/util';
import { u8aToUtf8 } from '@polkadot/util';
u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
```
+2
View File
@@ -8,6 +8,7 @@ const u8aFromHex = require('./fromHex');
const u8aFromUtf8 = require('./fromUtf8');
const u8aFromString = require('./fromString');
const u8aToBuffer = require('./toBuffer');
const u8aToHex = require('./toHex');
const u8aToUtf8 = require('./toUtf8');
/**
@@ -19,5 +20,6 @@ module.exports = {
u8aFromString,
u8aFromUtf8,
u8aToBuffer,
u8aToHex,
u8aToUtf8
};
+25
View File
@@ -0,0 +1,25 @@
// 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
/**
@name u8aToHex
@signature u8aToHex (value?: UInt8Array): string
@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
import { u8aToHex } from '@polkadot/util';
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
*/
module.exports = function u8aToHex (value?: Uint8Array): string {
if (!value || !value.length) {
return '0x';
}
return value.reduce((result, item) => {
return result + ('0' + item.toString(16)).slice(-2);
}, '0x');
};
+21
View File
@@ -0,0 +1,21 @@
// 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 { u8aToHex } = require('./index');
describe('u8aToHex', () => {
it('returns empty as 0x', () => {
expect(
u8aToHex()
).toEqual('0x');
});
it('returns the hex value for the array', () => {
expect(
u8aToHex(
new Uint8Array([128, 0, 10])
)
).toEqual('0x80000a');
});
});
+1 -1
View File
@@ -14,7 +14,7 @@ const decoder = new TextDecoder('utf-8');
@description
`UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
@example
import { u8aToString } from '@polkadot/util';
import { u8aToUtf8 } from '@polkadot/util';
u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
*/