From 8488254a0c16cb8cd56cc23500a41c8f8f9d6055 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 2 Feb 2018 13:28:52 +0100 Subject: [PATCH] Add u8aToHex (#73) * Add u8aToHex * Update doc * Cleanup allocation * Fix typo --- packages/util/docs/u8a.md | 20 +++++++++++++++++++- packages/util/src/u8a/index.js | 2 ++ packages/util/src/u8a/toHex.js | 25 +++++++++++++++++++++++++ packages/util/src/u8a/toHex.spec.js | 21 +++++++++++++++++++++ packages/util/src/u8a/toUtf8.js | 2 +- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 packages/util/src/u8a/toHex.js create mode 100644 packages/util/src/u8a/toHex.spec.js diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index 476b71ee8..ddba5d0c5 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -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 ``` \ No newline at end of file diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index eeaed2aa5..3e9ec7030 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -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 }; diff --git a/packages/util/src/u8a/toHex.js b/packages/util/src/u8a/toHex.js new file mode 100644 index 000000000..7b32a7ddc --- /dev/null +++ b/packages/util/src/u8a/toHex.js @@ -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'); +}; diff --git a/packages/util/src/u8a/toHex.spec.js b/packages/util/src/u8a/toHex.spec.js new file mode 100644 index 000000000..8ab9dbfa7 --- /dev/null +++ b/packages/util/src/u8a/toHex.spec.js @@ -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'); + }); +}); diff --git a/packages/util/src/u8a/toUtf8.js b/packages/util/src/u8a/toUtf8.js index a8e7cdad2..1ae1b67fc 100644 --- a/packages/util/src/u8a/toUtf8.js +++ b/packages/util/src/u8a/toUtf8.js @@ -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 */