From 0a7ac8a89b2ea10b3437359421e003ffb6677209 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 17 Apr 2018 11:20:23 +0200 Subject: [PATCH] u8a to u8a convenience function (#116) * Convenience u8aToU8a conversion * u8aConcat take u8a | hex --- package.json | 2 +- packages/util/docs/u8a.md | 21 ++++++++++++++++++- packages/util/package.json | 2 +- packages/util/src/u8a/concat.js | 8 +++++--- packages/util/src/u8a/index.js | 2 ++ packages/util/src/u8a/toU8a.js | 32 +++++++++++++++++++++++++++++ packages/util/src/u8a/toU8a.spec.js | 29 ++++++++++++++++++++++++++ yarn.lock | 20 +++++++++--------- 8 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 packages/util/src/u8a/toU8a.js create mode 100644 packages/util/src/u8a/toU8a.spec.js diff --git a/package.json b/package.json index 888c28232..cf64011c7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.17.4", + "@polkadot/dev": "^0.17.6", "lerna": "^2.5.1" } } diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index 43398553a..012ee90e4 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -13,6 +13,7 @@ Utility methods to convert to and from `Uint8Array` objects - [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string. - [u8aToHex](#u8atohex) Creates a hex string from a Uint8Array object. - [u8aToString](#u8atostring) Creates a string from a Uint8Array object. +- [u8aToU8a](#u8atou8a) Creates a Uint8Array value from a Uint8Array bignumber or hex input. - [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object. ## u8aConcat @@ -20,7 +21,7 @@ Utility methods to convert to and from `Uint8Array` objects Creates a concatenated Uint8Array from the inputs. ```js -u8aConcat (...values: Array): Uint8Array +u8aConcat (...values: Array): Uint8Array ``` @@ -192,6 +193,24 @@ import { u8aToString } from '@polkadot/util'; u8aToString(new Uint8Array([21,23,45,67])); // 21,23,45,67 ``` +## u8aToU8a + +Creates a Uint8Array value from a Uint8Array bignumber or hex input. + +```js +u8aToU8a (value?: Uint8Array | string): Uint8Array +``` + + +`null` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation. + +```js +import { u8aToU8a } from '@polkadot/util'; + +u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34]) +u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34]) +``` + ## u8aToUtf8 Creates a utf-8 string from a Uint8Array object. diff --git a/packages/util/package.json b/packages/util/package.json index 2fb545384..4a7ef6bf4 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.5" + "@polkadot/api-jsonrpc": "^0.8.11" }, "dependencies": { "babel-runtime": "^6.26.0", diff --git a/packages/util/src/u8a/concat.js b/packages/util/src/u8a/concat.js index 319cec9b0..3e9802eba 100644 --- a/packages/util/src/u8a/concat.js +++ b/packages/util/src/u8a/concat.js @@ -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 + @signature u8aConcat (...values: Array): 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 { +module.exports = function u8aConcat (...list: Array): 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; diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index 148790c95..ffa24adf6 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -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 }; diff --git a/packages/util/src/u8a/toU8a.js b/packages/util/src/u8a/toU8a.js new file mode 100644 index 000000000..b7199d7b9 --- /dev/null +++ b/packages/util/src/u8a/toU8a.js @@ -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); +}; diff --git a/packages/util/src/u8a/toU8a.spec.js b/packages/util/src/u8a/toU8a.spec.js new file mode 100644 index 000000000..b108a0a75 --- /dev/null +++ b/packages/util/src/u8a/toU8a.spec.js @@ -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]) + ); + }); +}); diff --git a/yarn.lock b/yarn.lock index 680316c94..198e6eb2e 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.5": - version "0.8.5" - resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.5.tgz#f27afc6304dcf94b0e95f62b33b2091ba6b690e5" +"@polkadot/api-jsonrpc@^0.8.11": + version "0.8.11" + resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.11.tgz#9ff8dc38f026bac0bb8274836f4f68ba93aa090e" dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.17.4": - version "0.17.4" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.4.tgz#d5bcbbb0a6531ebbce222d946be9ae4a52eea1e4" +"@polkadot/dev@^0.17.6": + version "0.17.6" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.6.tgz#8935c83ee71d32bf343e83fe537deb20b5764be0" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -112,7 +112,7 @@ eslint-plugin-promise "^3.6.0" eslint-plugin-react "^7.7.0" eslint-plugin-standard "^3.0.1" - flow-bin "^0.69.0" + flow-bin "^0.70.0" flow-copy-source "^1.3.0" jest "^22.4.2" makeshift "^1.1.0" @@ -2360,9 +2360,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.69.0: - version "0.69.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.69.0.tgz#053159a684a6051fcbf0b71a2eb19a9679082da6" +flow-bin@^0.70.0: + version "0.70.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5" flow-copy-source@^1.3.0: version "1.3.0"