diff --git a/package.json b/package.json index 14b627d16..3fe6b36eb 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.17.8", + "@polkadot/dev": "^0.17.9", "lerna": "^2.5.1" } } diff --git a/packages/util/package.json b/packages/util/package.json index f309cefdc..5fc9777c4 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -29,7 +29,7 @@ "test": "echo \"Tests only available from root wrapper\"" }, "devDependencies": { - "@polkadot/api-jsonrpc": "^0.8.14" + "@polkadot/api-jsonrpc": "^0.8.16" }, "dependencies": { "babel-runtime": "^6.26.0", diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index ffa24adf6..e8a0b1683 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -13,6 +13,7 @@ const u8aFromString = require('./fromString'); const u8aToBn = require('./toBn'); const u8aToBuffer = require('./toBuffer'); const u8aToHex = require('./toHex'); +const u8aToHexShort = require('./toHexShort'); const u8aToString = require('./toString'); const u8aToU8a = require('./toU8a'); const u8aToUtf8 = require('./toUtf8'); @@ -31,6 +32,7 @@ module.exports = { u8aToBn, u8aToBuffer, u8aToHex, + u8aToHexShort, u8aToString, u8aToU8a, u8aToUtf8 diff --git a/packages/util/src/u8a/toHexShort.js b/packages/util/src/u8a/toHexShort.js new file mode 100644 index 000000000..200c377d7 --- /dev/null +++ b/packages/util/src/u8a/toHexShort.js @@ -0,0 +1,33 @@ +// 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 u8aToHexShort + @signature u8aToHexShort (value?: UInt8Array, max: number = 16): string + @summary Creates a hex string from a Uint8Array object with max characters. + @description + `UInt8Array` input values return the actual hex string shortened to the specified characters. `null` or `undefined` values returns an `0x` string. + @example + import { u8aToHexShort } from '@polkadot/util'; + + u8aToHexShort(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf]), 4); // 0x68...0f +*/ +module.exports = function u8aToHex (value?: Uint8Array, max: number = 16): string { + if (!value || !value.length) { + return '0x'; + } + + const interim = value.reduce((result, item) => { + return result + ('0' + item.toString(16)).slice(-2); + }, ''); + + if (interim.length <= max) { + return `0x${interim}`; + } + + const mid = Math.floor(max / 2); + + return `0x${interim.substr(0, mid)}...${interim.slice(-1 * mid)}`; +}; diff --git a/packages/util/src/u8a/toHexShort.spec.js b/packages/util/src/u8a/toHexShort.spec.js new file mode 100644 index 000000000..caa0ea08d --- /dev/null +++ b/packages/util/src/u8a/toHexShort.spec.js @@ -0,0 +1,33 @@ +// 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 { u8aToHexShort } = require('./index'); + +describe('u8aToHexShort', () => { + const input = new Uint8Array([1, 2, 3, 4, 5, 0, 6, 7, 8, 9]); + + it('returns empty as 0x', () => { + expect( + u8aToHexShort() + ).toEqual('0x'); + }); + + it('returns the hex value for the array (max >= length)', () => { + expect( + u8aToHexShort(input, 20) + ).toEqual('0x01020304050006070809'); + }); + + it('returns the hex value for the array (max (default) < length)', () => { + expect( + u8aToHexShort(input) + ).toEqual('0x01020304...06070809'); + }); + + it('returns the hex value for the array (max < length)', () => { + expect( + u8aToHexShort(input, 10) + ).toEqual('0x01020...70809'); + }); +}); diff --git a/yarn.lock b/yarn.lock index fe7bbbefe..8302f8be0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,15 +79,15 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/api-jsonrpc@^0.8.14": - version "0.8.14" - resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.14.tgz#94be22fcd1802ddbd87c35e52b8916e1f9135dd6" +"@polkadot/api-jsonrpc@^0.8.16": + version "0.8.16" + resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.16.tgz#0779480eb0e388a4d83a8089bff92d956623bf38" dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.17.8": - version "0.17.8" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.8.tgz#d22daea75e88587250c4f8d7ebdda1dd957570df" +"@polkadot/dev@^0.17.9": + version "0.17.9" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.9.tgz#12316416384eb3da5e926f2e875902a3c8e4e5a3" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0"