From 366965371f608d359087e9b40c650a4ad527e48d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 15 Dec 2017 20:59:27 +0100 Subject: [PATCH] Don't pad fixLength by default (#51) * Don't pad fixLength by default * Update docs --- docs/hex.md | 11 ++++++----- src/hex/fixLength.js | 21 ++++++++++----------- src/hex/fixLength.spec.js | 30 +++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/docs/hex.md b/docs/hex.md index 708ac6e06..814aff912 100644 --- a/docs/hex.md +++ b/docs/hex.md @@ -3,7 +3,7 @@ Internal utilities to create and test for hex values - [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values. -- [hexFixLength](#hexfixlength) Pads or shifts a hex string to a specific bitLength +- [hexFixLength](#hexfixlength) Shifts a hex string to a specific bitLength - [hexFromBn](#hexfrombn) Creates a hex value from a BN.js bignumber object. - [hexFromBn](#hexfrombn) Creates a hex value from a Buffer object. - [hexFromNumber](#hexfromnumber) Creates a hex value from a number. @@ -32,19 +32,20 @@ console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12 ## hexFixLength -Pads or shifts a hex string to a specific bitLength +Shifts a hex string to a specific bitLength ```js -hexFixLength (value: string, bitLength: number = -1): string +hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string ``` -Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length. +Returns a `0x` prefixed string 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. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`. ```js import { hexFixLength } from '@polkadot/util'; -console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012 +console.log('fixed', hexFixLength('0x12', 16)) // => 0x12 +console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012 console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 ``` diff --git a/src/hex/fixLength.js b/src/hex/fixLength.js index 0e8062808..f4f326947 100644 --- a/src/hex/fixLength.js +++ b/src/hex/fixLength.js @@ -9,28 +9,27 @@ const { ZEROS_256 } = require('./constants'); /** @name hexFixLength - @signature hexFixLength (value: string, bitLength: number = -1): string - @summary Pads or shifts a hex string to a specific bitLength + @signature hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string + @summary Shifts a hex string to a specific bitLength @description - Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length. + Returns a `0x` prefixed string 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. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`. @example import { hexFixLength } from '@polkadot/util'; - console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012 + console.log('fixed', hexFixLength('0x12', 16)) // => 0x12 + console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012 console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 */ -module.exports = function hexFixLength (value: string, bitLength: number = -1): string { +module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string { assert(isHex(value), `Expected hex input value, found '${value}' instead`); - if (bitLength === -1) { - return value; - } - const strLength = bitLength / 4; const hexLength = strLength + 2; - if (value.length === hexLength) { - return value; + if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) { + return hexAddPrefix( + hexStripPrefix(value) + ); } if (value.length > hexLength) { diff --git a/src/hex/fixLength.spec.js b/src/hex/fixLength.spec.js index 1b21bee72..f082f20d1 100644 --- a/src/hex/fixLength.spec.js +++ b/src/hex/fixLength.spec.js @@ -15,16 +15,10 @@ describe('hexFixLength', () => { ).toEqual('0x12345678'); }); - it('trims values when bitLength > length', () => { + it('returns bitLength === -1 as-is (adding missing 0)', () => { expect( - hexFixLength('0x12345678', 16) - ).toEqual('0x5678'); - }); - - it('adds zeros when bitLength < length', () => { - expect( - hexFixLength('0x1234', 32) - ).toEqual('0x00001234'); + hexFixLength('0x1234567') + ).toEqual('0x01234567'); }); it('does not change when bitlength === length', () => { @@ -32,4 +26,22 @@ describe('hexFixLength', () => { hexFixLength('0x12345678', 32) ).toEqual('0x12345678'); }); + + it('trims values when bitLength > length', () => { + expect( + hexFixLength('0x12345678', 16) + ).toEqual('0x5678'); + }); + + it('returns as-is when bitLength < length', () => { + expect( + hexFixLength('0x1234', 32) + ).toEqual('0x1234'); + }); + + it('adds zeros when bitLength < length (withPadded)', () => { + expect( + hexFixLength('0x1234', 32, true) + ).toEqual('0x00001234'); + }); });