From 5c270164ecea8b625163865757cdbdee080b591b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 12 Feb 2018 17:10:40 +0100 Subject: [PATCH] Add u8aFixLength (#84) --- packages/util/docs/u8a.md | 20 ++++++++++++++ packages/util/src/hex/fixLength.js | 2 +- packages/util/src/u8a/fixLength.js | 35 +++++++++++++++++++++++++ packages/util/src/u8a/fixLength.spec.js | 33 +++++++++++++++++++++++ packages/util/src/u8a/index.js | 2 ++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 packages/util/src/u8a/fixLength.js create mode 100644 packages/util/src/u8a/fixLength.spec.js diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index f6c732b11..8289c1456 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -3,6 +3,7 @@ Utility methods to convert to and from `Uint8Array` objects - [u8aConcat](#u8aconcat) Creates a concatenated Uint8Array from the inputs. +- [u8aFixLength](#u8afixlength) Shifts a Uint8Array to a specific bitLength - [u8aFromBn](#u8afrombn) Creates a Uint8Array value from a BN object. - [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object. - [u8aFromHex](#u8afromhex) Creates a Uint8Array value from a hex string. @@ -33,6 +34,25 @@ new Uint8Array([4, 5, 6]) ]); // [1, 2, 3, 4, 5, 6] ``` +## u8aFixLength + +Shifts a Uint8Array to a specific bitLength + +```js +u8aFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string +``` + + +Returns a uint8Array with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. + +```js +import { u8aFixLength } from '@polkadot/util'; + +u8aFixLength('0x12') // => 0x12 +u8aFixLength('0x12', 16) // => 0x0012 +u8aFixLength('0x1234', 8) // => 0x12 +``` + ## u8aFromBn Creates a Uint8Array value from a BN object. [(alias of bnToU8a)](bn.md#bntou8a) diff --git a/packages/util/src/hex/fixLength.js b/packages/util/src/hex/fixLength.js index 188c73e46..a27bc8983 100644 --- a/packages/util/src/hex/fixLength.js +++ b/packages/util/src/hex/fixLength.js @@ -21,7 +21,7 @@ const { ZEROS_256 } = require('./constants'); console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 */ module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string { - const strLength = bitLength / 4; + const strLength = Math.ceil(bitLength / 4); const hexLength = strLength + 2; if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) { diff --git a/packages/util/src/u8a/fixLength.js b/packages/util/src/u8a/fixLength.js new file mode 100644 index 000000000..5cba61fe9 --- /dev/null +++ b/packages/util/src/u8a/fixLength.js @@ -0,0 +1,35 @@ +// 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 u8aFixLength + @signature u8aFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string + @summary Shifts a Uint8Array to a specific bitLength + @description + Returns a uint8Array with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. + @example + import { u8aFixLength } from '@polkadot/util'; + + u8aFixLength('0x12') // => 0x12 + u8aFixLength('0x12', 16) // => 0x0012 + u8aFixLength('0x1234', 8) // => 0x12 +*/ +module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1): Uint8Array { + const byteLength = Math.ceil(bitLength / 8); + + if (bitLength === -1 || value.length === byteLength) { + return value; + } + + if (value.length > byteLength) { + return value.subarray(0, byteLength); + } + + const result = new Uint8Array(byteLength); + + result.set(value, byteLength - value.length); + + return result; +}; diff --git a/packages/util/src/u8a/fixLength.spec.js b/packages/util/src/u8a/fixLength.spec.js new file mode 100644 index 000000000..2968baf27 --- /dev/null +++ b/packages/util/src/u8a/fixLength.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 { u8aFixLength } = require('./index'); + +describe('u8aFixLength', () => { + it('returns bitLength === -1 as-is', () => { + expect( + u8aFixLength( + new Uint8Array([0x12, 0x34, 0x56, 0x78]) + ) + ).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78])); + }); + + it('does not change when bitlength === length', () => { + expect( + u8aFixLength(new Uint8Array([0x12, 0x34, 0x56, 0x78]), 32) + ).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78])); + }); + + it('trims values when bitLength > length', () => { + expect( + u8aFixLength(new Uint8Array([0x12, 0x34, 0x56, 0x78]), 16) + ).toEqual(new Uint8Array([0x12, 0x34])); + }); + + it('adds zeros when bitLength < length (withPadded)', () => { + expect( + u8aFixLength(new Uint8Array([0x12, 0x34]), 32) + ).toEqual(new Uint8Array([0, 0, 0x12, 0x34])); + }); +}); diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index 647adaffc..ed2f66e0a 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -4,6 +4,7 @@ // @flow const u8aConcat = require('./concat'); +const u8aFixLength = require('./fixLength'); const u8aFromBn = require('./fromBn'); const u8aFromBuffer = require('./fromBuffer'); const u8aFromHex = require('./fromHex'); @@ -19,6 +20,7 @@ const u8aToUtf8 = require('./toUtf8'); */ module.exports = { u8aConcat, + u8aFixLength, u8aFromBn, u8aFromBuffer, u8aFromHex,